Fixes #14016: Fixed empty messages marked as unused in PHP and PO sources when extracted with message command when markUnused is false

This commit is contained in:
Alexander Makarov
2017-10-06 22:24:38 +02:00
committed by GitHub
parent 66723d0e74
commit 6bde69aa96
3 changed files with 38 additions and 2 deletions

View File

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.13 under development
------------------------
- Bug #14016: Fixed empty messages marked as unused in PHP and PO sources when extracted with message command when `markUnused` is `false` (samdark)
- Enh #9438: `yii\web\DbSession` now relies on error handler to display errors (samdark)
- Bug #6226: Fix fatal symlink error when publishing in multi threaded environments (dynasource)
- Enh #13486: Use DI container to instantiate cookies in order to be able to set defaults (samdark)

View File

@ -702,7 +702,7 @@ EOD;
ksort($existingMessages);
foreach ($existingMessages as $message => $translation) {
if (!$removeUnused && !isset($merged[$message]) && !isset($todo[$message])) {
if (!empty($translation) && (!$markUnused || (strncmp($translation, '@@', 2) === 0 && substr_compare($translation, '@@', -2, 2) === 0))) {
if (!$markUnused || (!empty($translation) && (strncmp($translation, '@@', 2) === 0 && substr_compare($translation, '@@', -2, 2) === 0))) {
$todo[$message] = $translation;
} else {
$todo[$message] = '@@' . $translation . '@@';
@ -804,7 +804,7 @@ EOD;
// add obsolete unused messages
foreach ($existingMessages as $message => $translation) {
if (!$removeUnused && !isset($merged[$category . chr(4) . $message]) && !isset($todos[$category . chr(4) . $message])) {
if (!empty($translation) && (!$markUnused || (substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@'))) {
if (!$markUnused || (!empty($translation) && ((substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@')))) {
$todos[$category . chr(4) . $message] = $translation;
} else {
$todos[$category . chr(4) . $message] = '@@' . $translation . '@@';

View File

@ -478,6 +478,41 @@ abstract class BaseMessageControllerTest extends TestCase
$this->assertArrayHasKey($mainMessage, $messages,
"\"$mainMessage\" is missing in translation file. Command output:\n\n" . $out);
}
/**
* @see https://github.com/yiisoft/yii2/issues/14016
*/
public function testShouldNotMarkUnused()
{
$category = 'my';
$key1 = 'key1';
$key2 = 'key2';
$this->saveMessages(
[
$key1 => '',
$key2 => '',
],
$category
);
$sourceFileContent = 'Yii::t("my", "test");';
$this->createSourceFile($sourceFileContent);
$this->saveConfigFile($this->getConfig(['markUnused' => false]));
$out = $this->runMessageControllerAction('extract', [$this->configFileName]);
$messages = $this->loadMessages($category);
$this->assertArrayHasKey($key1, $messages, "$key1 isn't there. Command output:\n\n" . $out);
$this->assertArrayHasKey($key2, $messages, "$key2 isn't there. Command output:\n\n" . $out);
$value1 = $messages[$key1];
$value2 = $messages[$key2];
$this->assertEquals('', $value1, "Message at $key1 should be empty but it is $value1. Command output:\n\n" . $out);
$this->assertEquals('', $value2, "Message at $key2 should be empty but it is $value2. Command output:\n\n" . $out);
}
}
class MessageControllerMock extends MessageController