Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marius7383/bfe59084495f1e429498 to your computer and use it in GitHub Desktop.
Save marius7383/bfe59084495f1e429498 to your computer and use it in GitHub Desktop.
Patch that shows how an additional config parameter "markUnused" could be introduced into Yii2 MessageController in order to prevent appending and prepending @@ strings to (potentially) unused messages.
diff --git a/vendor/yiisoft/yii2/console/controllers/MessageController.php b/vendor/yiisoft/yii2/console/controllers/MessageController.php
--- a/vendor/yiisoft/yii2/console/controllers/MessageController.php
+++ b/vendor/yiisoft/yii2/console/controllers/MessageController.php
@@ -89,6 +89,7 @@ class MessageController extends Controller
'translator' => 'Yii::t',
'overwrite' => false,
'removeUnused' => false,
+ 'markUnused' => false,
'sort' => false,
'format' => 'php',
'ignoreCategories' => [],
@@ -128,9 +129,9 @@ class MessageController extends Controller
}
if ($config['format'] === 'po') {
$catalog = isset($config['catalog']) ? $config['catalog'] : 'messages';
- $this->saveMessagesToPO($messages, $dir, $config['overwrite'], $config['removeUnused'], $config['sort'], $catalog);
+ $this->saveMessagesToPO($messages, $dir, $config['overwrite'], $config['removeUnused'], $config['markUnused'], $config['sort'], $catalog);
} else {
- $this->saveMessagesToPHP($messages, $dir, $config['overwrite'], $config['removeUnused'], $config['sort']);
+ $this->saveMessagesToPHP($messages, $dir, $config['overwrite'], $config['removeUnused'], $config['markUnused'], $config['sort']);
}
}
} elseif ($config['format'] === 'db') {
@@ -146,6 +147,7 @@ class MessageController extends Controller
$sourceMessageTable,
$messageTable,
$config['removeUnused'],
+ $config['markUnused'],
$config['languages']
);
}
@@ -161,7 +163,7 @@ class MessageController extends Controller
* @param boolean $removeUnused
* @param array $languages
*/
- protected function saveMessagesToDb($messages, $db, $sourceMessageTable, $messageTable, $removeUnused, $languages)
+ protected function saveMessagesToDb($messages, $db, $sourceMessageTable, $messageTable, $removeUnused, $markUnused, $languages)
{
$q = new \yii\db\Query;
$current = [];
@@ -224,10 +226,14 @@ class MessageController extends Controller
->execute();
$this->stdout("deleted.\n");
} else {
+ $expression = 'message';
+ if ($markUnused) {
+ $expression = "CONCAT('@@',message,'@@')";
+ }
$db->createCommand()
->update(
$sourceMessageTable,
- ['message' => new \yii\db\Expression("CONCAT('@@',message,'@@')")],
+ ['message' => new \yii\db\Expression($expression)],
['in', 'id', $obsolete]
)->execute();
$this->stdout("updated.\n");
@@ -352,7 +358,7 @@ class MessageController extends Controller
* @param boolean $removeUnused if obsolete translations should be removed
* @param boolean $sort if translations should be sorted
*/
- protected function saveMessagesToPHP($messages, $dirName, $overwrite, $removeUnused, $sort)
+ protected function saveMessagesToPHP($messages, $dirName, $overwrite, $removeUnused, $markUnused, $sort)
{
foreach ($messages as $category => $msgs) {
$file = str_replace("\\", '/', "$dirName/$category.php");
@@ -361,7 +367,7 @@ class MessageController extends Controller
$msgs = array_values(array_unique($msgs));
$coloredFileName = Console::ansiFormat($file, [Console::FG_CYAN]);
$this->stdout("Saving messages to $coloredFileName...\n");
- $this->saveMessagesCategoryToPHP($msgs, $file, $overwrite, $removeUnused, $sort, $category);
+ $this->saveMessagesCategoryToPHP($msgs, $file, $overwrite, $removeUnused, $markUnused, $sort, $category);
}
}
@@ -375,7 +381,7 @@ class MessageController extends Controller
* @param boolean $sort if translations should be sorted
* @param string $category message category
*/
- protected function saveMessagesCategoryToPHP($messages, $fileName, $overwrite, $removeUnused, $sort, $category)
+ protected function saveMessagesCategoryToPHP($messages, $fileName, $overwrite, $removeUnused, $markUnused, $sort, $category)
{
if (is_file($fileName)) {
$existingMessages = require($fileName);
@@ -403,7 +409,7 @@ class MessageController extends Controller
ksort($existingMessages);
foreach ($existingMessages as $message => $translation) {
if (!$removeUnused && !isset($merged[$message]) && !isset($todo[$message])) {
- if (!empty($translation) && strncmp($translation, '@@', 2) === 0 && substr_compare($translation, '@@', -2, 2) === 0) {
+ if (!empty($translation) && ((strncmp($translation, '@@', 2) === 0 && substr_compare($translation, '@@', -2, 2) === 0) || !$markUnused)) {
$todo[$message] = $translation;
} else {
$todo[$message] = '@@' . $translation . '@@';
@@ -465,7 +471,7 @@ EOD;
* @param boolean $sort if translations should be sorted
* @param string $catalog message catalog
*/
- protected function saveMessagesToPO($messages, $dirName, $overwrite, $removeUnused, $sort, $catalog)
+ protected function saveMessagesToPO($messages, $dirName, $overwrite, $removeUnused, $markUnused, $sort, $catalog)
{
$file = str_replace("\\", '/', "$dirName/$catalog.po");
FileHelper::createDirectory(dirname($file));
@@ -517,7 +523,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) && substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@') {
+ if (!empty($translation) && ((substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@') || !$markUnused)) {
$todos[$category . chr(4) . $message] = $translation;
} else {
$todos[$category . chr(4) . $message] = '@@' . $translation . '@@';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment