Skip to content

Commit 10ffc86

Browse files
authored
Merge pull request #18 from utopia-php/feat-get-text-default
Feat: getText default
2 parents 301984f + 3bd4495 commit 10ffc86

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/Locale/Locale.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class Locale
88
{
9+
const string DEFAULT_DYNAMIC_KEY = '[[defaultDynamicKey]]'; // Replaced at runime by $key wrapped in {{ and }}
10+
911
/**
1012
* @var array<string, array<string, string>>
1113
*/
@@ -121,16 +123,17 @@ public function setDefault(string $name): self
121123
*
122124
* @param string $key
123125
* @param array<string, string|int> $placeholders
126+
* @param string|null $default
124127
* @return mixed
125128
*
126129
* @throws Exception
127130
*/
128-
public function getText(string $key, array $placeholders = [])
131+
public function getText(string $key, string|null $default = self::DEFAULT_DYNAMIC_KEY, array $placeholders = [])
129132
{
130133
$defaultExists = \array_key_exists($key, self::$language[$this->default]);
131134
$fallbackExists = \array_key_exists($key, self::$language[$this->fallback ?? ''] ?? []);
132135

133-
$translation = '{{'.$key.'}}';
136+
$translation = $default === self::DEFAULT_DYNAMIC_KEY ? '{{'.$key.'}}' : $default;
134137

135138
if ($fallbackExists) {
136139
$translation = self::$language[$this->fallback ?? ''][$key];
@@ -144,6 +147,10 @@ public function getText(string $key, array $placeholders = [])
144147
throw new Exception('Key named "'.$key.'" not found');
145148
}
146149

150+
if (\is_null($translation)) {
151+
return null;
152+
}
153+
147154
foreach ($placeholders as $placeholderKey => $placeholderValue) {
148155
$translation = str_replace('{{'.$placeholderKey.'}}', (string) $placeholderValue, $translation);
149156
}

tests/Locale/LocaleTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,20 @@ public function testTexts(): void
6565
// Test placeholders
6666
$locale->setDefault('en-US');
6767

68-
$this->assertEquals('Hello Matej Bačo!', $locale->getText('helloPlaceholder', [
68+
$this->assertEquals('Hello Matej Bačo!', $locale->getText('helloPlaceholder', placeholders: [
6969
'name' => 'Matej',
7070
'surname' => 'Bačo',
7171
]));
72-
$this->assertEquals('Hello Matej {{surname}}!', $locale->getText('helloPlaceholder', [
72+
$this->assertEquals('Hello Matej {{surname}}!', $locale->getText('helloPlaceholder', placeholders: [
7373
'name' => 'Matej',
7474
]));
7575
$this->assertEquals('Hello {{name}} {{surname}}!', $locale->getText('helloPlaceholder'));
7676

77-
$this->assertEquals('We have 12 users registered.', $locale->getText('numericPlaceholder', [
77+
$this->assertEquals('We have 12 users registered.', $locale->getText('numericPlaceholder', placeholders: [
7878
'usersAmount' => 6 + 6,
7979
]));
8080

81-
$this->assertEquals('Lets repeat: Appwrite, Appwrite, Appwrite', $locale->getText('multiplePlaceholders', [
81+
$this->assertEquals('Lets repeat: Appwrite, Appwrite, Appwrite', $locale->getText('multiplePlaceholders', placeholders: [
8282
'word' => 'Appwrite',
8383
]));
8484

@@ -120,4 +120,15 @@ public function testFallback(): void
120120
$this->assertInstanceOf(Exception::class, $e);
121121
}
122122
}
123+
124+
public function testGetTextDefault(): void
125+
{
126+
$locale = new Locale('en-US');
127+
128+
$this->assertEquals('Hello', $locale->getText('hello'));
129+
$this->assertEquals('{{missing}}', $locale->getText('missing'));
130+
$this->assertEquals('A custom text', $locale->getText('missing', default: 'A custom text'));
131+
$this->assertEquals(null, $locale->getText('missing', default: null));
132+
$this->assertEquals('Sorry Matej, missing text', $locale->getText('missing', placeholders: ['name' => 'Matej'], default: 'Sorry {{name}}, missing text'));
133+
}
123134
}

0 commit comments

Comments
 (0)