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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions src/Locale/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ class Locale
*/
public $default;

/**
* Get list of configured languages
*
* @return array<string>
*/
public static function getLanguages(): array
{
return \array_keys(self::$language);
}

/**
* Set New Locale from an array
*
Expand Down Expand Up @@ -109,4 +119,14 @@ public function getText(string $key, array $placeholders = [])

return $translation;
}

/**
* Get list of configured transltions in specific language
*
* @return array<string, string>
*/
public function getTranslations(): array
{
return self::$language[$this->default];
}
}
19 changes: 19 additions & 0 deletions tests/Locale/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');

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

// Test exceptions
$locale->setDefault('he-IL');

Locale::$exceptions = true;

try {
Expand Down