Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Locale/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Locale
{
const string DEFAULT_DYNAMIC_KEY = '[[defaultDynamicKey]]'; // Replaced at runime by $key wrapped in {{ and }}

/**
* @var array<string, array<string, string>>
*/
Expand Down Expand Up @@ -121,16 +123,17 @@ public function setDefault(string $name): self
*
* @param string $key
* @param array<string, string|int> $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];
Expand All @@ -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);
}
Expand Down
19 changes: 15 additions & 4 deletions tests/Locale/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]));

Expand Down Expand Up @@ -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'));
}
}