Commit 7c9f7472 by Klimov Paul

"yii\console\controllers\MessageController" has been fixed.

parent 41d1a354
...@@ -55,7 +55,8 @@ class MessageController extends Controller ...@@ -55,7 +55,8 @@ class MessageController extends Controller
* directory 'sourcePath/a/b'. * directory 'sourcePath/a/b'.
* - translator: the name of the function for translating messages. * - translator: the name of the function for translating messages.
* Defaults to 'Yii::t'. This is used as a mark to find messages to be * Defaults to 'Yii::t'. This is used as a mark to find messages to be
* translated. * translated. Accepts both string for single function name or array for
* multiple function names.
* - overwrite: if message file must be overwritten with the merged messages. * - overwrite: if message file must be overwritten with the merged messages.
* - removeOld: if message no longer needs translation it will be removed, * - removeOld: if message no longer needs translation it will be removed,
* instead of being enclosed between a pair of '@@' marks. * instead of being enclosed between a pair of '@@' marks.
...@@ -133,19 +134,24 @@ class MessageController extends Controller ...@@ -133,19 +134,24 @@ class MessageController extends Controller
{ {
echo "Extracting messages from $fileName...\n"; echo "Extracting messages from $fileName...\n";
$subject = file_get_contents($fileName); $subject = file_get_contents($fileName);
$messages = array();
if (!is_array($translator)) {
$translator = array($translator);
}
foreach ($translator as $currentTranslator) {
$n = preg_match_all( $n = preg_match_all(
'/\b' . $translator . '\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s', '/\b' . $currentTranslator . '\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s',
$subject, $matches, PREG_SET_ORDER); $subject, $matches, PREG_SET_ORDER);
$messages = array();
for ($i = 0; $i < $n; ++$i) { for ($i = 0; $i < $n; ++$i) {
if (($pos = strpos($matches[$i][1], '.')) !== false) { if (($pos = strpos($matches[$i][1], '.')) !== false) {
$category=substr($matches[$i][1], $pos + 1, -1); $category = substr($matches[$i][1], $pos + 1, -1);
} else { } else {
$category=substr($matches[$i][1], 1, -1); $category = substr($matches[$i][1], 1, -1);
} }
$message = $matches[$i][2]; $message = $matches[$i][2];
$messages[$category][] = eval("return $message;"); // use eval to eliminate quote escape $messages[$category][] = eval("return $message;"); // use eval to eliminate quote escape
} }
}
return $messages; return $messages;
} }
...@@ -172,7 +178,7 @@ class MessageController extends Controller ...@@ -172,7 +178,7 @@ class MessageController extends Controller
$merged = array(); $merged = array();
$untranslated = array(); $untranslated = array();
foreach ($messages as $message) { foreach ($messages as $message) {
if (!empty($translated[$message])) { if (array_key_exists($message, $translated) && strlen($translated[$message]) > 0) {
$merged[$message] = $translated[$message]; $merged[$message] = $translated[$message];
} else { } else {
$untranslated[] = $message; $untranslated[] = $message;
......
...@@ -17,6 +17,9 @@ class MessageControllerTest extends TestCase ...@@ -17,6 +17,9 @@ class MessageControllerTest extends TestCase
{ {
$this->sourcePath = Yii::getAlias('@yiiunit/runtime/test_source'); $this->sourcePath = Yii::getAlias('@yiiunit/runtime/test_source');
$this->createDir($this->sourcePath); $this->createDir($this->sourcePath);
if (!file_exists($this->sourcePath)) {
$this->markTestIncomplete('Unit tests runtime directory should have writable permissions!');
}
$this->messagePath = Yii::getAlias('@yiiunit/runtime/test_messages'); $this->messagePath = Yii::getAlias('@yiiunit/runtime/test_messages');
$this->createDir($this->messagePath); $this->createDir($this->messagePath);
$this->configFileName = Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . 'message_controller_test_config.php'; $this->configFileName = Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . 'message_controller_test_config.php';
...@@ -81,8 +84,9 @@ class MessageControllerTest extends TestCase ...@@ -81,8 +84,9 @@ class MessageControllerTest extends TestCase
protected function createMessageController() protected function createMessageController()
{ {
$module = $this->getMock('yii\\base\\Module', array('fake'), array('console')); $module = $this->getMock('yii\\base\\Module', array('fake'), array('console'));
$command = new MessageController('message', $module); $messageController = new MessageController('message', $module);
return $command; $messageController->interactive = false;
return $messageController;
} }
/** /**
...@@ -91,7 +95,7 @@ class MessageControllerTest extends TestCase ...@@ -91,7 +95,7 @@ class MessageControllerTest extends TestCase
* @param array $args action arguments. * @param array $args action arguments.
* @return string command output. * @return string command output.
*/ */
protected function runMessageControllerAction($actionId, array $args=array()) protected function runMessageControllerAction($actionId, array $args = array())
{ {
$controller = $this->createMessageController(); $controller = $this->createMessageController();
ob_start(); ob_start();
...@@ -166,13 +170,11 @@ class MessageControllerTest extends TestCase ...@@ -166,13 +170,11 @@ class MessageControllerTest extends TestCase
public function testCreateTranslation() public function testCreateTranslation()
{ {
$this->markTestIncomplete('MessageController is incomplete');
$language = 'en'; $language = 'en';
$category = 'test_category'; $category = 'test_category';
$message = 'test message'; $message = 'test message';
$sourceFileContent = "Yii::t('{$category}','{$message}')"; $sourceFileContent = "Yii::t('{$category}', '{$message}')";
$this->createSourceFile($sourceFileContent); $this->createSourceFile($sourceFileContent);
$this->composeConfigFile(array( $this->composeConfigFile(array(
......
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