Skip to content

Commit 2a817a2

Browse files
authored
Merge pull request #12 from utopia-php/feat-get-languages
Feat: getLanguages(), getTranslations() methods
2 parents 2d4e6af + a0b4ae3 commit 2a817a2

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"phpunit/phpunit": "^9.3",
1616
"vimeo/psalm": "4.0.1",
1717
"laravel/pint": "1.2.*",
18-
"phpstan/phpstan": "1.9.x-dev"
18+
"phpstan/phpstan": "1.*"
1919
},
2020
"scripts": {
2121
"lint": "./vendor/bin/pint --test",

src/Locale/Locale.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ class Locale
2525
*/
2626
public $default;
2727

28+
/**
29+
* Get list of configured languages
30+
*
31+
* @return array<string>
32+
*/
33+
public static function getLanguages(): array
34+
{
35+
return \array_keys(self::$language);
36+
}
37+
2838
/**
2939
* Set New Locale from an array
3040
*
@@ -109,4 +119,14 @@ public function getText(string $key, array $placeholders = [])
109119

110120
return $translation;
111121
}
122+
123+
/**
124+
* Get list of configured transltions in specific language
125+
*
126+
* @return array<string, string>
127+
*/
128+
public function getTranslations(): array
129+
{
130+
return self::$language[$this->default];
131+
}
112132
}

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)