diff --git a/src/Locale/Locale.php b/src/Locale/Locale.php index 0e610a2..7b1cf73 100644 --- a/src/Locale/Locale.php +++ b/src/Locale/Locale.php @@ -6,6 +6,8 @@ class Locale { + const string DEFAULT_DYNAMIC_KEY = '[[defaultDynamicKey]]'; // 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, 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 = '{{'.$key.'}}'; + $translation = $default === self::DEFAULT_DYNAMIC_KEY ? '{{'.$key.'}}' : $default; if ($fallbackExists) { $translation = self::$language[$this->fallback ?? ''][$key]; @@ -144,6 +147,10 @@ public function getText(string $key, array $placeholders = []) 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')); + } }