From d2e5f2c5074500e22f7845420b168301e7459e6f Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 19 Nov 2013 16:33:52 -0500 Subject: [PATCH] Fixes #1234: use dash instead of underscore to separate language and country in locale id. --- apps/advanced/backend/views/layouts/main.php | 2 +- apps/advanced/frontend/views/layouts/main.php | 2 +- apps/basic/views/layouts/main.php | 2 +- docs/guide/i18n.md | 2 +- docs/guide/view.md | 2 +- extensions/debug/panels/DbPanel.php | 2 +- framework/yii/BaseYii.php | 2 +- framework/yii/base/Application.php | 4 ++-- framework/yii/i18n/I18N.php | 8 ++++---- framework/yii/i18n/MissingTranslationEvent.php | 2 +- framework/yii/messages/config.php | 2 +- framework/yii/web/Request.php | 8 ++++---- tests/unit/data/i18n/messages/de-DE/test.php | 9 ++++++++ tests/unit/data/i18n/messages/de_DE/test.php | 9 -------- tests/unit/data/i18n/messages/en-US/test.php | 7 +++++++ tests/unit/data/i18n/messages/en_US/test.php | 7 ------- .../i18n/FallbackMessageFormatterTest.php | 8 ++++---- tests/unit/framework/i18n/FormatterTest.php | 2 +- tests/unit/framework/i18n/I18NTest.php | 24 +++++++++++----------- tests/unit/framework/i18n/MessageFormatterTest.php | 14 ++++++------- 20 files changed, 59 insertions(+), 59 deletions(-) create mode 100644 tests/unit/data/i18n/messages/de-DE/test.php delete mode 100644 tests/unit/data/i18n/messages/de_DE/test.php create mode 100644 tests/unit/data/i18n/messages/en-US/test.php delete mode 100644 tests/unit/data/i18n/messages/en_US/test.php diff --git a/apps/advanced/backend/views/layouts/main.php b/apps/advanced/backend/views/layouts/main.php index 9f0280d..fdffc26 100644 --- a/apps/advanced/backend/views/layouts/main.php +++ b/apps/advanced/backend/views/layouts/main.php @@ -13,7 +13,7 @@ AppAsset::register($this); ?> beginPage(); ?> - + <?= Html::encode($this->title) ?> diff --git a/apps/advanced/frontend/views/layouts/main.php b/apps/advanced/frontend/views/layouts/main.php index 7b2ce6f..febcc5a 100644 --- a/apps/advanced/frontend/views/layouts/main.php +++ b/apps/advanced/frontend/views/layouts/main.php @@ -14,7 +14,7 @@ AppAsset::register($this); ?> beginPage(); ?> - + <?= Html::encode($this->title) ?> diff --git a/apps/basic/views/layouts/main.php b/apps/basic/views/layouts/main.php index 8489e9c..04acdfb 100644 --- a/apps/basic/views/layouts/main.php +++ b/apps/basic/views/layouts/main.php @@ -13,7 +13,7 @@ AppAsset::register($this); ?> beginPage(); ?> - + <?= Html::encode($this->title) ?> diff --git a/docs/guide/i18n.md b/docs/guide/i18n.md index 6524801..477de0a 100644 --- a/docs/guide/i18n.md +++ b/docs/guide/i18n.md @@ -57,7 +57,7 @@ Yii tries to load approprite translation from one of the message sources defined 'app*' => [ 'class' => 'yii\i18n\PhpMessageSource', //'basePath' => '@app/messages', - //'sourceLanguage' => 'en_US', + //'sourceLanguage' => 'en-US', 'fileMap' => [ 'app' => 'app.php', 'app/error' => 'error.php', diff --git a/docs/guide/view.md b/docs/guide/view.md index 5951a30..ae86809 100644 --- a/docs/guide/view.md +++ b/docs/guide/view.md @@ -256,7 +256,7 @@ use yii\helpers\Html; ?> beginPage(); ?> - + <?= Html::encode($this->title) ?> diff --git a/extensions/debug/panels/DbPanel.php b/extensions/debug/panels/DbPanel.php index e24da93..a487dd4 100644 --- a/extensions/debug/panels/DbPanel.php +++ b/extensions/debug/panels/DbPanel.php @@ -48,7 +48,7 @@ EOD; public function getDetail() { $timings = $this->calculateTimings(); - ArrayHelper::multisort($timings, 3, true); + ArrayHelper::multisort($timings, 3, SORT_DESC); $rows = []; foreach ($timings as $timing) { $duration = sprintf('%.1f ms', $timing[3] * 1000); diff --git a/framework/yii/BaseYii.php b/framework/yii/BaseYii.php index 357a1e7..f11df7f 100644 --- a/framework/yii/BaseYii.php +++ b/framework/yii/BaseYii.php @@ -498,7 +498,7 @@ class BaseYii * @param string $category the message category. * @param string $message the message to be translated. * @param array $params the parameters that will be used to replace the corresponding placeholders in the message. - * @param string $language the language code (e.g. `en_US`, `en`). If this is null, the current + * @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current * [[\yii\base\Application::language|application language]] will be used. * @return string the translated message. */ diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index 0af586c..dc363f1 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -79,13 +79,13 @@ abstract class Application extends Module * @var string the language that is meant to be used for end users. * @see sourceLanguage */ - public $language = 'en_US'; + public $language = 'en-US'; /** * @var string the language that the application is written in. This mainly refers to * the language that the messages and view files are written in. * @see language */ - public $sourceLanguage = 'en_US'; + public $sourceLanguage = 'en-US'; /** * @var Controller the currently active controller instance */ diff --git a/framework/yii/i18n/I18N.php b/framework/yii/i18n/I18N.php index 5575621..c59a6d2 100644 --- a/framework/yii/i18n/I18N.php +++ b/framework/yii/i18n/I18N.php @@ -53,14 +53,14 @@ class I18N extends Component if (!isset($this->translations['yii'])) { $this->translations['yii'] = [ 'class' => 'yii\i18n\PhpMessageSource', - 'sourceLanguage' => 'en_US', + 'sourceLanguage' => 'en-US', 'basePath' => '@yii/messages', ]; } if (!isset($this->translations['app'])) { $this->translations['app'] = [ 'class' => 'yii\i18n\PhpMessageSource', - 'sourceLanguage' => 'en_US', + 'sourceLanguage' => 'en-US', 'basePath' => '@app/messages', ]; } @@ -75,7 +75,7 @@ class I18N extends Component * @param string $category the message category. * @param string $message the message to be translated. * @param array $params the parameters that will be used to replace the corresponding placeholders in the message. - * @param string $language the language code (e.g. `en_US`, `en`). + * @param string $language the language code (e.g. `en-US`, `en`). * @return string the translated and formatted message. */ public function translate($category, $message, $params, $language) @@ -89,7 +89,7 @@ class I18N extends Component * * @param string $message the message to be formatted. * @param array $params the parameters that will be used to replace the corresponding placeholders in the message. - * @param string $language the language code (e.g. `en_US`, `en`). + * @param string $language the language code (e.g. `en-US`, `en`). * @return string the formatted message. */ public function format($message, $params, $language) diff --git a/framework/yii/i18n/MissingTranslationEvent.php b/framework/yii/i18n/MissingTranslationEvent.php index 9ac337a..5c8ffd3 100644 --- a/framework/yii/i18n/MissingTranslationEvent.php +++ b/framework/yii/i18n/MissingTranslationEvent.php @@ -27,7 +27,7 @@ class MissingTranslationEvent extends Event */ public $category; /** - * @var string the language ID (e.g. en_US) that the message is to be translated to + * @var string the language ID (e.g. en-US) that the message is to be translated to */ public $language; } diff --git a/framework/yii/messages/config.php b/framework/yii/messages/config.php index 0f724ba..b707abb 100644 --- a/framework/yii/messages/config.php +++ b/framework/yii/messages/config.php @@ -6,7 +6,7 @@ return [ // string, required, root directory containing message translations. 'messagePath' => __DIR__, // array, required, list of language codes that the extracted messages - // should be translated to. For example, ['zh_cn', 'de']. + // should be translated to. For example, ['zh-CN', 'de']. 'languages' => ['de'], // string, the name of the function for translating messages. // Defaults to 'Yii::t'. This is used as a mark to find the messages to be diff --git a/framework/yii/web/Request.php b/framework/yii/web/Request.php index 0b49730..7a7cedf 100644 --- a/framework/yii/web/Request.php +++ b/framework/yii/web/Request.php @@ -908,11 +908,11 @@ class Request extends \yii\base\Request return isset($acceptedLanguages[0]) ? $acceptedLanguages[0] : null; } foreach ($acceptedLanguages as $acceptedLanguage) { - $acceptedLanguage = str_replace('-', '_', strtolower($acceptedLanguage)); + $acceptedLanguage = str_replace('_', '-', strtolower($acceptedLanguage)); foreach ($languages as $language) { - $language = str_replace('-', '_', strtolower($language)); - // en_us==en_us, en==en_us, en_us==en - if ($language === $acceptedLanguage || strpos($acceptedLanguage, $language . '_') === 0 || strpos($language, $acceptedLanguage . '_') === 0) { + $language = str_replace('_', '-', strtolower($language)); + // en-us==en-us, en==en-us, en-us==en + if ($language === $acceptedLanguage || strpos($acceptedLanguage, $language . '-') === 0 || strpos($language, $acceptedLanguage . '-') === 0) { return $language; } } diff --git a/tests/unit/data/i18n/messages/de-DE/test.php b/tests/unit/data/i18n/messages/de-DE/test.php new file mode 100644 index 0000000..d8c5290 --- /dev/null +++ b/tests/unit/data/i18n/messages/de-DE/test.php @@ -0,0 +1,9 @@ + 'Der Hund rennt schnell.', + 'His speed is about {n} km/h.' => 'Seine Geschwindigkeit beträgt {n} km/h.', + 'His name is {name} and his speed is about {n, number} km/h.' => 'Er heißt {name} und ist {n, number} km/h schnell.', +]; \ No newline at end of file diff --git a/tests/unit/data/i18n/messages/de_DE/test.php b/tests/unit/data/i18n/messages/de_DE/test.php deleted file mode 100644 index d8c5290..0000000 --- a/tests/unit/data/i18n/messages/de_DE/test.php +++ /dev/null @@ -1,9 +0,0 @@ - 'Der Hund rennt schnell.', - 'His speed is about {n} km/h.' => 'Seine Geschwindigkeit beträgt {n} km/h.', - 'His name is {name} and his speed is about {n, number} km/h.' => 'Er heißt {name} und ist {n, number} km/h schnell.', -]; \ No newline at end of file diff --git a/tests/unit/data/i18n/messages/en-US/test.php b/tests/unit/data/i18n/messages/en-US/test.php new file mode 100644 index 0000000..83342b0 --- /dev/null +++ b/tests/unit/data/i18n/messages/en-US/test.php @@ -0,0 +1,7 @@ + 'Der Hund rennt schell.', +]; \ No newline at end of file diff --git a/tests/unit/data/i18n/messages/en_US/test.php b/tests/unit/data/i18n/messages/en_US/test.php deleted file mode 100644 index 83342b0..0000000 --- a/tests/unit/data/i18n/messages/en_US/test.php +++ /dev/null @@ -1,7 +0,0 @@ - 'Der Hund rennt schell.', -]; \ No newline at end of file diff --git a/tests/unit/framework/i18n/FallbackMessageFormatterTest.php b/tests/unit/framework/i18n/FallbackMessageFormatterTest.php index 6aa8a22..bdc4e43 100644 --- a/tests/unit/framework/i18n/FallbackMessageFormatterTest.php +++ b/tests/unit/framework/i18n/FallbackMessageFormatterTest.php @@ -136,7 +136,7 @@ _MSG_ public function testNamedArguments($pattern, $expected, $args) { $formatter = new FallbackMessageFormatter(); - $result = $formatter->fallbackFormat($pattern, $args, 'en_US'); + $result = $formatter->fallbackFormat($pattern, $args, 'en-US'); $this->assertEquals($expected, $result, $formatter->getErrorMessage()); } @@ -147,7 +147,7 @@ _MSG_ $formatter = new FallbackMessageFormatter(); $result = $formatter->fallbackFormat('{'.self::SUBJECT.'} is {'.self::N.'}', [ self::N => self::N_VALUE, - ], 'en_US'); + ], 'en-US'); $this->assertEquals($expected, $result); } @@ -157,7 +157,7 @@ _MSG_ $pattern = '{'.self::SUBJECT.'} is '.self::N; $formatter = new FallbackMessageFormatter(); - $result = $formatter->fallbackFormat($pattern, [], 'en_US'); + $result = $formatter->fallbackFormat($pattern, [], 'en-US'); $this->assertEquals($pattern, $result, $formatter->getErrorMessage()); } } @@ -168,4 +168,4 @@ class FallbackMessageFormatter extends MessageFormatter { return parent::fallbackFormat($pattern, $args, $locale); } -} \ No newline at end of file +} diff --git a/tests/unit/framework/i18n/FormatterTest.php b/tests/unit/framework/i18n/FormatterTest.php index 622c308..ed5ab33 100644 --- a/tests/unit/framework/i18n/FormatterTest.php +++ b/tests/unit/framework/i18n/FormatterTest.php @@ -29,7 +29,7 @@ class FormatterTest extends TestCase $this->markTestSkipped('intl extension is required.'); } $this->mockApplication(); - $this->formatter = new Formatter(['locale' => 'en_US']); + $this->formatter = new Formatter(['locale' => 'en-US']); } protected function tearDown() diff --git a/tests/unit/framework/i18n/I18NTest.php b/tests/unit/framework/i18n/I18NTest.php index 65d3d11..aa2356b 100644 --- a/tests/unit/framework/i18n/I18NTest.php +++ b/tests/unit/framework/i18n/I18NTest.php @@ -40,16 +40,16 @@ class I18NTest extends TestCase public function testTranslate() { $msg = 'The dog runs fast.'; - $this->assertEquals('The dog runs fast.', $this->i18n->translate('test', $msg, [], 'en_US')); - $this->assertEquals('Der Hund rennt schnell.', $this->i18n->translate('test', $msg, [], 'de_DE')); + $this->assertEquals('The dog runs fast.', $this->i18n->translate('test', $msg, [], 'en-US')); + $this->assertEquals('Der Hund rennt schnell.', $this->i18n->translate('test', $msg, [], 'de-DE')); } public function testTranslateParams() { $msg = 'His speed is about {n} km/h.'; $params = ['n' => 42]; - $this->assertEquals('His speed is about 42 km/h.', $this->i18n->translate('test', $msg, $params, 'en_US')); - $this->assertEquals('Seine Geschwindigkeit beträgt 42 km/h.', $this->i18n->translate('test', $msg, $params, 'de_DE')); + $this->assertEquals('His speed is about 42 km/h.', $this->i18n->translate('test', $msg, $params, 'en-US')); + $this->assertEquals('Seine Geschwindigkeit beträgt 42 km/h.', $this->i18n->translate('test', $msg, $params, 'de-DE')); } public function testTranslateParams2() @@ -62,22 +62,22 @@ class I18NTest extends TestCase 'n' => 42, 'name' => 'DA VINCI', // http://petrix.com/dognames/d.html ]; - $this->assertEquals('His name is DA VINCI and his speed is about 42 km/h.', $this->i18n->translate('test', $msg, $params, 'en_US')); - $this->assertEquals('Er heißt DA VINCI und ist 42 km/h schnell.', $this->i18n->translate('test', $msg, $params, 'de_DE')); + $this->assertEquals('His name is DA VINCI and his speed is about 42 km/h.', $this->i18n->translate('test', $msg, $params, 'en-US')); + $this->assertEquals('Er heißt DA VINCI und ist 42 km/h schnell.', $this->i18n->translate('test', $msg, $params, 'de-DE')); } public function testSpecialParams() { $msg = 'His speed is about {0} km/h.'; - $this->assertEquals('His speed is about 0 km/h.', $this->i18n->translate('test', $msg, 0, 'en_US')); - $this->assertEquals('His speed is about 42 km/h.', $this->i18n->translate('test', $msg, 42, 'en_US')); - $this->assertEquals('His speed is about {0} km/h.', $this->i18n->translate('test', $msg, null, 'en_US')); - $this->assertEquals('His speed is about {0} km/h.', $this->i18n->translate('test', $msg, [], 'en_US')); + $this->assertEquals('His speed is about 0 km/h.', $this->i18n->translate('test', $msg, 0, 'en-US')); + $this->assertEquals('His speed is about 42 km/h.', $this->i18n->translate('test', $msg, 42, 'en-US')); + $this->assertEquals('His speed is about {0} km/h.', $this->i18n->translate('test', $msg, null, 'en-US')); + $this->assertEquals('His speed is about {0} km/h.', $this->i18n->translate('test', $msg, [], 'en-US')); $msg = 'His name is {name} and he is {age} years old.'; $model = new ParamModel(); - $this->assertEquals('His name is peer and he is 5 years old.', $this->i18n->translate('test', $msg, $model, 'en_US')); + $this->assertEquals('His name is peer and he is 5 years old.', $this->i18n->translate('test', $msg, $model, 'en-US')); } } @@ -85,4 +85,4 @@ class ParamModel extends Model { public $name = 'peer'; public $age = 5; -} \ No newline at end of file +} diff --git a/tests/unit/framework/i18n/MessageFormatterTest.php b/tests/unit/framework/i18n/MessageFormatterTest.php index 7bc8047..1d5d007 100644 --- a/tests/unit/framework/i18n/MessageFormatterTest.php +++ b/tests/unit/framework/i18n/MessageFormatterTest.php @@ -250,7 +250,7 @@ _MSG_ 1 => 123, 2 => 37.073 ], - 'en_US' + 'en-US' ], [ @@ -272,7 +272,7 @@ _MSG_ 'trees' => 123, 'monkeysPerTree' => 37.073 ], - 'en_US' + 'en-US' ], [ @@ -297,14 +297,14 @@ _MSG_ $this->markTestSkipped($skipMessage); } $formatter = new MessageFormatter(); - $result = $formatter->format($pattern, $args, 'en_US'); + $result = $formatter->format($pattern, $args, 'en-US'); $this->assertEquals($expected, $result, $formatter->getErrorMessage()); } /** * @dataProvider parsePatterns */ - public function testParseNamedArguments($pattern, $expected, $args, $locale = 'en_US') + public function testParseNamedArguments($pattern, $expected, $args, $locale = 'en-US') { if (!extension_loaded("intl")) { $this->markTestSkipped("intl not installed. Skipping."); @@ -322,7 +322,7 @@ _MSG_ $formatter = new MessageFormatter(); $result = $formatter->format('{'.self::SUBJECT.'} is {'.self::N.', number}', [ self::N => self::N_VALUE, - ], 'en_US'); + ], 'en-US'); $this->assertEquals($expected, $result, $formatter->getErrorMessage()); } @@ -331,7 +331,7 @@ _MSG_ { $pattern = '{'.self::SUBJECT.'} is '.self::N; $formatter = new MessageFormatter(); - $result = $formatter->format($pattern, [], 'en_US'); + $result = $formatter->format($pattern, [], 'en-US'); $this->assertEquals($pattern, $result, $formatter->getErrorMessage()); } -} \ No newline at end of file +}