diff --git a/composer.json b/composer.json index c1f8ac7..1b69ef7 100755 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "phpunit/phpunit": "^9.3", "vimeo/psalm": "4.0.1", "laravel/pint": "1.2.*", - "phpstan/phpstan": "1.9.x-dev" + "phpstan/phpstan": "1.*" }, "scripts": { "lint": "./vendor/bin/pint --test", diff --git a/src/Locale/Locale.php b/src/Locale/Locale.php index 7d5640a..fc8ecce 100644 --- a/src/Locale/Locale.php +++ b/src/Locale/Locale.php @@ -25,6 +25,16 @@ class Locale */ public $default; + /** + * Get list of configured languages + * + * @return array + */ + public static function getLanguages(): array + { + return \array_keys(self::$language); + } + /** * Set New Locale from an array * @@ -109,4 +119,14 @@ public function getText(string $key, array $placeholders = []) return $translation; } + + /** + * Get list of configured transltions in specific language + * + * @return array + */ + public function getTranslations(): array + { + return self::$language[$this->default]; + } } diff --git a/tests/Locale/LocaleTest.php b/tests/Locale/LocaleTest.php index a012f73..3b0b0d2 100755 --- a/tests/Locale/LocaleTest.php +++ b/tests/Locale/LocaleTest.php @@ -17,9 +17,19 @@ 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()); + 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()); } public function tearDown(): void @@ -33,16 +43,24 @@ public function testTexts(): void $this->assertEquals('Hello', $locale->getText('hello')); $this->assertEquals('World', $locale->getText('world')); + $translations = $locale->getTranslations(); + $this->assertCount(5, $translations); + $this->assertEquals(['hello' => 'Hello', 'world' => 'World', 'helloPlaceholder' => 'Hello {{name}} {{surname}}!', 'numericPlaceholder' => 'We have {{usersAmount}} users registered.', 'multiplePlaceholders' => 'Lets repeat: {{word}}, {{word}}, {{word}}'], $translations); + $locale->setDefault('hi-IN'); $this->assertEquals('Namaste', $locale->getText('hello')); $this->assertEquals('Duniya', $locale->getText('world')); + $this->assertCount(2, $locale->getTranslations()); + $locale->setDefault('he-IL'); $this->assertEquals('שלום', $locale->getText('hello')); // $this->assertEquals('empty', $locale->getText('world', 'empty')); Has been removed in 0.5.0 + $this->assertCount(1, $locale->getTranslations()); + // Test placeholders $locale->setDefault('en-US'); @@ -65,6 +83,7 @@ public function testTexts(): void // Test exceptions $locale->setDefault('he-IL'); + Locale::$exceptions = true; try {