Commit d2e5f2c5 by Qiang Xue

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

parent 53457a0a
...@@ -13,7 +13,7 @@ AppAsset::register($this); ...@@ -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>
......
...@@ -14,7 +14,7 @@ AppAsset::register($this); ...@@ -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>
......
...@@ -13,7 +13,7 @@ AppAsset::register($this); ...@@ -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>
......
...@@ -57,7 +57,7 @@ Yii tries to load approprite translation from one of the message sources defined ...@@ -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',
......
...@@ -256,7 +256,7 @@ use yii\helpers\Html; ...@@ -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>
......
...@@ -48,7 +48,7 @@ EOD; ...@@ -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);
......
...@@ -498,7 +498,7 @@ class BaseYii ...@@ -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.
*/ */
......
...@@ -79,13 +79,13 @@ abstract class Application extends Module ...@@ -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
*/ */
......
...@@ -53,14 +53,14 @@ class I18N extends Component ...@@ -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 ...@@ -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 ...@@ -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)
......
...@@ -27,7 +27,7 @@ class MissingTranslationEvent extends Event ...@@ -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;
} }
...@@ -6,7 +6,7 @@ return [ ...@@ -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
......
...@@ -908,11 +908,11 @@ class Request extends \yii\base\Request ...@@ -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;
} }
} }
......
...@@ -136,7 +136,7 @@ _MSG_ ...@@ -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_ ...@@ -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_ ...@@ -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());
} }
} }
......
...@@ -29,7 +29,7 @@ class FormatterTest extends TestCase ...@@ -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()
......
...@@ -40,16 +40,16 @@ class I18NTest extends TestCase ...@@ -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 ...@@ -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'));
} }
} }
......
...@@ -250,7 +250,7 @@ _MSG_ ...@@ -250,7 +250,7 @@ _MSG_
1 => 123, 1 => 123,
2 => 37.073 2 => 37.073
], ],
'en_US' 'en-US'
], ],
[ [
...@@ -272,7 +272,7 @@ _MSG_ ...@@ -272,7 +272,7 @@ _MSG_
'trees' => 123, 'trees' => 123,
'monkeysPerTree' => 37.073 'monkeysPerTree' => 37.073
], ],
'en_US' 'en-US'
], ],
[ [
...@@ -297,14 +297,14 @@ _MSG_ ...@@ -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_ ...@@ -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_ ...@@ -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());
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment