From d36ee79dfae2d7b662576769c9c2f1f29f867ea9 Mon Sep 17 00:00:00 2001 From: Hadi Akbarzadeh Date: Tue, 2 Apr 2024 02:53:41 +0330 Subject: [PATCH] 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. --- composer.json | 2 +- examples/array/langs/{generic => en}/main.php | 1 + examples/array/langs/fa/main.php | 10 +++++++ examples/array/localizer.php | 7 +++-- examples/class/langs/{generic => en}/main.php | 2 ++ examples/class/langs/fa/main.php | 28 +++++++++++++++++++ examples/class/localizer.php | 9 ++++-- src/Localizer.php | 12 ++++++-- 8 files changed, 62 insertions(+), 9 deletions(-) rename examples/array/langs/{generic => en}/main.php (91%) create mode 100644 examples/array/langs/fa/main.php rename examples/class/langs/{generic => en}/main.php (95%) create mode 100644 examples/class/langs/fa/main.php diff --git a/composer.json b/composer.json index aa43aa2..fb1a2e8 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/examples/array/langs/generic/main.php b/examples/array/langs/en/main.php similarity index 91% rename from examples/array/langs/generic/main.php rename to examples/array/langs/en/main.php index 4d6de6e..ac9f6d1 100644 --- a/examples/array/langs/generic/main.php +++ b/examples/array/langs/en/main.php @@ -7,4 +7,5 @@ 'secondary_btn_title' => 'No', 'success' => 'Welcome', 'unsuccess' => 'Bye', + 'error' => 'Ops', ]; \ No newline at end of file diff --git a/examples/array/langs/fa/main.php b/examples/array/langs/fa/main.php new file mode 100644 index 0000000..c187785 --- /dev/null +++ b/examples/array/langs/fa/main.php @@ -0,0 +1,10 @@ + 'لایت لوکالیزیشن', + 'message' => 'سلام عسلم، آماده‌ای؟', + 'primary_btn_title' => 'بله', + 'secondary_btn_title' => 'خیر', + 'success' => 'خوش اومدی', + 'unsuccess' => 'بای', +]; \ No newline at end of file diff --git a/examples/array/localizer.php b/examples/array/localizer.php index 5741c49..9e7afea 100644 --- a/examples/array/localizer.php +++ b/examples/array/localizer.php @@ -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'); @@ -13,4 +14,6 @@ echo PHP_EOL; echo $localizer->get('success'); echo PHP_EOL; -echo $localizer->get('unsuccess'); \ No newline at end of file +echo $localizer->get('unsuccess'); +echo PHP_EOL; +echo $localizer->get('error'); // It doesn't exist in 'fa', so it takes from 'en'. \ No newline at end of file diff --git a/examples/class/langs/generic/main.php b/examples/class/langs/en/main.php similarity index 95% rename from examples/class/langs/generic/main.php rename to examples/class/langs/en/main.php index 2567b1a..160f5f4 100644 --- a/examples/class/langs/generic/main.php +++ b/examples/class/langs/en/main.php @@ -13,6 +13,8 @@ public string $unsuccess = 'Bye'; + public string $ops = 'Ops'; + public function message() { $msgs = [ diff --git a/examples/class/langs/fa/main.php b/examples/class/langs/fa/main.php new file mode 100644 index 0000000..b3a933e --- /dev/null +++ b/examples/class/langs/fa/main.php @@ -0,0 +1,28 @@ +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'); \ No newline at end of file +echo $localizer->get('unsuccess'); +echo PHP_EOL; +echo $localizer->get('ops'); \ No newline at end of file diff --git a/src/Localizer.php b/src/Localizer.php index 510df31..7c47069 100644 --- a/src/Localizer.php +++ b/src/Localizer.php @@ -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; @@ -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 = '') { @@ -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); } } \ No newline at end of file