Commit 89fe3549 by Qiang Xue

Merge pull request #2285 from yiisoft/missing-translation-formatting

When translation is missing source language should be used for formatting
parents 928df009 0f59ba2d
...@@ -40,6 +40,7 @@ Yii Framework 2 Change Log ...@@ -40,6 +40,7 @@ Yii Framework 2 Change Log
- Bug #2084: AssetController adjusting CSS URLs declared at same line fixed (klimov-paul) - Bug #2084: AssetController adjusting CSS URLs declared at same line fixed (klimov-paul)
- Bug #2091: `QueryBuilder::buildInCondition()` fails to handle array not starting with index 0 (qiangxue) - Bug #2091: `QueryBuilder::buildInCondition()` fails to handle array not starting with index 0 (qiangxue)
- Bug #2160: SphinxQL does not support OFFSET (qiangxue, romeo7) - Bug #2160: SphinxQL does not support OFFSET (qiangxue, romeo7)
- Bug #2209: When I18N message translation is missing source language is now used for formatting (samdark)
- Bug #2212: `yii\gridview\DataColumn` generates incorrect labels when used with nosql DB and there is no data (qiangxue) - Bug #2212: `yii\gridview\DataColumn` generates incorrect labels when used with nosql DB and there is no data (qiangxue)
- Bug #2298: Fixed the bug that Gii controller generator did not allow digit in the controller ID (qiangxue) - Bug #2298: Fixed the bug that Gii controller generator did not allow digit in the controller ID (qiangxue)
- Bug #2303: Fixed the bug that `yii\base\Theme::pathMap` did not support dynamic update with path aliases (qiangxue) - Bug #2303: Fixed the bug that `yii\base\Theme::pathMap` did not support dynamic update with path aliases (qiangxue)
......
...@@ -85,8 +85,13 @@ class I18N extends Component ...@@ -85,8 +85,13 @@ class I18N extends Component
*/ */
public function translate($category, $message, $params, $language) public function translate($category, $message, $params, $language)
{ {
$message = $this->getMessageSource($category)->translate($category, $message, $language); $messageSource = $this->getMessageSource($category);
return $this->format($message, $params, $language); $translation = $messageSource->translate($category, $message, $language);
if ($translation === false) {
return $this->format($message, $params, $messageSource->sourceLanguage);
} else {
return $this->format($translation, $params, $language);
}
} }
/** /**
......
...@@ -78,14 +78,14 @@ class MessageSource extends Component ...@@ -78,14 +78,14 @@ class MessageSource 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 string $language the target language * @param string $language the target language
* @return string the translated message (or the original message if translation is not needed) * @return string|boolean the translated message or false if translation wasn't found or isn't required
*/ */
public function translate($category, $message, $language) public function translate($category, $message, $language)
{ {
if ($this->forceTranslation || $language !== $this->sourceLanguage) { if ($this->forceTranslation || $language !== $this->sourceLanguage) {
return $this->translateMessage($category, $message, $language); return $this->translateMessage($category, $message, $language);
} else { } else {
return $message; return false;
} }
} }
...@@ -96,7 +96,7 @@ class MessageSource extends Component ...@@ -96,7 +96,7 @@ class MessageSource extends Component
* @param string $category the category that the message belongs to * @param string $category the category that the message belongs to
* @param string $message the message to be translated * @param string $message the message to be translated
* @param string $language the target language * @param string $language the target language
* @return string the translated message * @return string|boolean the translated message or false if translation wasn't found
*/ */
protected function translateMessage($category, $message, $language) protected function translateMessage($category, $message, $language)
{ {
...@@ -113,9 +113,8 @@ class MessageSource extends Component ...@@ -113,9 +113,8 @@ class MessageSource extends Component
'language' => $language, 'language' => $language,
]); ]);
$this->trigger(self::EVENT_MISSING_TRANSLATION, $event); $this->trigger(self::EVENT_MISSING_TRANSLATION, $event);
return $this->_messages[$key] = $event->message; $this->_messages[$key] = $event->message;
} else {
return $message;
} }
return false;
} }
} }
...@@ -160,25 +160,31 @@ abstract class BaseListView extends Widget ...@@ -160,25 +160,31 @@ abstract class BaseListView extends Widget
$page = $pagination->getPage() + 1; $page = $pagination->getPage() + 1;
$pageCount = $pagination->pageCount; $pageCount = $pagination->pageCount;
if (($summaryContent = $this->summary) === null) { if (($summaryContent = $this->summary) === null) {
$summaryContent = '<div class="summary">' return '<div class="summary">'
. Yii::t('yii', 'Showing <b>{begin, number}-{end, number}</b> of <b>{totalCount, number}</b> {totalCount, plural, one{item} other{items}}.') . Yii::t('yii', 'Showing <b>{begin, number}-{end, number}</b> of <b>{totalCount, number}</b> {totalCount, plural, one{item} other{items}}.', [
'begin' => $begin,
'end' => $end,
'count' => $count,
'totalCount' => $totalCount,
'page' => $page,
'pageCount' => $pageCount,
])
. '</div>'; . '</div>';
} }
} else { } else {
$begin = $page = $pageCount = 1; $begin = $page = $pageCount = 1;
$end = $totalCount = $count; $end = $totalCount = $count;
if (($summaryContent = $this->summary) === null) { if (($summaryContent = $this->summary) === null) {
$summaryContent = '<div class="summary">' . Yii::t('yii', 'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.') . '</div>'; return '<div class="summary">' . Yii::t('yii', 'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.', [
}
}
return Yii::$app->getI18n()->format($summaryContent, [
'begin' => $begin, 'begin' => $begin,
'end' => $end, 'end' => $end,
'count' => $count, 'count' => $count,
'totalCount' => $totalCount, 'totalCount' => $totalCount,
'page' => $page, 'page' => $page,
'pageCount' => $pageCount, 'pageCount' => $pageCount,
], Yii::$app->language); ]) . '</div>';
}
}
} }
/** /**
......
...@@ -89,6 +89,15 @@ class I18NTest extends TestCase ...@@ -89,6 +89,15 @@ class I18NTest extends TestCase
$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'));
} }
/**
* When translation is missing source language should be used for formatting.
* https://github.com/yiisoft/yii2/issues/2209
*/
public function testMissingTranslationFormatting()
{
$this->assertEquals('1 item', $this->i18n->translate('test', '{0, number} {0, plural, one{item} other{items}}', 1, 'hu'));
}
} }
class ParamModel extends Model class ParamModel extends Model
......
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