Skip to content

Commit 5a520d7

Browse files
committed
Lang part
0 parents  commit 5a520d7

10 files changed

Lines changed: 191 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/*

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 QuentinBubu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Translation library
2+
3+
## Usage
4+
5+
```php
6+
<?php
7+
8+
use Bubu\Lang\Lang;
9+
10+
// Setup new translation
11+
$trad = [
12+
'hello' => [
13+
Lang::FR => 'Bonjour',
14+
Lang::EN => 'Hello'
15+
],
16+
17+
'how' => [
18+
Lang::FR => 'Comment'
19+
]
20+
];
21+
22+
Lang::registre($trad);
23+
24+
// Change default lang
25+
Lang::setLang(Lang::FR);
26+
27+
// Get translation
28+
echo Lang::get('hello');
29+
30+
```

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "bubu-framework/lang",
3+
"description": "Bubu-framework's Langs",
4+
"type": "library",
5+
"license": "MIT",
6+
"homepage": "https://github.com/bubu-framework/lang",
7+
"authors": [
8+
{
9+
"name": "QuentinBubu",
10+
"email": "QuentinBubu.dev@gmail.com"
11+
}
12+
],
13+
"autoload": {
14+
"psr-4": {
15+
"Bubu\\Lang\\": "src/"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Bubu\\Lang\\Tests\\" : "tests/"
21+
}
22+
}
23+
}

composer.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"error":"Error","existing-username":"Username already registre!","existing-email":"Email already registre!","email-not-verified":"Email was not verified!","not-same-password":"Password did not match!","password-length":"Your password should be contains between 10 and 30 characters!","username-length":"Your username should be contains minimum 3 characters!","undefined-error":"An error has occurred!","account-not-found":"Account not found!","incorrect-password":"Incorrect password!","unauthorize":"Access forbidden!","disconnect":"Disconnect!","ask-username":"Your username","ask-password":"Your password","ask-email":"Your email","ask-keep-connected":"Keep connected ?","login-button":"Login","signup-button":"Sign up","hello":"Hello"}

lang/fr.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"error":"Erreur","existing-username":"Nom d'utilisateur d\u00e9j\u00e0 existant!","existing-email":"Email d\u00e9j\u00e0 existant!","email-not-verified":"L'email n'a pas \u00e9t\u00e9 valid\u00e9!","not-same-password":"Les mots de passe ne correspondent pas!","password-length":"Saisissez un mot de passe entre 10 et 30 caract\u00e8res!","username-length":"Votre identifiant doit contenir au moins 3 caract\u00e8res inclus!","undefined-error":"Une erreur s'est produite!","account-not-found":"Compte introuvable!","incorrect-password":"Mot de passe incorrect!","unauthorize":"Vous n'\u00eates pas autoris\u00e9 \u00e0 acc\u00e9der \u00e0 cette page!","disconnect":"D\u00e9connect\u00e9!","ask-username":"Votre nom d'utilisateur","ask-password":"Votre mot de passe","ask-email":"Votre mail","ask-keep-connected":"Rester connecter?","login-button":"Se connecter","signup-button":"S'inscrire","hello":"Bonjour","how":"Comment"}

src/Exception/LangException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Exception;
4+
5+
class LangException extends \Exception
6+
{
7+
}

src/Lang.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Bubu\Lang;
4+
5+
use Exception\LangException;
6+
7+
class Lang
8+
{
9+
10+
public const AUTO = 'AUTO';
11+
public const FR = 'fr';
12+
public const EN = 'en';
13+
14+
private const FILE_LOC = '../lang/';
15+
16+
public static string $lang = self::AUTO;
17+
18+
public static function setLang(string $lang): string
19+
{
20+
self::$lang = $lang;
21+
return self::$lang;
22+
}
23+
24+
public static function get(string $name): string
25+
{
26+
if (self::$lang == self::AUTO) Lang::setLang(Lang::autoLang());
27+
$content = json_decode(file_get_contents(self::FILE_LOC . self::$lang . '.json'));
28+
if (isset($content->{$name})) return $content->{$name};
29+
else throw new LangException('Translation not found');
30+
}
31+
32+
/**
33+
* register translation
34+
*
35+
* @param array $trad
36+
* [
37+
* 'index_name' => [
38+
* 'lang' => 'trad'
39+
* ]
40+
* ]
41+
* @return boolean
42+
*/
43+
public static function registre(array $trans): bool
44+
{
45+
$sorted = [];
46+
foreach ($trans as $trads => $trad) {
47+
foreach ($trad as $lang => $value) $sorted[$lang][$trads] = $value;
48+
}
49+
50+
foreach ($sorted as $lang => $trans) {
51+
$content = json_decode(file_get_contents(self::FILE_LOC . $lang . '.json'), true);
52+
$new = array_merge($content, $trans);
53+
file_put_contents(self::FILE_LOC . $lang . '.json', json_encode($new));
54+
}
55+
56+
return true;
57+
}
58+
59+
private static function autoLang(): string
60+
{
61+
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
62+
$lang = in_array($lang, get_class_vars(Lang::class)) ? $lang : 'en';
63+
return $lang;
64+
}
65+
}

tests/index.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Bubu\Lang\Lang;
4+
5+
require '../vendor/autoload.php';
6+
7+
$trad = [
8+
'hello' => [
9+
Lang::FR => 'Bonjour',
10+
Lang::EN => 'Hello'
11+
],
12+
13+
'how' => [
14+
Lang::FR => 'Comment'
15+
]
16+
];
17+
18+
Lang::registre($trad);
19+
20+
21+
echo Lang::setLang(Lang::FR);
22+
echo Lang::get('hello');
23+
24+

0 commit comments

Comments
 (0)