mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-09 01:27:20 +08:00
Fixes #6895: Added ignoreCategories config option for message command to ignore categories specified
This commit is contained in:
@ -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 #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)
|
- 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 #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 #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)
|
- 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)
|
||||||
|
|||||||
@ -91,6 +91,7 @@ class MessageController extends Controller
|
|||||||
'removeUnused' => false,
|
'removeUnused' => false,
|
||||||
'sort' => false,
|
'sort' => false,
|
||||||
'format' => 'php',
|
'format' => 'php',
|
||||||
|
'ignoreCategories' => [],
|
||||||
], require($configFile));
|
], require($configFile));
|
||||||
|
|
||||||
if (!isset($config['sourcePath'], $config['languages'])) {
|
if (!isset($config['sourcePath'], $config['languages'])) {
|
||||||
@ -117,7 +118,7 @@ class MessageController extends Controller
|
|||||||
|
|
||||||
$messages = [];
|
$messages = [];
|
||||||
foreach ($files as $file) {
|
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'])) {
|
if (in_array($config['format'], ['php', 'po'])) {
|
||||||
foreach ($config['languages'] as $language) {
|
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 $fileName name of the file to extract messages from
|
||||||
* @param string $translator name of the function used to translate messages
|
* @param string $translator name of the function used to translate messages
|
||||||
|
* @param array $ignoreCategories message categories to ignore
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function extractMessages($fileName, $translator)
|
protected function extractMessages($fileName, $translator, $ignoreCategories = [])
|
||||||
{
|
{
|
||||||
$coloredFileName = Console::ansiFormat($fileName, [Console::FG_CYAN]);
|
$coloredFileName = Console::ansiFormat($fileName, [Console::FG_CYAN]);
|
||||||
$this->stdout("Extracting messages from $coloredFileName...\n");
|
$this->stdout("Extracting messages from $coloredFileName...\n");
|
||||||
@ -276,10 +278,12 @@ class MessageController extends Controller
|
|||||||
$category = stripcslashes($buffer[0][1]);
|
$category = stripcslashes($buffer[0][1]);
|
||||||
$category = mb_substr($category, 1, mb_strlen($category) - 2);
|
$category = mb_substr($category, 1, mb_strlen($category) - 2);
|
||||||
|
|
||||||
$message = stripcslashes($buffer[2][1]);
|
if (!in_array($category, $ignoreCategories, true)) {
|
||||||
$message = mb_substr($message, 1, mb_strlen($message) - 2);
|
$message = stripcslashes($buffer[2][1]);
|
||||||
|
$message = mb_substr($message, 1, mb_strlen($message) - 2);
|
||||||
|
|
||||||
$messages[$category][] = $message;
|
$messages[$category][] = $message;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// invalid call or dynamic call we can't extract
|
// invalid call or dynamic call we can't extract
|
||||||
|
|
||||||
|
|||||||
@ -48,6 +48,12 @@ return [
|
|||||||
// boolean, whether the message file should be overwritten with the merged messages
|
// boolean, whether the message file should be overwritten with the merged messages
|
||||||
'overwrite' => true,
|
'overwrite' => true,
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Message categories to ignore
|
||||||
|
'ignoreCategories' => [
|
||||||
|
'yii',
|
||||||
|
],
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// 'db' output format is for saving messages to database.
|
// 'db' output format is for saving messages to database.
|
||||||
|
|||||||
@ -99,9 +99,10 @@ abstract class BaseMessageControllerTest extends TestCase
|
|||||||
* Loads messages
|
* Loads messages
|
||||||
*
|
*
|
||||||
* @param string $category
|
* @param string $category
|
||||||
|
* @param boolean $shouldExist if file should exist
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
abstract protected function loadMessages($category);
|
abstract protected function loadMessages($category, $shouldExist = true);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array default config
|
* @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->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);
|
$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
|
class MessageControllerMock extends MessageController
|
||||||
|
|||||||
@ -70,15 +70,21 @@ class PHPMessageControllerTest extends BaseMessageControllerTest
|
|||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
protected function loadMessages($category)
|
protected function loadMessages($category, $shouldExist = true)
|
||||||
{
|
{
|
||||||
if (defined('HHVM_VERSION')) {
|
if (defined('HHVM_VERSION')) {
|
||||||
// https://github.com/facebook/hhvm/issues/1447
|
// 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);
|
$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;
|
return require $messageFilePath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -74,10 +74,15 @@ class POMessageControllerTest extends BaseMessageControllerTest
|
|||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
protected function loadMessages($category)
|
protected function loadMessages($category, $shouldExist = true)
|
||||||
{
|
{
|
||||||
$messageFilePath = $this->getMessageFilePath();
|
$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();
|
$gettext = new GettextPoFile();
|
||||||
return $gettext->load($messageFilePath, $category);
|
return $gettext->load($messageFilePath, $category);
|
||||||
|
|||||||
Reference in New Issue
Block a user