Browse Source

Fixes #1234: use dash instead of underscore to separate language and country in locale id.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
d2e5f2c507
  1. 2
      apps/advanced/backend/views/layouts/main.php
  2. 2
      apps/advanced/frontend/views/layouts/main.php
  3. 2
      apps/basic/views/layouts/main.php
  4. 2
      docs/guide/i18n.md
  5. 2
      docs/guide/view.md
  6. 2
      extensions/debug/panels/DbPanel.php
  7. 2
      framework/yii/BaseYii.php
  8. 4
      framework/yii/base/Application.php
  9. 8
      framework/yii/i18n/I18N.php
  10. 2
      framework/yii/i18n/MissingTranslationEvent.php
  11. 2
      framework/yii/messages/config.php
  12. 8
      framework/yii/web/Request.php
  13. 0
      tests/unit/data/i18n/messages/de-DE/test.php
  14. 0
      tests/unit/data/i18n/messages/en-US/test.php
  15. 6
      tests/unit/framework/i18n/FallbackMessageFormatterTest.php
  16. 2
      tests/unit/framework/i18n/FormatterTest.php
  17. 22
      tests/unit/framework/i18n/I18NTest.php
  18. 12
      tests/unit/framework/i18n/MessageFormatterTest.php

2
apps/advanced/backend/views/layouts/main.php

@ -13,7 +13,7 @@ AppAsset::register($this);
?> ?>
<?php $this->beginPage(); ?> <?php $this->beginPage(); ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="<?= Yii::$app->language ?>">
<head> <head>
<meta charset="<?= Yii::$app->charset ?>"/> <meta charset="<?= Yii::$app->charset ?>"/>
<title><?= Html::encode($this->title) ?></title> <title><?= Html::encode($this->title) ?></title>

2
apps/advanced/frontend/views/layouts/main.php

@ -14,7 +14,7 @@ AppAsset::register($this);
?> ?>
<?php $this->beginPage(); ?> <?php $this->beginPage(); ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="<?= Yii::$app->language ?>">
<head> <head>
<meta charset="<?= Yii::$app->charset ?>"/> <meta charset="<?= Yii::$app->charset ?>"/>
<title><?= Html::encode($this->title) ?></title> <title><?= Html::encode($this->title) ?></title>

2
apps/basic/views/layouts/main.php

@ -13,7 +13,7 @@ AppAsset::register($this);
?> ?>
<?php $this->beginPage(); ?> <?php $this->beginPage(); ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="<?= Yii::$app->language ?>">
<head> <head>
<meta charset="<?= Yii::$app->charset ?>"/> <meta charset="<?= Yii::$app->charset ?>"/>
<title><?= Html::encode($this->title) ?></title> <title><?= Html::encode($this->title) ?></title>

2
docs/guide/i18n.md

