From 9ac54194d04bafc5431632e22aa25ec59297486c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 12 Aug 2025 12:30:20 +0000 Subject: [PATCH 1/3] Add default param for getText --- src/Locale/Locale.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Locale/Locale.php b/src/Locale/Locale.php index 0e610a2..2fa94bb 100644 --- a/src/Locale/Locale.php +++ b/src/Locale/Locale.php @@ -6,6 +6,8 @@ class Locale { + const string DEFAULT_DYNAMIC_KEY = '[[defaultDynamciKey]]'; // Replaced at runime by $key wrapped in {{ and }} + /** * @var array> */ @@ -121,16 +123,17 @@ public function setDefault(string $name): self * * @param string $key * @param array $placeholders + * @param string|null $default * @return mixed * * @throws Exception */ - public function getText(string $key, array $placeholders = []) + public function getText(string $key, array $placeholders = [], string|null $default = self::DEFAULT_DYNAMIC_KEY) { $defaultExists = \array_key_exists($key, self::$language[$this->default]); $fallbackExists = \array_key_exists($key, self::$language[$this->fallback ?? ''] ?? []); - $translation = '{{'.$key.'}}'; + $translation = $default === self::DEFAULT_DYNAMIC_KEY ? '{{' . $key . '}}' : $default; if ($fallbackExists) { $translation = self::$language[$this->fallback ?? ''][$key]; From 26347e4173cce40b8d43dbeef7ccaa9b9b0fd828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 12 Aug 2025 12:35:59 +0000 Subject: [PATCH 2/3] Add text defauls from version 0.4.x --- src/Locale/Locale.php | 10 +++++++--- tests/Locale/LocaleTest.php | 19 +++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Locale/Locale.php b/src/Locale/Locale.php index 2fa94bb..1bd1fce 100644 --- a/src/Locale/Locale.php +++ b/src/Locale/Locale.php @@ -123,17 +123,17 @@ public function setDefault(string $name): self * * @param string $key * @param array $placeholders - * @param string|null $default + * @param string|null $default * @return mixed * * @throws Exception */ - public function getText(string $key, array $placeholders = [], string|null $default = self::DEFAULT_DYNAMIC_KEY) + public function getText(string $key, string|null $default = self::DEFAULT_DYNAMIC_KEY, array $placeholders = []) { $defaultExists = \array_key_exists($key, self::$language[$this->default]); $fallbackExists = \array_key_exists($key, self::$language[$this->fallback ?? ''] ?? []); - $translation = $default === self::DEFAULT_DYNAMIC_KEY ? '{{' . $key . '}}' : $default; + $translation = $default === self::DEFAULT_DYNAMIC_KEY ? '{{'.$key.'}}' : $default; if ($fallbackExists) { $translation = self::$language[$this->fallback ?? ''][$key]; @@ -147,6 +147,10 @@ public function getText(string $key, array $placeholders = [], string|null $defa throw new Exception('Key named "'.$key.'" not found'); } + if (\is_null($translation)) { + return null; + } + foreach ($placeholders as $placeholderKey => $placeholderValue) { $translation = str_replace('{{'.$placeholderKey.'}}', (string) $placeholderValue, $translation); } diff --git a/tests/Locale/LocaleTest.php b/tests/Locale/LocaleTest.php index 57709f0..cef251e 100755 --- a/tests/Locale/LocaleTest.php +++ b/tests/Locale/LocaleTest.php @@ -65,20 +65,20 @@ public function testTexts(): void // Test placeholders $locale->setDefault('en-US'); - $this->assertEquals('Hello Matej Bačo!', $locale->getText('helloPlaceholder', [ + $this->assertEquals('Hello Matej Bačo!', $locale->getText('helloPlaceholder', placeholders: [ 'name' => 'Matej', 'surname' => 'Bačo', ])); - $this->assertEquals('Hello Matej {{surname}}!', $locale->getText('helloPlaceholder', [ + $this->assertEquals('Hello Matej {{surname}}!', $locale->getText('helloPlaceholder', placeholders: [ 'name' => 'Matej', ])); $this->assertEquals('Hello {{name}} {{surname}}!', $locale->getText('helloPlaceholder')); - $this->assertEquals('We have 12 users registered.', $locale->getText('numericPlaceholder', [ + $this->assertEquals('We have 12 users registered.', $locale->getText('numericPlaceholder', placeholders: [ 'usersAmount' => 6 + 6, ])); - $this->assertEquals('Lets repeat: Appwrite, Appwrite, Appwrite', $locale->getText('multiplePlaceholders', [ + $this->assertEquals('Lets repeat: Appwrite, Appwrite, Appwrite', $locale->getText('multiplePlaceholders', placeholders: [ 'word' => 'Appwrite', ])); @@ -120,4 +120,15 @@ public function testFallback(): void $this->assertInstanceOf(Exception::class, $e); } } + + public function testGetTextDefault(): void + { + $locale = new Locale('en-US'); + + $this->assertEquals('Hello', $locale->getText('hello')); + $this->assertEquals('{{missing}}', $locale->getText('missing')); + $this->assertEquals('A custom text', $locale->getText('missing', default: 'A custom text')); + $this->assertEquals(null, $locale->getText('missing', default: null)); + $this->assertEquals('Sorry Matej, missing text', $locale->getText('missing', placeholders: ['name' => 'Matej'], default: 'Sorry {{name}}, missing text')); + } } From 3bd4495b66607f02e6e79d1264f155e5c38843ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 12 Aug 2025 12:50:01 +0000 Subject: [PATCH 3/3] Fix typo --- src/Locale/Locale.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Locale/Locale.php b/src/Locale/Locale.php index 1bd1fce..7b1cf73 100644 --- a/src/Locale/Locale.php +++ b/src/Locale/Locale.php @@ -6,7 +6,7 @@ class Locale { - const string DEFAULT_DYNAMIC_KEY = '[[defaultDynamciKey]]'; // Replaced at runime by $key wrapped in {{ and }} + const string DEFAULT_DYNAMIC_KEY = '[[defaultDynamicKey]]'; // Replaced at runime by $key wrapped in {{ and }} /** * @var array>