Skip to content

Commit

Permalink
v0.1.2
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nabeghe committed Apr 1, 2024
1 parent 0e3e877 commit d36ee79
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nabeghe/light-localization",
"description": "A light weight and path-based PHP localization library that translations are loaded up when needed.",
"type": "library",
"version": "0.1.1",
"version": "0.1.2",
"homepage": "https://github.com/nabeghe/light-localization",
"license": "MIT",
"autoload": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
'secondary_btn_title' => 'No',
'success' => 'Welcome',
'unsuccess' => 'Bye',
'error' => 'Ops',
];
10 changes: 10 additions & 0 deletions examples/array/langs/fa/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'title' => 'لایت لوکالیزیشن',
'message' => 'سلام عسلم، آماده‌ای؟',
'primary_btn_title' => 'بله',
'secondary_btn_title' => 'خیر',
'success' => 'خوش اومدی',
'unsuccess' => 'بای',
];
7 changes: 5 additions & 2 deletions examples/array/localizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

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

$localizer = new Localizer(__DIR__ . '/langs');
$defaultLocalizer = new Localizer(__DIR__ . '/langs', 'en');
$localizer = new Localizer(__DIR__ . '/langs', 'fa' , $defaultLocalizer);
echo $localizer->get('title');
echo PHP_EOL;
echo $localizer->get('message');
Expand All @@ -13,4 +14,6 @@
echo PHP_EOL;
echo $localizer->get('success');
echo PHP_EOL;
echo $localizer->get('unsuccess');
echo $localizer->get('unsuccess');
echo PHP_EOL;
echo $localizer->get('error'); // It doesn't exist in 'fa', so it takes from 'en'.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

public string $unsuccess = 'Bye';

public string $ops = 'Ops';

public function message()
{
$msgs = [
Expand Down
28 changes: 28 additions & 0 deletions examples/class/langs/fa/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Nabeghe\LightLocalization\Translator;

return new class extends Translator {
public string $title = 'لایت لوکالیزیشن';

public string $primary_btn_title = 'بله';

public string $secondary_btn_title = 'خیر';

public string $success = 'خوش اومدی';

public string $unsuccess = 'بای';

public function message()
{
$msgs = [
'سلام عسلم، آماده‌ای؟',
'هی عزیزم، بریم؟',
'آره عزیزم... با من بیا.',
'سلام عزیزم، برای تفریح آماده‌ای؟',
'هی عشقم، نظرت در مورد یک ماجراجویی کوچک چیست؟',
'عزیزم، آیا این روز را به یک روز به یاد ماندنی تبدیل کنیم؟',
];
return $msgs[array_rand($msgs)];
}
};
9 changes: 6 additions & 3 deletions examples/class/localizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

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

$localizer = new Localizer(__DIR__ . '/langs');
$defaultLocalizer = new Localizer(__DIR__ . '/langs', 'en');
$localizer = new Localizer(__DIR__ . '/langs', 'fa', $defaultLocalizer);
var_dump($localizer->getTranslators());
echo $localizer->get('title');
echo PHP_EOL;
echo $localizer->get('message');
echo $localizer->get('message'); // Random message
echo PHP_EOL;
echo $localizer->get('primary_btn_title') . '|' . $localizer->get('secondary_btn_title');
echo PHP_EOL;
echo $localizer->get('success');
echo PHP_EOL;
echo $localizer->get('unsuccess');
echo $localizer->get('unsuccess');
echo PHP_EOL;
echo $localizer->get('ops');
12 changes: 9 additions & 3 deletions src/Localizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Localizer
/**
* The default translation returned if the requested key (translation) doesn't exist.
* @see self::get()
* @var mixed
* @var Localizer|string
*/
protected $defaultTranslation;

Expand All @@ -51,7 +51,7 @@ public function getTranslators(): array
* Constructor.
* @param string $path The root path where the directories related to the codes are located.
* @param string $code Optional. Localization code. Default generic.
* @param string $default_translation Optional. The default translation returned if the requested key (translation) doesn't exist.. Default empty string.
* @param Localizer|string $default_translation Optional. The default translation returned if the requested key (translation) doesn't exist.. Default empty string.
*/
public function __construct(string $path, string $code = 'generic', $default_translation = '')
{
Expand Down Expand Up @@ -151,6 +151,12 @@ public function get(string $key, $translator_name = self::DEFAULT_TRANSLATOR)
if (!isset($this->translators[$translator_name])) {
$this->load($translator_name);
}
return $this->translators[$translator_name][$key] ?? $this->defaultTranslation;
if (isset($this->translators[$translator_name][$key])) {
return $this->translators[$translator_name][$key];
}
if (is_string($this->defaultTranslation)) {
return $this->defaultTranslation;
}
return $this->defaultTranslation->get($key, $translator_name);
}
}

0 comments on commit d36ee79

Please sign in to comment.