@ -57,7 +57,7 @@ Yii tries to load approprite translation from one of the message sources defined
'app*' => [ 'app*' => [
'class' => 'yii\i18n\PhpMessageSource', 'class' => 'yii\i18n\PhpMessageSource',
//'basePath' => '@app/messages', //'basePath' => '@app/messages',
//'sourceLanguage' => 'en_US', //'sourceLanguage' => 'en-US',
'fileMap' => [ 'fileMap' => [
'app' => 'app.php', 'app' => 'app.php',
'app/error' => 'error.php', 'app/error' => 'error.php',

2
docs/guide/view.md

@ -256,7 +256,7 @@ use yii\helpers\Html;
?> ?>
<?php $this->beginPage(); ?> <?php $this->beginPage(); ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="<?= Yii::$app->charset ?>"> <html lang="<?= Yii::$app->language ?>">
<head> <head>
<meta charset="<?= Yii::$app->charset ?>"/> <meta charset="<?= Yii::$app->charset ?>"/>
<title><?= Html::encode($this->title) ?></title> <title><?= Html::encode($this->title) ?></title>

2
extensions/debug/panels/DbPanel.php

@ -48,7 +48,7 @@ EOD;
public function getDetail() public function getDetail()
{ {
$timings = $this->calculateTimings(); $timings = $this->calculateTimings();
ArrayHelper::multisort($timings, 3, true); ArrayHelper::multisort($timings, 3, SORT_DESC);
$rows = []; $rows = [];
foreach ($timings as $timing) { foreach ($timings as $timing) {
$duration = sprintf('%.1f ms', $timing[3] * 1000); $duration = sprintf('%.1f ms', $timing[3] * 1000);

2
framework/yii/BaseYii.php

@ -498,7 +498,7 @@ class BaseYii
* @param string $category the message category. * @param string $category the message category.
* @param string $message the message to be translated. * @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 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. * [[\yii\base\Application::language|application language]] will be used.
* @return string the translated message. * @return string the translated message.
*/ */

4
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. * @var string the language that is meant to be used for end users.
* @see sourceLanguage * @see sourceLanguage
*/ */
public $language = 'en_US'; public $language = 'en-US';
/** /**
* @var string the language that the application is written in. This mainly refers to * @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. * the language that the messages and view files are written in.
* @see language * @see language
*/ */
public $sourceLanguage = 'en_US'; public $sourceLanguage = 'en-US';
/** /**
* @var Controller the currently active controller instance * @var Controller the currently active controller instance
*/ */

8
framework/yii/i18n/I18N.php

@ -53,14 +53,14 @@ class I18N extends Component
if (!isset($this->translations['yii'])) { if (!isset($this->translations['yii'])) {
$this->translations['yii'] = [ $this->translations['yii'] = [
'class' => 'yii\i18n\PhpMessageSource', 'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en_US', 'sourceLanguage' => 'en-US',
'basePath' => '@yii/messages', 'basePath' => '@yii/messages',
]; ];
} }
if (!isset($this->translations['app'])) { if (!isset($this->translations['app'])) {
$this->translations['app'] = [ $this->translations['app'] = [
'class' => 'yii\i18n\PhpMessageSource', 'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en_US', 'sourceLanguage' => 'en-US',
'basePath' => '@app/messages', 'basePath' => '@app/messages',
]; ];
} }
@ -75,7 +75,7 @@ class I18N extends Component
* @param string $category the message category. * @param string $category the message category.
* @param string $message the message to be translated. * @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 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. * @return string the translated and formatted message.
*/ */
public function translate($category, $message, $params, $language) public function translate($category, $message, $params, $language)
@ -89,7 +89,7 @@ class I18N extends Component
* *
* @param string $message the message to be formatted. * @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 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. * @return string the formatted message.
*/ */
public function format($message, $params, $language) public function format($message, $params, $language)

2
framework/yii/i18n/MissingTranslationEvent.php

@ -27,7 +27,7 @@ class MissingTranslationEvent extends Event
*/ */
public $category; 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; public $language;
} }

2
framework/yii/messages/config.php

@ -6,7 +6,7 @@ return [
// string, required, root directory containing message translations. // string, required, root directory containing message translations.
'messagePath' => __DIR__, 'messagePath' => __DIR__,
// array, required, list of language codes that the extracted messages // 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'], 'languages' => ['de'],
// string, the name of the function for translating messages. // 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 // Defaults to 'Yii::t'. This is used as a mark to find the messages to be

8
framework/yii/web/Request.php

@ -908,11 +908,11 @@ class Request extends \yii\base\Request
return isset($acceptedLanguages[0]) ? $acceptedLanguages[0] : null; return isset($acceptedLanguages[0]) ? $acceptedLanguages[0] : null;
} }
foreach ($acceptedLanguages as $acceptedLanguage) { foreach ($acceptedLanguages as $acceptedLanguage) {
$acceptedLanguage = str_replace('-', '_', strtolower($acceptedLanguage)); $acceptedLanguage = str_replace('_', '-', strtolower($acceptedLanguage));
foreach ($languages as $language) { foreach ($languages as $language) {
$language = str_replace('-', '_', strtolower($language)); $language = str_replace('_', '-', strtolower($language));
// en_us==en_us, en==en_us, en_us==en // en-us==en-us, en==en-us, en-us==en
if ($language === $acceptedLanguage || strpos($acceptedLanguage, $language . '_') === 0 || strpos($language, $acceptedLanguage . '_') === 0) { if ($language === $acceptedLanguage || strpos($acceptedLanguage, $language . '-') === 0 || strpos($language, $acceptedLanguage . '-') === 0) {
return $language; return $language;
} }
} }

0
tests/unit/data/i18n/messages/de_DE/test.php → tests/unit/data/i18n/messages/de-DE/test.php

0
tests/unit/data/i18n/messages/en_US/test.php → tests/unit/data/i18n/messages/en-US/test.php

6
tests/unit/framework/i18n/FallbackMessageFormatterTest.php

@ -136,7 +136,7 @@ _MSG_
public function testNamedArguments($pattern, $expected, $args) public function testNamedArguments($pattern, $expected, $args)
{ {
$formatter = new FallbackMessageFormatter(); $formatter = new FallbackMessageFormatter();
$result = $formatter->fallbackFormat($pattern, $args, 'en_US'); $result = $formatter->fallbackFormat($pattern, $args, 'en-US');
$this->assertEquals($expected, $result, $formatter->getErrorMessage()); $this->assertEquals($expected, $result, $formatter->getErrorMessage());
} }
@ -147,7 +147,7 @@ _MSG_
$formatter = new FallbackMessageFormatter(); $formatter = new FallbackMessageFormatter();
$result = $formatter->fallbackFormat('{'.self::SUBJECT.'} is {'.self::N.'}', [ $result = $formatter->fallbackFormat('{'.self::SUBJECT.'} is {'.self::N.'}', [
self::N => self::N_VALUE, self::N => self::N_VALUE,
], 'en_US'); ], 'en-US');
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
@ -157,7 +157,7 @@ _MSG_
$pattern = '{'.self::SUBJECT.'} is '.self::N; $pattern = '{'.self::SUBJECT.'} is '.self::N;
$formatter = new FallbackMessageFormatter(); $formatter = new FallbackMessageFormatter();
$result = $formatter->fallbackFormat($pattern, [], 'en_US'); $result = $formatter->fallbackFormat($pattern, [], 'en-US');
$this->assertEquals($pattern, $result, $formatter->getErrorMessage()); $this->assertEquals($pattern, $result, $formatter->getErrorMessage());
} }
} }

2
tests/unit/framework/i18n/FormatterTest.php

@ -29,7 +29,7 @@ class FormatterTest extends TestCase
$this->markTestSkipped('intl extension is required.'); $this->markTestSkipped('intl extension is required.');
} }
$this->mockApplication(); $this->mockApplication();
$this->formatter = new Formatter(['locale' => 'en_US']); $this->formatter = new Formatter(['locale' => 'en-US']);
} }
protected function tearDown() protected function tearDown()

