Commit 77776576 by SergeiKutanov

po file generatin added.

Files are generated using message/extract command. E.g.: ./yii message/extract message/config.php po
parent cbb39d19
...@@ -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,16 +109,24 @@ class MessageController extends Controller ...@@ -109,16 +109,24 @@ class MessageController extends Controller
@mkdir($dir); @mkdir($dir);
} }
foreach ($messages as $category => $msgs) { 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.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));
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']);
} }
} }
} }
}
/** /**
* Extracts messages from a file * Extracts messages from a file
...@@ -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)) {
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); $translated = require($fileName);
}
sort($messages); sort($messages);
ksort($translated); ksort($translated);
if (array_keys($translated) == $messages) { if (array_keys($translated) == $messages) {
...@@ -204,15 +220,38 @@ class MessageController extends Controller ...@@ -204,15 +220,38 @@ 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 {
if($type == 'po'){
$merged = '';
sort($messages);
foreach($messages as $message){
$merged .= "msgid \"$message\"\n";
$merged .= "msgstr \"\"\n";
$merged .= "\n";
}
}else if($type == null){
$merged = []; $merged = [];
foreach ($messages as $message) { foreach ($messages as $message) {
$merged[$message] = ''; $merged[$message] = '';
} }
ksort($merged); ksort($merged);
}
echo "saved.\n"; echo "saved.\n";
} }
if($type == 'po'){
$content = $merged;
}else if($type == null){
$array = str_replace("\r", '', var_export($merged, true)); $array = str_replace("\r", '', var_export($merged, true));
$content = <<<EOD $content = <<<EOD
<?php <?php
...@@ -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