Skip to content

Commit 620b9f2

Browse files
authored
Merge branch 'master' into feat-fallback-translations
2 parents 02bed77 + 2a817a2 commit 620b9f2

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Locale/Locale.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ class Locale
3333
*/
3434
public $fallback = null;
3535

36+
/**
37+
* Get list of configured languages
38+
*
39+
* @return array<string>
40+
*/
41+
public static function getLanguages(): array
42+
{
43+
return \array_keys(self::$language);
44+
}
45+
3646
/**
3747
* Set New Locale from an array
3848
*
@@ -140,4 +150,14 @@ public function getText(string $key, array $placeholders = [])
140150

141151
return $translation;
142152
}
153+
154+
/**
155+
* Get list of configured transltions in specific language
156+
*
157+
* @return array<string, string>
158+
*/
159+
public function getTranslations(): array
160+
{
161+
return self::$language[$this->default];
162+
}
143163
}

tests/Locale/LocaleTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ public function setUp(): void
1717
{
1818
Locale::$exceptions = false; // Disable exceptions
1919

20+
$this->assertCount(0, Locale::getLanguages());
21+
2022
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
23+
24+
$this->assertCount(1, Locale::getLanguages());
25+
2126
Locale::setLanguageFromArray('he-IL', ['hello' => 'שלום']); // Set Hebrew
27+
28+
$this->assertCount(2, Locale::getLanguages());
29+
2230
Locale::setLanguageFromJSON('hi-IN', realpath(__DIR__.'/../hi-IN.json') ?: ''); // Set Hindi
31+
32+
$this->assertCount(3, Locale::getLanguages());
2333
}
2434

2535
public function tearDown(): void
@@ -33,16 +43,24 @@ public function testTexts(): void
3343
$this->assertEquals('Hello', $locale->getText('hello'));
3444
$this->assertEquals('World', $locale->getText('world'));
3545

46+
$translations = $locale->getTranslations();
47+
$this->assertCount(5, $translations);
48+
$this->assertEquals(['hello' => 'Hello', 'world' => 'World', 'helloPlaceholder' => 'Hello {{name}} {{surname}}!', 'numericPlaceholder' => 'We have {{usersAmount}} users registered.', 'multiplePlaceholders' => 'Lets repeat: {{word}}, {{word}}, {{word}}'], $translations);
49+
3650
$locale->setDefault('hi-IN');
3751

3852
$this->assertEquals('Namaste', $locale->getText('hello'));
3953
$this->assertEquals('Duniya', $locale->getText('world'));
4054

55+
$this->assertCount(2, $locale->getTranslations());
56+
4157
$locale->setDefault('he-IL');
4258

4359
$this->assertEquals('שלום', $locale->getText('hello'));
4460
// $this->assertEquals('empty', $locale->getText('world', 'empty')); Has been removed in 0.5.0
4561

62+
$this->assertCount(1, $locale->getTranslations());
63+
4664
// Test placeholders
4765
$locale->setDefault('en-US');
4866

@@ -65,6 +83,7 @@ public function testTexts(): void
6583

6684
// Test exceptions
6785
$locale->setDefault('he-IL');
86+
6887
Locale::$exceptions = true;
6988

7089
try {

0 commit comments

Comments
 (0)