22
tests/unit/framework/i18n/I18NTest.php

@ -40,16 +40,16 @@ class I18NTest extends TestCase
public function testTranslate() public function testTranslate()
{ {
$msg = 'The dog runs fast.'; $msg = 'The dog runs fast.';
$this->assertEquals('The dog runs fast.', $this->i18n->translate('test', $msg, [], 'en_US')); $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('Der Hund rennt schnell.', $this->i18n->translate('test', $msg, [], 'de-DE'));
} }
public function testTranslateParams() public function testTranslateParams()
{ {
$msg = 'His speed is about {n} km/h.'; $msg = 'His speed is about {n} km/h.';
$params = ['n' => 42]; $params = ['n' => 42];
$this->assertEquals('His speed is about 42 km/h.', $this->i18n->translate('test', $msg, $params, 'en_US')); $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('Seine Geschwindigkeit beträgt 42 km/h.', $this->i18n->translate('test', $msg, $params, 'de-DE'));
} }
public function testTranslateParams2() public function testTranslateParams2()
@ -62,22 +62,22 @@ class I18NTest extends TestCase
'n' => 42, 'n' => 42,
'name' => 'DA VINCI', // http://petrix.com/dognames/d.html '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('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('Er heißt DA VINCI und ist 42 km/h schnell.', $this->i18n->translate('test', $msg, $params, 'de-DE'));
} }
public function testSpecialParams() public function testSpecialParams()
{ {
$msg = 'His speed is about {0} km/h.'; $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 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 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, 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, [], 'en-US'));
$msg = 'His name is {name} and he is {age} years old.'; $msg = 'His name is {name} and he is {age} years old.';
$model = new ParamModel(); $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'));
} }
} }

12
tests/unit/framework/i18n/MessageFormatterTest.php

@ -250,7 +250,7 @@ _MSG_
1 => 123, 1 => 123,
2 => 37.073 2 => 37.073
], ],
'en_US' 'en-US'
], ],
[ [
@ -272,7 +272,7 @@ _MSG_
'trees' => 123, 'trees' => 123,
'monkeysPerTree' => 37.073 'monkeysPerTree' => 37.073
], ],
'en_US' 'en-US'
], ],
[ [
@ -297,14 +297,14 @@ _MSG_
$this->markTestSkipped($skipMessage); $this->markTestSkipped($skipMessage);
} }
$formatter = new MessageFormatter(); $formatter = new MessageFormatter();
$result = $formatter->format($pattern, $args, 'en_US'); $result = $formatter->format($pattern, $args, 'en-US');
$this->assertEquals($expected, $result, $formatter->getErrorMessage()); $this->assertEquals($expected, $result, $formatter->getErrorMessage());
} }
/** /**
* @dataProvider parsePatterns * @dataProvider parsePatterns
*/ */
public function testParseNamedArguments($pattern, $expected, $args, $locale = 'en_US') public function testParseNamedArguments($pattern, $expected, $args, $locale = 'en-US')
{ {
if (!extension_loaded("intl")) { if (!extension_loaded("intl")) {
$this->markTestSkipped("intl not installed. Skipping."); $this->markTestSkipped("intl not installed. Skipping.");
@ -322,7 +322,7 @@ _MSG_
$formatter = new MessageFormatter(); $formatter = new MessageFormatter();
$result = $formatter->format('{'.self::SUBJECT.'} is {'.self::N.', number}', [ $result = $formatter->format('{'.self::SUBJECT.'} is {'.self::N.', number}', [
self::N => self::N_VALUE, self::N => self::N_VALUE,
], 'en_US'); ], 'en-US');
$this->assertEquals($expected, $result, $formatter->getErrorMessage()); $this->assertEquals($expected, $result, $formatter->getErrorMessage());
} }
@ -331,7 +331,7 @@ _MSG_
{ {
$pattern = '{'.self::SUBJECT.'} is '.self::N; $pattern = '{'.self::SUBJECT.'} is '.self::N;
$formatter = new MessageFormatter(); $formatter = new MessageFormatter();
$result = $formatter->format($pattern, [], 'en_US'); $result = $formatter->format($pattern, [], 'en-US');
$this->assertEquals($pattern, $result, $formatter->getErrorMessage()); $this->assertEquals($pattern, $result, $formatter->getErrorMessage());
} }
} }
Loading…
Cancel
Save