Browse Source

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

tags/2.0.0-beta
Klimov Paul 11 years ago
parent
commit
7c9f74722c
  1. 14
      framework/yii/console/controllers/MessageController.php
  2. 10
      tests/unit/framework/console/controllers/MessageControllerTest.php

14
framework/yii/console/controllers/MessageController.php

@ -55,7 +55,8 @@ class MessageController extends Controller
* directory 'sourcePath/a/b'.
* - translator: the name of the function for translating messages.
* 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.
* - removeOld: if message no longer needs translation it will be removed,
* instead of being enclosed between a pair of '@@' marks.
@ -133,10 +134,14 @@ class MessageController extends Controller
{
echo "Extracting messages from $fileName...\n";
$subject = file_get_contents($fileName);
$messages = array();
if (!is_array($translator)) {
$translator = array($translator);
}
foreach ($translator as $currentTranslator) {
$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);
$messages = array();
for ($i = 0; $i < $n; ++$i) {
if (($pos = strpos($matches[$i][1], '.')) !== false) {
$category = substr($matches[$i][1], $pos + 1, -1);
@ -146,6 +151,7 @@ class MessageController extends Controller
$message = $matches[$i][2];
$messages[$category][] = eval("return $message;"); // use eval to eliminate quote escape
}
}
return $messages;
}
@ -172,7 +178,7 @@ class MessageController extends Controller
$merged = array();
$untranslated = array();
foreach ($messages as $message) {
if (!empty($translated[$message])) {
if (array_key_exists($message, $translated) && strlen($translated[$message]) > 0) {
$merged[$message] = $translated[$message];
} else {
$untranslated[] = $message;

10
tests/unit/framework/console/controllers/MessageControllerTest.php

@ -17,6 +17,9 @@ class MessageControllerTest extends TestCase
{
$this->sourcePath = Yii::getAlias('@yiiunit/runtime/test_source');
$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->createDir($this->messagePath);
$this->configFileName = Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . 'message_controller_test_config.php';
@ -81,8 +84,9 @@ class MessageControllerTest extends TestCase
protected function createMessageController()
{
$module = $this->getMock('yii\\base\\Module', array('fake'), array('console'));
$command = new MessageController('message', $module);
return $command;
$messageController = new MessageController('message', $module);
$messageController->interactive = false;
return $messageController;
}
/**
@ -166,8 +170,6 @@ class MessageControllerTest extends TestCase
public function testCreateTranslation()
{
$this->markTestIncomplete('MessageController is incomplete');
$language = 'en';
$category = 'test_category';

Loading…
Cancel
Save