Commit 7c9f7472 by Klimov Paul

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

parent 41d1a354
......@@ -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,18 +134,23 @@ class MessageController extends Controller
{
echo "Extracting messages from $fileName...\n";
$subject = file_get_contents($fileName);
$n = preg_match_all(
'/\b' . $translator . '\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);
} else {
$category=substr($matches[$i][1], 1, -1);
if (!is_array($translator)) {
$translator = array($translator);
}
foreach ($translator as $currentTranslator) {
$n = preg_match_all(
'/\b' . $currentTranslator . '\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s',
$subject, $matches, PREG_SET_ORDER);
for ($i = 0; $i < $n; ++$i) {
if (($pos = strpos($matches[$i][1], '.')) !== false) {
$category = substr($matches[$i][1], $pos + 1, -1);
} else {
$category = substr($matches[$i][1], 1, -1);
}
$message = $matches[$i][2];
$messages[$category][] = eval("return $message;"); // use eval to eliminate quote escape
}
$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;
......
......@@ -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;
}
/**
......@@ -91,7 +95,7 @@ class MessageControllerTest extends TestCase
* @param array $args action arguments.
* @return string command output.
*/
protected function runMessageControllerAction($actionId, array $args=array())
protected function runMessageControllerAction($actionId, array $args = array())
{
$controller = $this->createMessageController();
ob_start();
......@@ -166,13 +170,11 @@ class MessageControllerTest extends TestCase
public function testCreateTranslation()
{
$this->markTestIncomplete('MessageController is incomplete');
$language = 'en';
$category = 'test_category';
$message = 'test message';
$sourceFileContent = "Yii::t('{$category}','{$message}')";
$sourceFileContent = "Yii::t('{$category}', '{$message}')";
$this->createSourceFile($sourceFileContent);
$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