From 5f7b96b57763e295046f824f69534e8bdafe7f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 7 Oct 2022 07:16:39 +0000 Subject: [PATCH 1/3] Add getLanguages support --- src/Locale/Locale.php | 10 ++++++++++ tests/Locale/LocaleTest.php | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Locale/Locale.php b/src/Locale/Locale.php index 86bdc83..66e34b8 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 + */ + static public function getLanguages(): array + { + return \array_keys(self::$language); + } + /** * Set New Locale from an array * diff --git a/tests/Locale/LocaleTest.php b/tests/Locale/LocaleTest.php index 4f79a60..2d4613f 100755 --- a/tests/Locale/LocaleTest.php +++ b/tests/Locale/LocaleTest.php @@ -28,9 +28,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 From 88f855c56445452a916f60282529f8c3d96a66b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 7 Oct 2022 07:22:14 +0000 Subject: [PATCH 2/3] Implement getTranslations --- src/Locale/Locale.php | 12 +++++++++++- tests/Locale/LocaleTest.php | 9 +++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Locale/Locale.php b/src/Locale/Locale.php index 66e34b8..4eeeb23 100644 --- a/src/Locale/Locale.php +++ b/src/Locale/Locale.php @@ -28,7 +28,7 @@ class Locale /** * Get list of configured languages * - * @return array + * @return array */ static public function getLanguages(): array { @@ -117,4 +117,14 @@ public function getText(string $key, $placeholders = []) return $translation; } + + /** + * Get list of configured transltions in specific language + * + * @return array + */ + public function getTranslations(): array + { + return self::$language[$this->default]; + } } \ No newline at end of file diff --git a/tests/Locale/LocaleTest.php b/tests/Locale/LocaleTest.php index 2d4613f..ee8e9e3 100755 --- a/tests/Locale/LocaleTest.php +++ b/tests/Locale/LocaleTest.php @@ -54,16 +54,24 @@ public function testTexts() $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'); @@ -86,6 +94,7 @@ public function testTexts() // Test exceptions $locale->setDefault('he-IL'); + Locale::$exceptions = true; try { From a0b4ae365858f510ddc8a76cae6d78a5e3ff1d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 12 Aug 2025 10:41:26 +0000 Subject: [PATCH 3/3] formatting, code QL fix --- composer.json | 2 +- src/Locale/Locale.php | 6 +++--- tests/Locale/LocaleTest.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) 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 41a3bd3..fc8ecce 100644 --- a/src/Locale/Locale.php +++ b/src/Locale/Locale.php @@ -28,9 +28,9 @@ class Locale /** * Get list of configured languages * - * @return array + * @return array */ - static public function getLanguages(): array + public static function getLanguages(): array { return \array_keys(self::$language); } @@ -123,7 +123,7 @@ public function getText(string $key, array $placeholders = []) /** * Get list of configured transltions in specific language * - * @return array + * @return array */ public function getTranslations(): array { diff --git a/tests/Locale/LocaleTest.php b/tests/Locale/LocaleTest.php index f42dde4..3b0b0d2 100755 --- a/tests/Locale/LocaleTest.php +++ b/tests/Locale/LocaleTest.php @@ -20,7 +20,7 @@ public function setUp(): void $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 @@ -45,7 +45,7 @@ public function testTexts(): void $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); + $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');