diff --git a/CHANGELOG.md b/CHANGELOG.md index e960d20..230827e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/params.php b/config/params.php index 83a3e71..73fe652 100644 --- a/config/params.php +++ b/config/params.php @@ -4,7 +4,7 @@ return [ 'yiisoft/translator' => [ - 'locale' => 'en_US', + 'locale' => 'en-US', 'fallbackLocale' => null, 'defaultCategory' => 'app', ], diff --git a/src/SimpleMessageFormatter.php b/src/SimpleMessageFormatter.php index 411c436..36d1d93 100644 --- a/src/SimpleMessageFormatter.php +++ b/src/SimpleMessageFormatter.php @@ -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 = []; diff --git a/src/Translator.php b/src/Translator.php index f75a073..a0451e8 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -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, diff --git a/tests/CategoryTest.php b/tests/CategoryTest.php index eacd35c..868d64e 100644 --- a/tests/CategoryTest.php +++ b/tests/CategoryTest.php @@ -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) ); } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 086ecc6..c18bafd 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -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 diff --git a/tests/IdMessageReaderTest.php b/tests/IdMessageReaderTest.php index 0bf32ce..3370c76 100644 --- a/tests/IdMessageReaderTest.php +++ b/tests/IdMessageReaderTest.php @@ -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 @@ -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'); } } diff --git a/tests/TranslatorTest.php b/tests/TranslatorTest.php index 2409a9f..1b0c949 100644 --- a/tests/TranslatorTest.php +++ b/tests/TranslatorTest.php @@ -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