Commit edf98ced by Alexander Makarov

Fixes #1973: Re-implemented solution for `yii message/extract` is now able to generate `.po` files

parent f6e6ea99
......@@ -64,6 +64,7 @@ Yii Framework 2 Change Log
- Enh #1852: ActiveRecord::tableName() now returns table name using DbConnection::tablePrefix (creocoder)
- Enh #1894: The path aliases `@webroot` and `@web` are now available right after the application is initialized (qiangxue)
- Enh #1921: Grid view ActionColumn now allow to name buttons like `{controller/action}` (creocoder)
- Enh #1973: `yii message/extract` is now able to generate `.po` files (SergeiKutanov, samdark)
- Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark)
- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue)
- Enh: Support for file aliases in console command 'message' (omnilight)
......
......@@ -15,8 +15,9 @@ use yii\helpers\FileHelper;
/**
* This command extracts messages to be translated from source files.
* The extracted messages are saved as PHP message source files
* under the specified directory.
* The extracted messages are saved either as PHP message source files
* or ".po" files under the specified directory. Format depends on `format`
* setting in config file.
*
* Usage:
* 1. Create a configuration file using the 'message/config' command:
......@@ -69,7 +70,7 @@ class MessageController extends Controller
* this file and then customize it for your needs.
* @throws Exception on failure.
*/
public function actionExtract($configFile, $out=null)
public function actionExtract($configFile)
{
$configFile = Yii::getAlias($configFile);
if (!is_file($configFile)) {
......@@ -81,6 +82,7 @@ class MessageController extends Controller
'overwrite' => false,
'removeUnused' => false,
'sort' => false,
'format' => 'php',
], require($configFile));
if (!isset($config['sourcePath'], $config['messagePath'], $config['languages'])) {
......@@ -95,6 +97,9 @@ class MessageController extends Controller
if (empty($config['languages'])) {
throw new Exception("Languages cannot be empty.");
}
if (empty($config['format']) || !in_array($config['format'], ['php', 'po'])) {
throw new Exception('Format should be either "php" or "po".');
}
$files = FileHelper::findFiles(realpath($config['sourcePath']), $config);
......@@ -109,21 +114,13 @@ class MessageController extends Controller
@mkdir($dir);
}
foreach ($messages as $category => $msgs) {
if($out == 'po'){
$file = str_replace("\\", '/', "$dir/$category.po");
}else if($out == null){
$file = str_replace("\\", '/', "$dir/$category.php");
}
$file = str_replace("\\", '/', "$dir/$category." . $config['format']);
$path = dirname($file);
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
$msgs = array_values(array_unique($msgs));
if($out == 'po'){
$this->generateMessageFile($msgs, $file, $config['overwrite'], $config['removeUnused'], $config['sort'], $out);
}else if($out == null){
$this->generateMessageFile($msgs, $file, $config['overwrite'], $config['removeUnused'], $config['sort']);
}
$this->generateMessageFile($msgs, $file, $config['overwrite'], $config['removeUnused'], $config['sort'], $config['format']);
}
}
}
......@@ -168,18 +165,19 @@ 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 $format output format
*/
protected function generateMessageFile($messages, $fileName, $overwrite, $removeUnused, $sort, $type=null)
protected function generateMessageFile($messages, $fileName, $overwrite, $removeUnused, $sort, $format)
{
echo "Saving messages to $fileName...";
if (is_file($fileName)) {
if($type == 'po'){
if($format === 'po'){
$translated = file_get_contents($fileName);
preg_match_all('/(?<=msgid ").*(?="\nmsgstr)/', $translated, $keys);
preg_match_all('/(?<=msgstr ").*(?="\n\n)/', $translated, $values);
$translated = array_combine($keys[0], $values[0]);
}else if($type == null){
} else {
$translated = require($fileName);
}
sort($messages);
......@@ -220,9 +218,8 @@ class MessageController extends Controller
if (false === $overwrite) {
$fileName .= '.merged';
}
if($type == 'po'){
if($format === 'po'){
$out_str = '';
ksort($merged);
foreach($merged as $k=>$v){
$out_str .= "msgid \"$k\"\n";
$out_str .= "msgstr \"$v\"\n";
......@@ -232,15 +229,15 @@ class MessageController extends Controller
}
echo "translation merged.\n";
} else {
if($type == 'po'){
if ($format === 'po') {
$merged = '';
sort($messages);
foreach($messages as $message){
foreach($messages as $message) {
$merged .= "msgid \"$message\"\n";
$merged .= "msgstr \"\"\n";
$merged .= "\n";
}
}else if($type == null){
} else {
$merged = [];
foreach ($messages as $message) {
$merged[$message] = '';
......@@ -249,9 +246,9 @@ class MessageController extends Controller
}
echo "saved.\n";
}
if($type == 'po'){
if ($format === 'po') {
$content = $merged;
}else if($type == null){
} else {
$array = str_replace("\r", '', var_export($merged, true));
$content = <<<EOD
<?php
......
......@@ -42,4 +42,6 @@ return [
'.hgkeep',
'/messages',
],
// Generated file format. Can be either "php" or "po".
'format' => 'php',
];
......@@ -6,7 +6,7 @@ return [
// string, required, root directory containing message translations.
'messagePath' => __DIR__ . DIRECTORY_SEPARATOR . 'messages',
// array, required, list of language codes that the extracted messages
// should be translated to. For example, ['zh_cn', 'de'].
// should be translated to. For example, ['zh-CN', 'de'].
'languages' => ['de'],
// string, the name of the function for translating messages.
// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
......@@ -42,4 +42,6 @@ return [
'.hgkeep',
'/messages',
],
// Generated file format. Can be either "php" or "po".
'format' => 'php',
];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment