Skip to content

Commit d36ee79

Browse files
committed
v0.1.2
Now the $default_translation argument can be a different Localizer object; so that if the current localizer cannot find the translation, it will look for its default in the second localizer. Each localizer can have a different localizer object as default.
1 parent 0e3e877 commit d36ee79

File tree

8 files changed

+62
-9
lines changed

8 files changed

+62
-9
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nabeghe/light-localization",
33
"description": "A light weight and path-based PHP localization library that translations are loaded up when needed.",
44
"type": "library",
5-
"version": "0.1.1",
5+
"version": "0.1.2",
66
"homepage": "https://github.com/nabeghe/light-localization",
77
"license": "MIT",
88
"autoload": {

examples/array/langs/generic/main.php examples/array/langs/en/main.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
'secondary_btn_title' => 'No',
88
'success' => 'Welcome',
99
'unsuccess' => 'Bye',
10+
'error' => 'Ops',
1011
];

examples/array/langs/fa/main.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return [
4+
'title' => 'لایت لوکالیزیشن',
5+
'message' => 'سلام عسلم، آماده‌ای؟',
6+
'primary_btn_title' => 'بله',
7+
'secondary_btn_title' => 'خیر',
8+
'success' => 'خوش اومدی',
9+
'unsuccess' => 'بای',
10+
];

examples/array/localizer.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
require_once __DIR__ . '/../../vendor/autoload.php';
66

7-
$localizer = new Localizer(__DIR__ . '/langs');
7+
$defaultLocalizer = new Localizer(__DIR__ . '/langs', 'en');
8+
$localizer = new Localizer(__DIR__ . '/langs', 'fa' , $defaultLocalizer);
89
echo $localizer->get('title');
910
echo PHP_EOL;
1011
echo $localizer->get('message');
@@ -13,4 +14,6 @@
1314
echo PHP_EOL;
1415
echo $localizer->get('success');
1516
echo PHP_EOL;
16-
echo $localizer->get('unsuccess');
17+
echo $localizer->get('unsuccess');
18+
echo PHP_EOL;
19+
echo $localizer->get('error'); // It doesn't exist in 'fa', so it takes from 'en'.

examples/class/langs/generic/main.php examples/class/langs/en/main.php

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
public string $unsuccess = 'Bye';
1515

16+
public string $ops = 'Ops';
17+
1618
public function message()
1719
{
1820
$msgs = [

examples/class/langs/fa/main.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Nabeghe\LightLocalization\Translator;
4+
5+
return new class extends Translator {
6+
public string $title = 'لایت لوکالیزیشن';
7+
8+
public string $primary_btn_title = 'بله';
9+
10+
public string $secondary_btn_title = 'خیر';
11+
12+
public string $success = 'خوش اومدی';
13+
14+
public string $unsuccess = 'بای';
15+
16+
public function message()
17+
{
18+
$msgs = [
19+
'سلام عسلم، آماده‌ای؟',
20+
'هی عزیزم، بریم؟',
21+
'آره عزیزم... با من بیا.',
22+
'سلام عزیزم، برای تفریح آماده‌ای؟',
23+
'هی عشقم، نظرت در مورد یک ماجراجویی کوچک چیست؟',
24+
'عزیزم، آیا این روز را به یک روز به یاد ماندنی تبدیل کنیم؟',
25+
];
26+
return $msgs[array_rand($msgs)];
27+
}
28+
};

examples/class/localizer.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
require_once __DIR__ . '/../../vendor/autoload.php';
66

7-
$localizer = new Localizer(__DIR__ . '/langs');
7+
$defaultLocalizer = new Localizer(__DIR__ . '/langs', 'en');
8+
$localizer = new Localizer(__DIR__ . '/langs', 'fa', $defaultLocalizer);
89
var_dump($localizer->getTranslators());
910
echo $localizer->get('title');
1011
echo PHP_EOL;
11-
echo $localizer->get('message');
12+
echo $localizer->get('message'); // Random message
1213
echo PHP_EOL;
1314
echo $localizer->get('primary_btn_title') . '|' . $localizer->get('secondary_btn_title');
1415
echo PHP_EOL;
1516
echo $localizer->get('success');
1617
echo PHP_EOL;
17-
echo $localizer->get('unsuccess');
18+
echo $localizer->get('unsuccess');
19+
echo PHP_EOL;
20+
echo $localizer->get('ops');

src/Localizer.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Localizer
2828
/**
2929
* The default translation returned if the requested key (translation) doesn't exist.
3030
* @see self::get()
31-
* @var mixed
31+
* @var Localizer|string
3232
*/
3333
protected $defaultTranslation;
3434

@@ -51,7 +51,7 @@ public function getTranslators(): array
5151
* Constructor.
5252
* @param string $path The root path where the directories related to the codes are located.
5353
* @param string $code Optional. Localization code. Default generic.
54-
* @param string $default_translation Optional. The default translation returned if the requested key (translation) doesn't exist.. Default empty string.
54+
* @param Localizer|string $default_translation Optional. The default translation returned if the requested key (translation) doesn't exist.. Default empty string.
5555
*/
5656
public function __construct(string $path, string $code = 'generic', $default_translation = '')
5757
{
@@ -151,6 +151,12 @@ public function get(string $key, $translator_name = self::DEFAULT_TRANSLATOR)
151151
if (!isset($this->translators[$translator_name])) {
152152
$this->load($translator_name);
153153
}
154-
return $this->translators[$translator_name][$key] ?? $this->defaultTranslation;
154+
if (isset($this->translators[$translator_name][$key])) {
155+
return $this->translators[$translator_name][$key];
156+
}
157+
if (is_string($this->defaultTranslation)) {
158+
return $this->defaultTranslation;
159+
}
160+
return $this->defaultTranslation->get($key, $translator_name);
155161
}
156162
}

0 commit comments

Comments
 (0)