diff --git a/framework/helpers/BaseFileHelper.php b/framework/helpers/BaseFileHelper.php index 3e2d56a66c..e8f3d4beca 100644 --- a/framework/helpers/BaseFileHelper.php +++ b/framework/helpers/BaseFileHelper.php @@ -42,8 +42,8 @@ class BaseFileHelper * The searching is based on the specified language code. In particular, * a file with the same name will be looked for under the subdirectory * whose name is the same as the language code. For example, given the file "path/to/view.php" - * and language code "zh_CN", the localized file will be looked for as - * "path/to/zh_CN/view.php". If the file is not found, it will try a fallback with just a language code that is + * and language code "zh-CN", the localized file will be looked for as + * "path/to/zh-CN/view.php". If the file is not found, it will try a fallback with just a language code that is * "zh" i.e. "path/to/zh/view.php". If it is not found as well the original file will be returned. * * If the target and the source language codes are the same, diff --git a/framework/i18n/DbMessageSource.php b/framework/i18n/DbMessageSource.php index 69a5380af3..31fca124ea 100644 --- a/framework/i18n/DbMessageSource.php +++ b/framework/i18n/DbMessageSource.php @@ -162,7 +162,7 @@ class DbMessageSource extends MessageSource ->andWhere('t2.id NOT IN (SELECT id FROM '.$this->messageTable.' WHERE language = :language)') ->params([':category' => $category, ':language' => $language, ':fallbackLanguage' => $fallbackLanguage]); - $mainQuery->union($fallbackQuery); + $mainQuery->union($fallbackQuery, true); } $messages = $mainQuery->createCommand($this->db)->queryAll(); diff --git a/framework/i18n/GettextMessageSource.php b/framework/i18n/GettextMessageSource.php index d7bd4900a4..c5b1f6f7e1 100644 --- a/framework/i18n/GettextMessageSource.php +++ b/framework/i18n/GettextMessageSource.php @@ -73,8 +73,8 @@ class GettextMessageSource extends MessageSource } else if (empty($messages)) { return $fallbackMessages; } else if (!empty($fallbackMessages)) { - foreach ($messages as $key => $value) { - if (empty($value) && !empty($fallbackMessages[$key])) { + foreach ($fallbackMessages as $key => $value) { + if (!empty($value) && empty($messages[$key])) { $messages[$key] = $fallbackMessages[$key]; } } diff --git a/framework/i18n/PhpMessageSource.php b/framework/i18n/PhpMessageSource.php index 453ef79f85..2e611f50e0 100644 --- a/framework/i18n/PhpMessageSource.php +++ b/framework/i18n/PhpMessageSource.php @@ -76,8 +76,8 @@ class PhpMessageSource extends MessageSource } else if (empty($messages)) { return $fallbackMessages; } else if (!empty($fallbackMessages)) { - foreach ($messages as $key => $value) { - if (empty($value) && !empty($fallbackMessages[$key])) { + foreach ($fallbackMessages as $key => $value) { + if (!empty($value) && empty($messages[$key])) { $messages[$key] = $fallbackMessages[$key]; } } diff --git a/tests/unit/data/i18n/messages/de/test.php b/tests/unit/data/i18n/messages/de/test.php new file mode 100644 index 0000000000..f2d6418c22 --- /dev/null +++ b/tests/unit/data/i18n/messages/de/test.php @@ -0,0 +1,7 @@ + 'Hallo Welt!', +]; \ No newline at end of file diff --git a/tests/unit/data/i18n/messages/ru/test.php b/tests/unit/data/i18n/messages/ru/test.php new file mode 100644 index 0000000000..ca6ad202ec --- /dev/null +++ b/tests/unit/data/i18n/messages/ru/test.php @@ -0,0 +1,7 @@ + 'Собака бегает быстро.', +]; \ No newline at end of file diff --git a/tests/unit/framework/i18n/I18NTest.php b/tests/unit/framework/i18n/I18NTest.php index aa2356be53..5d6a78b326 100644 --- a/tests/unit/framework/i18n/I18NTest.php +++ b/tests/unit/framework/i18n/I18NTest.php @@ -40,8 +40,18 @@ 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')); + + // source = target. Should be returned as is. + $this->assertEquals('The dog runs fast.', $this->i18n->translate('test', $msg, [], 'en')); + + // exact match $this->assertEquals('Der Hund rennt schnell.', $this->i18n->translate('test', $msg, [], 'de-DE')); + + // fallback to just language code with absent exact match + $this->assertEquals('Собака бегает быстро.', $this->i18n->translate('test', $msg, [], 'ru-RU')); + + // fallback to just langauge code with present exact match + $this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE')); } public function testTranslateParams()