Fixes #6895: Added ignoreCategories config option for message command to ignore categories specified

This commit is contained in:
Alexander Makarov
2015-03-06 21:51:42 +03:00
parent b6a591c720
commit b7a29e38d9
6 changed files with 71 additions and 11 deletions

View File

@ -6,6 +6,7 @@ Yii Framework 2 Change Log
- Bug #7529: Fixed `yii\web\Response::sendContentAsFile()` that was broken in 2.0.3 (samdark)
- Bug #7603: Fixed escape characters in `FormatConverter` to work with unicode characters (maddoger, cebe)
- Enh #6895: Added `ignoreCategories` config option for message command to ignore categories specified (samdark)
- Enh #7488: Added `StringHelper::explode` to perform explode with trimming and skipping of empty elements (SilverFire, nineinchnick, creocoder, samdark)
- Enh #7562: `yii help` now lists all sub-commands by default (callmez)
- Enh #7571: HTTP status 500 and "An internal server error occurred." are now returned in case there was an exception in layout and `YII_DEBUG` is false (samdark)

View File

@ -91,6 +91,7 @@ class MessageController extends Controller
'removeUnused' => false,
'sort' => false,
'format' => 'php',
'ignoreCategories' => [],
], require($configFile));
if (!isset($config['sourcePath'], $config['languages'])) {
@ -117,7 +118,7 @@ class MessageController extends Controller
$messages = [];
foreach ($files as $file) {
$messages = array_merge_recursive($messages, $this->extractMessages($file, $config['translator']));
$messages = array_merge_recursive($messages, $this->extractMessages($file, $config['translator'], $config['ignoreCategories']));
}
if (in_array($config['format'], ['php', 'po'])) {
foreach ($config['languages'] as $language) {
@ -240,9 +241,10 @@ class MessageController extends Controller
*
* @param string $fileName name of the file to extract messages from
* @param string $translator name of the function used to translate messages
* @param array $ignoreCategories message categories to ignore
* @return array
*/
protected function extractMessages($fileName, $translator)
protected function extractMessages($fileName, $translator, $ignoreCategories = [])
{
$coloredFileName = Console::ansiFormat($fileName, [Console::FG_CYAN]);
$this->stdout("Extracting messages from $coloredFileName...\n");
@ -276,10 +278,12 @@ class MessageController extends Controller
$category = stripcslashes($buffer[0][1]);
$category = mb_substr($category, 1, mb_strlen($category) - 2);
if (!in_array($category, $ignoreCategories, true)) {
$message = stripcslashes($buffer[2][1]);
$message = mb_substr($message, 1, mb_strlen($message) - 2);
$messages[$category][] = $message;
}
} else {
// invalid call or dynamic call we can't extract

View File

@ -48,6 +48,12 @@ return [
// boolean, whether the message file should be overwritten with the merged messages
'overwrite' => true,
/*
// Message categories to ignore
'ignoreCategories' => [
'yii',
],
*/
/*
// 'db' output format is for saving messages to database.

View File

@ -99,9 +99,10 @@ abstract class BaseMessageControllerTest extends TestCase
* Loads messages
*
* @param string $category
* @param boolean $shouldExist if file should exist
* @return array
*/
abstract protected function loadMessages($category);
abstract protected function loadMessages($category, $shouldExist = true);
/**
* @return array default config
@ -340,6 +341,43 @@ abstract class BaseMessageControllerTest extends TestCase
$this->assertArrayHasKey($message3, $messages2, "message3 not found in category2. Command output:\n\n" . $out);
$this->assertArrayNotHasKey($message2, $messages2, "message2 found in category2. Command output:\n\n" . $out);
}
public function testIgnoreCategories()
{
$category1 = 'category1';
$category2 = 'category2';
$message1 = 'message1';
$message2 = 'message2';
$message3 = 'message3';
$this->saveConfigFile($this->getConfig(['ignoreCategories' => ['category2']]));
// Generate initial translation
$sourceFileContent = "Yii::t('{$category1}', '{$message1}'); Yii::t('{$category2}', '{$message2}');";
$source = $this->createSourceFile($sourceFileContent);
$out = $this->runMessageControllerAction('extract', [$this->configFileName]);
unlink($source);
$messages1 = $this->loadMessages($category1);
$messages2 = $this->loadMessages($category2, false);
$this->assertArrayHasKey($message1, $messages1, "message1 not found in category1. Command output:\n\n" . $out);
$this->assertArrayNotHasKey($message2, $messages2, "message2 found in category2. Command output:\n\n" . $out);
$this->assertArrayNotHasKey($message3, $messages2, "message3 found in category2. Command output:\n\n" . $out);
// Change source code, run translation again
$sourceFileContent = "Yii::t('{$category1}', '{$message1}'); Yii::t('{$category2}', '{$message3}');";
$source = $this->createSourceFile($sourceFileContent);
$out .= "\n" . $this->runMessageControllerAction('extract', [$this->configFileName]);
unlink($source);
$messages1 = $this->loadMessages($category1);
$messages2 = $this->loadMessages($category2, false);
$this->assertArrayHasKey($message1, $messages1, "message1 not found in category1. Command output:\n\n" . $out);
$this->assertArrayNotHasKey($message3, $messages2, "message3 not found in category2. Command output:\n\n" . $out);
$this->assertArrayNotHasKey($message2, $messages2, "message2 found in category2. Command output:\n\n" . $out);
}
}
class MessageControllerMock extends MessageController

View File

@ -70,15 +70,21 @@ class PHPMessageControllerTest extends BaseMessageControllerTest
/**
* @inheritdoc
*/
protected function loadMessages($category)
protected function loadMessages($category, $shouldExist = true)
{
if (defined('HHVM_VERSION')) {
// https://github.com/facebook/hhvm/issues/1447
$this->markTestSkipped('Can not test on HHVM because require is cached.');
static::markTestSkipped('Can not test on HHVM because require is cached.');
}
$messageFilePath = $this->getMessageFilePath($category);
$this->assertTrue(file_exists($messageFilePath), "There's no message file $messageFilePath!");
if ($shouldExist) {
static::assertTrue(file_exists($messageFilePath), "There's no message file $messageFilePath that should exist!");
} else {
static::assertFalse(file_exists($messageFilePath), "There's message file $messageFilePath that should not exist!");
return [];
}
return require $messageFilePath;
}
}

View File

@ -74,10 +74,15 @@ class POMessageControllerTest extends BaseMessageControllerTest
/**
* @inheritdoc
*/
protected function loadMessages($category)
protected function loadMessages($category, $shouldExist = true)
{
$messageFilePath = $this->getMessageFilePath();
$this->assertTrue(file_exists($messageFilePath), "There's no message file $messageFilePath!");
if ($shouldExist) {
static::assertTrue(file_exists($messageFilePath), "There's no message file $messageFilePath that should exist!");
} else {
static::assertFalse(file_exists($messageFilePath), "There's message file $messageFilePath that should not exist!");
return [];
}
$gettext = new GettextPoFile();
return $gettext->load($messageFilePath, $category);