diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 0125a10368..cb25251fa4 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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) diff --git a/framework/console/controllers/MessageController.php b/framework/console/controllers/MessageController.php index 60c8db05c4..ddc26d56b1 100644 --- a/framework/console/controllers/MessageController.php +++ b/framework/console/controllers/MessageController.php @@ -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); - $message = stripcslashes($buffer[2][1]); - $message = mb_substr($message, 1, mb_strlen($message) - 2); + if (!in_array($category, $ignoreCategories, true)) { + $message = stripcslashes($buffer[2][1]); + $message = mb_substr($message, 1, mb_strlen($message) - 2); - $messages[$category][] = $message; + $messages[$category][] = $message; + } } else { // invalid call or dynamic call we can't extract diff --git a/framework/views/messageConfig.php b/framework/views/messageConfig.php index 206c663ea5..5b8d22e3b6 100644 --- a/framework/views/messageConfig.php +++ b/framework/views/messageConfig.php @@ -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. diff --git a/tests/unit/framework/console/controllers/BaseMessageControllerTest.php b/tests/unit/framework/console/controllers/BaseMessageControllerTest.php index 5cc72d55dd..cffc93acdf 100644 --- a/tests/unit/framework/console/controllers/BaseMessageControllerTest.php +++ b/tests/unit/framework/console/controllers/BaseMessageControllerTest.php @@ -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 diff --git a/tests/unit/framework/console/controllers/PHPMessageControllerTest.php b/tests/unit/framework/console/controllers/PHPMessageControllerTest.php index 20ddf1d78a..9f4e47175c 100644 --- a/tests/unit/framework/console/controllers/PHPMessageControllerTest.php +++ b/tests/unit/framework/console/controllers/PHPMessageControllerTest.php @@ -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; } } \ No newline at end of file diff --git a/tests/unit/framework/console/controllers/POMessageControllerTest.php b/tests/unit/framework/console/controllers/POMessageControllerTest.php index a02a13a68b..1ed97fbcf6 100644 --- a/tests/unit/framework/console/controllers/POMessageControllerTest.php +++ b/tests/unit/framework/console/controllers/POMessageControllerTest.php @@ -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);