mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-25 03:01:21 +08:00
Fixes #4453: yii message/extract wasn't properly writing to po files in case of multiple categories
This commit is contained in:
@@ -72,6 +72,7 @@ Yii Framework 2 Change Log
|
||||
- Bug #4409: Upper case letters in subdirectory prefixes of controller IDs were not properly handled (qiangxue)
|
||||
- Bug #4412: Formatter used SI Prefixes for base 1024, now uses binary prefixes (kmindi)
|
||||
- Bug #4427: Formatter could do one division too much (kmindi)
|
||||
- Bug #4453: `yii message/extract` wasn't properly writing to po files in case of multiple categories (samdark)
|
||||
- Bug: Fixed inconsistent return of `\yii\console\Application::runAction()` (samdark)
|
||||
- Bug: URL encoding for the route parameter added to `\yii\web\UrlManager` (klimov-paul)
|
||||
- Bug: Fixed the bug that requesting protected or private action methods would cause 500 error instead of 404 (qiangxue)
|
||||
|
||||
@@ -209,11 +209,11 @@ class MessageController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
echo $savedFlag ? "saved.\n" : "nothing new...skipped.\n";
|
||||
echo $savedFlag ? "saved.\n" : "Nothing new...skipped.\n";
|
||||
echo $removeUnused ? "Deleting obsoleted messages..." : "Updating obsoleted messages...";
|
||||
|
||||
if (empty($obsolete)) {
|
||||
echo "nothing obsoleted...skipped.\n";
|
||||
echo "Nothing obsoleted...skipped.\n";
|
||||
} else {
|
||||
if ($removeUnused) {
|
||||
$db->createCommand()
|
||||
@@ -286,12 +286,10 @@ class MessageController extends Controller
|
||||
foreach ($messages as $category => $msgs) {
|
||||
$file = str_replace("\\", '/', "$dirName/$category.php");
|
||||
$path = dirname($file);
|
||||
if (!is_dir($path)) {
|
||||
mkdir($path, 0755, true);
|
||||
}
|
||||
FileHelper::createDirectory($path);
|
||||
$msgs = array_values(array_unique($msgs));
|
||||
echo "Saving messages to $file...\n";
|
||||
$this->saveMessagesCategoryToPHP($msgs, $file, $overwrite, $removeUnused, $sort);
|
||||
$this->saveMessagesCategoryToPHP($msgs, $file, $overwrite, $removeUnused, $sort, $category);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,23 +301,23 @@ class MessageController extends Controller
|
||||
* @param boolean $overwrite if existing file should be overwritten without backup
|
||||
* @param boolean $removeUnused if obsolete translations should be removed
|
||||
* @param boolean $sort if translations should be sorted
|
||||
* @param string $category message category
|
||||
*/
|
||||
protected function saveMessagesCategoryToPHP($messages, $fileName, $overwrite, $removeUnused, $sort)
|
||||
protected function saveMessagesCategoryToPHP($messages, $fileName, $overwrite, $removeUnused, $sort, $category)
|
||||
{
|
||||
if (is_file($fileName)) {
|
||||
$translated = require($fileName);
|
||||
$existingMessages = require($fileName);
|
||||
sort($messages);
|
||||
ksort($translated);
|
||||
if (array_keys($translated) == $messages) {
|
||||
echo "nothing new...skipped.\n";
|
||||
|
||||
return self::EXIT_CODE_NORMAL;
|
||||
ksort($existingMessages);
|
||||
if (array_keys($existingMessages) == $messages) {
|
||||
echo "Nothing new in \"$category\" category... Nothing to save.\n";
|
||||
return;
|
||||
}
|
||||
$merged = [];
|
||||
$untranslated = [];
|
||||
foreach ($messages as $message) {
|
||||
if (array_key_exists($message, $translated) && strlen($translated[$message]) > 0) {
|
||||
$merged[$message] = $translated[$message];
|
||||
if (array_key_exists($message, $existingMessages) && strlen($existingMessages[$message]) > 0) {
|
||||
$merged[$message] = $existingMessages[$message];
|
||||
} else {
|
||||
$untranslated[] = $message;
|
||||
}
|
||||
@@ -330,10 +328,10 @@ class MessageController extends Controller
|
||||
foreach ($untranslated as $message) {
|
||||
$todo[$message] = '';
|
||||
}
|
||||
ksort($translated);
|
||||
foreach ($translated as $message => $translation) {
|
||||
ksort($existingMessages);
|
||||
foreach ($existingMessages as $message => $translation) {
|
||||
if (!isset($merged[$message]) && !isset($todo[$message]) && !$removeUnused) {
|
||||
if (substr_compare($translation, '@@', 0, 2) === 0 && substr_compare($translation, '@@', -2) === 0) {
|
||||
if (mb_strlen($translation, Yii::$app->charset) >= 2 && substr_compare($translation, '@@', 0, 2) === 0 && substr_compare($translation, '@@', -2) === 0) {
|
||||
$todo[$message] = $translation;
|
||||
} else {
|
||||
$todo[$message] = '@@' . $translation . '@@';
|
||||
@@ -355,7 +353,6 @@ class MessageController extends Controller
|
||||
}
|
||||
ksort($merged);
|
||||
}
|
||||
echo "Saved.\n";
|
||||
|
||||
|
||||
$array = VarDumper::export($merged);
|
||||
@@ -383,6 +380,7 @@ return $array;
|
||||
EOD;
|
||||
|
||||
file_put_contents($fileName, $content);
|
||||
echo "Saved.\n";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -398,10 +396,7 @@ EOD;
|
||||
protected function saveMessagesToPO($messages, $dirName, $overwrite, $removeUnused, $sort, $catalog)
|
||||
{
|
||||
$file = str_replace("\\", '/', "$dirName/$catalog.po");
|
||||
$path = dirname($file);
|
||||
if (!is_dir($path)) {
|
||||
mkdir($path, 0755, true);
|
||||
}
|
||||
FileHelper::createDirectory(dirname($file));
|
||||
echo "Saving messages to $file...\n";
|
||||
|
||||
$poFile = new GettextPoFile();
|
||||
@@ -411,6 +406,7 @@ EOD;
|
||||
$notTranslatedYet = [];
|
||||
$todos = [];
|
||||
|
||||
$hasSomethingToWrite = false;
|
||||
foreach ($messages as $category => $msgs) {
|
||||
$msgs = array_values(array_unique($msgs));
|
||||
|
||||
@@ -420,8 +416,14 @@ EOD;
|
||||
sort($msgs);
|
||||
ksort($existingMessages);
|
||||
if (array_keys($existingMessages) == $msgs) {
|
||||
echo "Nothing new... skipped.\n";
|
||||
return self::EXIT_CODE_NORMAL;
|
||||
echo "Nothing new in \"$category\" category...\n";
|
||||
|
||||
sort($msgs);
|
||||
foreach ($msgs as $message) {
|
||||
$merged[$category . chr(4) . $message] = '';
|
||||
}
|
||||
ksort($merged);
|
||||
continue;
|
||||
}
|
||||
|
||||
// merge existing message translations with new message translations
|
||||
@@ -443,7 +445,7 @@ EOD;
|
||||
// add obsolete unused messages
|
||||
foreach ($existingMessages as $message => $translation) {
|
||||
if (!isset($merged[$category . chr(4) . $message]) && !isset($todos[$category . chr(4) . $message]) && !$removeUnused) {
|
||||
if (substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@') {
|
||||
if (mb_strlen($translation, Yii::$app->charset) >= 2 && substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@') {
|
||||
$todos[$category . chr(4) . $message] = $translation;
|
||||
} else {
|
||||
$todos[$category . chr(4) . $message] = '@@' . $translation . '@@';
|
||||
@@ -459,8 +461,6 @@ EOD;
|
||||
if ($overwrite === false) {
|
||||
$file .= '.merged';
|
||||
}
|
||||
|
||||
echo "Translation merged.\n";
|
||||
} else {
|
||||
sort($msgs);
|
||||
foreach ($msgs as $message) {
|
||||
@@ -468,8 +468,14 @@ EOD;
|
||||
}
|
||||
ksort($merged);
|
||||
}
|
||||
echo "Category \"$category\" merged.\n";
|
||||
$hasSomethingToWrite = true;
|
||||
}
|
||||
if ($hasSomethingToWrite) {
|
||||
$poFile->save($file, $merged);
|
||||
echo "Saved.\n";
|
||||
} else {
|
||||
echo "Nothing to save.\n";
|
||||
}
|
||||
$poFile->save($file, $merged);
|
||||
echo "Saved.\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,6 @@ return [
|
||||
// with the existing ones. Defaults to false, which means the new (untranslated)
|
||||
// messages will be separated from the old (translated) ones.
|
||||
'sort' => false,
|
||||
// boolean, whether the message file should be overwritten with the merged messages
|
||||
'overwrite' => true,
|
||||
// boolean, whether to remove messages that no longer appear in the source code.
|
||||
// Defaults to false, which means each of these messages will be enclosed with a pair of '@@' marks.
|
||||
'removeUnused' => false,
|
||||
@@ -47,6 +45,8 @@ return [
|
||||
'format' => 'php',
|
||||
// Root directory containing message translations.
|
||||
'messagePath' => __DIR__ . DIRECTORY_SEPARATOR . 'messages',
|
||||
// boolean, whether the message file should be overwritten with the merged messages
|
||||
'overwrite' => true,
|
||||
|
||||
|
||||
/*
|
||||
@@ -67,5 +67,7 @@ return [
|
||||
'messagePath' => __DIR__ . DIRECTORY_SEPARATOR . 'messages',
|
||||
// Name of the file that will be used for translations.
|
||||
'catalog' => 'messages',
|
||||
// boolean, whether the message file should be overwritten with the merged messages
|
||||
'overwrite' => true,
|
||||
*/
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user