Skip to content

Commit

Permalink
Fix #110: Changed default value for locale to BCP-47 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdef authored Jan 7, 2023
1 parent ca4e597 commit a9b034c
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Enh #99: In `SimpleMessageFormatter` add support of messages where used parameters with plural modifier that contain
non-supported keys (@vjik)
- Bug #110: Changed default value for locale to BCP-47 compatible (@darkdef)

## 2.2.0 November 28, 2022

Expand Down
2 changes: 1 addition & 1 deletion config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

return [
'yiisoft/translator' => [
'locale' => 'en_US',
'locale' => 'en-US',
'fallbackLocale' => null,
'defaultCategory' => 'app',
],
Expand Down
2 changes: 1 addition & 1 deletion src/SimpleMessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SimpleMessageFormatter implements MessageFormatterInterface
private const PLURAL_OTHER = 'other';
private const PLURAL_KEYS = [self::PLURAL_ONE, self::PLURAL_OTHER];

public function format(string $message, array $parameters, string $locale = 'en_US'): string
public function format(string $message, array $parameters, string $locale = 'en-US'): string
{
preg_match_all('/{((?>[^{}]+)|(?R))*}/', $message, $matches);
$replacements = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class Translator implements TranslatorInterface
* @param EventDispatcherInterface|null $eventDispatcher Event dispatcher for translation events. Null for none.
*/
public function __construct(
private string $locale = 'en_US',
private string $locale = 'en-US',
private ?string $fallbackLocale = null,
private string $defaultCategory = 'app',
private ?EventDispatcherInterface $eventDispatcher = null,
Expand Down
2 changes: 1 addition & 1 deletion tests/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function testWithoutFormatter(
$categorySource = new CategorySource('test', $this->createMessageReader());
$this->assertSame(
$expectedMessage,
$categorySource->format($message, $parameters, 'en_US', $defaultMessageFormatter)
$categorySource->format($message, $parameters, 'en-US', $defaultMessageFormatter)
);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public function testReset(): void
$container = $this->createContainer();

$translator = $container->get(TranslatorInterface::class);
$translator->setLocale('ru_RU');
$translator->setLocale('ru-RU');

$container->get(StateResetter::class)->reset();

$this->assertSame('en_US', $translator->getLocale());
$this->assertSame('en-US', $translator->getLocale());
}

private function createContainer(?array $params = null, bool $withCategorySources = false): Container
Expand Down
4 changes: 2 additions & 2 deletions tests/IdMessageReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function testGetMessage(): void
{
$reader = new IdMessageReader();

$this->assertSame('test', $reader->getMessage('test', 'my-module', 'en_US'));
$this->assertSame('test', $reader->getMessage('test', 'my-module', 'en-US'));
}

public function testGetMessages(): void
Expand All @@ -23,6 +23,6 @@ public function testGetMessages(): void

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('IdMessageReader doesn\'t support getting all messages at once');
$reader->getMessages('my-module', 'en_US');
$reader->getMessages('my-module', 'en-US');
}
}
13 changes: 12 additions & 1 deletion tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ final class TranslatorTest extends TestCase
public function testDefaultLocale(): void
{
$translator = new Translator();
$this->assertSame('en_US', $translator->getLocale());
$this->assertSame('en-US', $translator->getLocale());
$this->assertSame('test', $translator->translate('test'));
}

public function testDefaultLocaleWithCategorySource(): void
{
$translator = new Translator();
$this->assertSame('en-US', $translator->getLocale());

$categorySource = $this->createCategory('app', []);
$translator->addCategorySources($categorySource);
$this->assertSame('test', $translator->translate('test'));
}

private function getMessages(): array
Expand Down

0 comments on commit a9b034c

Please sign in to comment.