Commit f6e6ea99 by Alexander Makarov

Merge pull request #1973 from SergeiKutanov/po_generation

po file generating added.
parents 2aa8fc71 77776576
...@@ -69,7 +69,7 @@ class MessageController extends Controller ...@@ -69,7 +69,7 @@ class MessageController extends Controller
* this file and then customize it for your needs. * this file and then customize it for your needs.
* @throws Exception on failure. * @throws Exception on failure.
*/ */
public function actionExtract($configFile) public function actionExtract($configFile, $out=null)
{ {
$configFile = Yii::getAlias($configFile); $configFile = Yii::getAlias($configFile);
if (!is_file($configFile)) { if (!is_file($configFile)) {
...@@ -109,13 +109,21 @@ class MessageController extends Controller ...@@ -109,13 +109,21 @@ class MessageController extends Controller
@mkdir($dir); @mkdir($dir);
} }
foreach ($messages as $category => $msgs) { foreach ($messages as $category => $msgs) {
$file = str_replace("\\", '/', "$dir/$category.php"); if($out == 'po'){
$file = str_replace("\\", '/', "$dir/$category.po");
}else if($out == null){
$file = str_replace("\\", '/', "$dir/$category.php");
}
$path = dirname($file); $path = dirname($file);
if (!is_dir($path)) { if (!is_dir($path)) {
mkdir($path, 0755, true); mkdir($path, 0755, true);
} }
$msgs = array_values(array_unique($msgs)); $msgs = array_values(array_unique($msgs));
$this->generateMessageFile($msgs, $file, $config['overwrite'], $config['removeUnused'], $config['sort']); 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']);
}
} }
} }
} }
...@@ -161,11 +169,19 @@ class MessageController extends Controller ...@@ -161,11 +169,19 @@ class MessageController extends Controller
* @param boolean $removeUnused if obsolete translations should be removed * @param boolean $removeUnused if obsolete translations should be removed
* @param boolean $sort if translations should be sorted * @param boolean $sort if translations should be sorted
*/ */
protected function generateMessageFile($messages, $fileName, $overwrite, $removeUnused, $sort) protected function generateMessageFile($messages, $fileName, $overwrite, $removeUnused, $sort, $type=null)
{ {
echo "Saving messages to $fileName..."; echo "Saving messages to $fileName...";
if (is_file($fileName)) { if (is_file($fileName)) {
$translated = require($fileName); if($type == '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){
$translated = require($fileName);
}
sort($messages); sort($messages);
ksort($translated); ksort($translated);
if (array_keys($translated) == $messages) { if (array_keys($translated) == $messages) {
...@@ -204,17 +220,40 @@ class MessageController extends Controller ...@@ -204,17 +220,40 @@ class MessageController extends Controller
if (false === $overwrite) { if (false === $overwrite) {
$fileName .= '.merged'; $fileName .= '.merged';
} }
if($type == 'po'){
$out_str = '';
ksort($merged);
foreach($merged as $k=>$v){
$out_str .= "msgid \"$k\"\n";
$out_str .= "msgstr \"$v\"\n";
$out_str .= "\n";
}
$merged = $out_str;
}
echo "translation merged.\n"; echo "translation merged.\n";
} else { } else {
$merged = []; if($type == 'po'){
foreach ($messages as $message) { $merged = '';
$merged[$message] = ''; sort($messages);
} foreach($messages as $message){
ksort($merged); $merged .= "msgid \"$message\"\n";
$merged .= "msgstr \"\"\n";
$merged .= "\n";
}
}else if($type == null){
$merged = [];
foreach ($messages as $message) {
$merged[$message] = '';
}
ksort($merged);
}
echo "saved.\n"; echo "saved.\n";
} }
$array = str_replace("\r", '', var_export($merged, true)); if($type == 'po'){
$content = <<<EOD $content = $merged;
}else if($type == null){
$array = str_replace("\r", '', var_export($merged, true));
$content = <<<EOD
<?php <?php
/** /**
* Message translations. * Message translations.
...@@ -236,6 +275,7 @@ class MessageController extends Controller ...@@ -236,6 +275,7 @@ class MessageController extends Controller
return $array; return $array;
EOD; EOD;
}
file_put_contents($fileName, $content); file_put_contents($fileName, $content);
} }
} }
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