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
51 changes: 41 additions & 10 deletions src/Locale/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ class Locale
*/
public $default;

/**
* Fallback locale. Used when specific or default locale is missing translation.
* Should always be set to locale that includes all translations.
*
* @var string|null
*/
public $fallback = null;

/**
* Get list of configured languages
*
Expand Down Expand Up @@ -54,7 +62,7 @@ public static function setLanguageFromArray(string $name, array $translations):
*/
public static function setLanguageFromJSON(string $name, string $path): void
{
if (! file_exists($path)) {
if (! file_exists($path) && self::$exceptions) {
throw new Exception('Translation file not found.');
}

Expand All @@ -65,13 +73,31 @@ public static function setLanguageFromJSON(string $name, string $path): void

public function __construct(string $default)
{
if (! \array_key_exists($default, self::$language)) {
if (! \array_key_exists($default, self::$language) && self::$exceptions) {
throw new Exception('Locale not found');
}

$this->default = $default;
}

/**
* Change fallback Locale
*
* @param $name
*
* @throws Exception
*/
public function setFallback(string $name): self
{
if (! \array_key_exists($name, self::$language) && self::$exceptions) {
throw new Exception('Locale not found');
}

$this->fallback = $name;

return $this;
}

/**
* Change Default Locale
*
Expand All @@ -81,7 +107,7 @@ public function __construct(string $default)
*/
public function setDefault(string $name): self
{
if (! \array_key_exists($name, self::$language)) {
if (! \array_key_exists($name, self::$language) && self::$exceptions) {
throw new Exception('Locale not found');
}

Expand All @@ -101,17 +127,22 @@ public function setDefault(string $name): self
*/
public function getText(string $key, array $placeholders = [])
{
$default = '{{'.$key.'}}';
$defaultExists = \array_key_exists($key, self::$language[$this->default]);
$fallbackExists = \array_key_exists($key, self::$language[$this->fallback ?? ''] ?? []);

if (! \array_key_exists($key, self::$language[$this->default])) {
if (self::$exceptions) {
throw new Exception('Key named "'.$key.'" not found');
}
$translation = '{{'.$key.'}}';

return $default;
if ($fallbackExists) {
$translation = self::$language[$this->fallback ?? ''][$key];
}

$translation = self::$language[$this->default][$key];
if ($defaultExists) {
$translation = self::$language[$this->default][$key];
}

if (! $defaultExists && ! $fallbackExists && self::$exceptions) {
throw new Exception('Key named "'.$key.'" not found');
}

foreach ($placeholders as $placeholderKey => $placeholderValue) {
$translation = str_replace('{{'.$placeholderKey.'}}', (string) $placeholderValue, $translation);
Expand Down
38 changes: 31 additions & 7 deletions tests/Locale/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ public function setUp(): void
{
Locale::$exceptions = false; // Disable exceptions

$this->assertCount(0, Locale::getLanguages());

Locale::setLanguageFromArray('en-US', ['hello' => 'Hello', 'world' => 'World', 'helloPlaceholder' => 'Hello {{name}} {{surname}}!', 'numericPlaceholder' => 'We have {{usersAmount}} users registered.', 'multiplePlaceholders' => 'Lets repeat: {{word}}, {{word}}, {{word}}']); // Set English

$this->assertCount(1, Locale::getLanguages());
// Set English
Locale::setLanguageFromArray('en-US', [
'hello' => 'Hello',
'world' => 'World',
'helloPlaceholder' => 'Hello {{name}} {{surname}}!',
'numericPlaceholder' => 'We have {{usersAmount}} users registered.',
'multiplePlaceholders' => 'Lets repeat: {{word}}, {{word}}, {{word}}',
]);

Locale::setLanguageFromArray('he-IL', ['hello' => 'שלום']); // Set Hebrew

$this->assertCount(2, Locale::getLanguages());

Locale::setLanguageFromJSON('hi-IN', realpath(__DIR__.'/../hi-IN.json') ?: ''); // Set Hindi

$this->assertCount(3, Locale::getLanguages());
Expand Down Expand Up @@ -96,4 +97,27 @@ public function testTexts(): void

$this->fail('No exception was thrown');
}

public function testFallback(): void
{
$locale = new Locale('he-IL');

$this->assertEquals('שלום', $locale->getText('hello'));
$this->assertEquals('{{world}}', $locale->getText('world'));
$this->assertEquals('{{missing}}', $locale->getText('missing'));

$locale->setFallback('en-US');

$this->assertEquals('שלום', $locale->getText('hello'));
$this->assertEquals('World', $locale->getText('world'));
$this->assertEquals('{{missing}}', $locale->getText('missing'));

Locale::$exceptions = true;
try {
$locale->getText('missing');
$this->fail('Failed to throw exception when translation is missing');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
}
}
}