Commit 1e602adb by Alexander Makarov

- Fixed BaseFileHelper::localize docs.

- DbMessageSource now performs UNION ALL. - Fixed message merging for php and po. - Updated tests.
parent f6c6fe27
...@@ -42,8 +42,8 @@ class BaseFileHelper ...@@ -42,8 +42,8 @@ class BaseFileHelper
* The searching is based on the specified language code. In particular, * The searching is based on the specified language code. In particular,
* a file with the same name will be looked for under the subdirectory * 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" * 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 * 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 * "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. * "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, * If the target and the source language codes are the same,
......
...@@ -162,7 +162,7 @@ class DbMessageSource extends MessageSource ...@@ -162,7 +162,7 @@ class DbMessageSource extends MessageSource
->andWhere('t2.id NOT IN (SELECT id FROM '.$this->messageTable.' WHERE language = :language)') ->andWhere('t2.id NOT IN (SELECT id FROM '.$this->messageTable.' WHERE language = :language)')
->params([':category' => $category, ':language' => $language, ':fallbackLanguage' => $fallbackLanguage]); ->params([':category' => $category, ':language' => $language, ':fallbackLanguage' => $fallbackLanguage]);
$mainQuery->union($fallbackQuery); $mainQuery->union($fallbackQuery, true);
} }
$messages = $mainQuery->createCommand($this->db)->queryAll(); $messages = $mainQuery->createCommand($this->db)->queryAll();
......
...@@ -73,8 +73,8 @@ class GettextMessageSource extends MessageSource ...@@ -73,8 +73,8 @@ class GettextMessageSource extends MessageSource
} else if (empty($messages)) { } else if (empty($messages)) {
return $fallbackMessages; return $fallbackMessages;
} else if (!empty($fallbackMessages)) { } else if (!empty($fallbackMessages)) {
foreach ($messages as $key => $value) { foreach ($fallbackMessages as $key => $value) {
if (empty($value) && !empty($fallbackMessages[$key])) { if (!empty($value) && empty($messages[$key])) {
$messages[$key] = $fallbackMessages[$key]; $messages[$key] = $fallbackMessages[$key];
} }
} }
......
...@@ -76,8 +76,8 @@ class PhpMessageSource extends MessageSource ...@@ -76,8 +76,8 @@ class PhpMessageSource extends MessageSource
} else if (empty($messages)) { } else if (empty($messages)) {
return $fallbackMessages; return $fallbackMessages;
} else if (!empty($fallbackMessages)) { } else if (!empty($fallbackMessages)) {
foreach ($messages as $key => $value) { foreach ($fallbackMessages as $key => $value) {
if (empty($value) && !empty($fallbackMessages[$key])) { if (!empty($value) && empty($messages[$key])) {
$messages[$key] = $fallbackMessages[$key]; $messages[$key] = $fallbackMessages[$key];
} }
} }
......
<?php
/**
*
*/
return [
'Hello world!' => 'Hallo Welt!',
];
\ No newline at end of file
<?php
/**
*
*/
return [
'The dog runs fast.' => 'Собака бегает быстро.',
];
\ No newline at end of file
...@@ -40,8 +40,18 @@ class I18NTest extends TestCase ...@@ -40,8 +40,18 @@ 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'));
// 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')); $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() public function testTranslateParams()
......
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