Commit c3976b0f by Digimon

added possibility to save messages to be translated

parent 9b723baa
...@@ -16,8 +16,8 @@ use yii\helpers\FileHelper; ...@@ -16,8 +16,8 @@ use yii\helpers\FileHelper;
/** /**
* This command extracts messages to be translated from source files. * This command extracts messages to be translated from source files.
* The extracted messages are saved either as PHP message source files * The extracted messages are saved either as PHP message source files
* or ".po" files under the specified directory. Format depends on `format` * or ".po" files under the specified directory or to a database. Format
* setting in config file. * depends on `format` setting in config file.
* *
* Usage: * Usage:
* 1. Create a configuration file using the 'message/config' command: * 1. Create a configuration file using the 'message/config' command:
...@@ -31,236 +31,343 @@ use yii\helpers\FileHelper; ...@@ -31,236 +31,343 @@ use yii\helpers\FileHelper;
*/ */
class MessageController extends Controller class MessageController extends Controller
{ {
/** /**
* @var string controller default action ID. * @var string controller default action ID.
*/ */
public $defaultAction = 'extract'; public $defaultAction = 'extract';
/** /**
* Creates a configuration file for the "extract" command. * Creates a configuration file for the "extract" command.
* *
* The generated configuration file contains detailed instructions on * The generated configuration file contains detailed instructions on
* how to customize it to fit for your needs. After customization, * how to customize it to fit for your needs. After customization,
* you may use this configuration file with the "extract" command. * you may use this configuration file with the "extract" command.
* *
* @param string $filePath output file name or alias. * @param string $filePath output file name or alias.
* @throws Exception on failure. * @throws Exception on failure.
*/ */
public function actionConfig($filePath) public function actionConfig($filePath)
{ {
$filePath = Yii::getAlias($filePath); $filePath = Yii::getAlias($filePath);
if (file_exists($filePath)) { if (file_exists($filePath)) {
if (!$this->confirm("File '{$filePath}' already exists. Do you wish to overwrite it?")) { if (!$this->confirm("File '{$filePath}' already exists. Do you wish to overwrite it?")) {
return; return;
} }
} }
copy(Yii::getAlias('@yii/views/messageConfig.php'), $filePath); copy(Yii::getAlias('@yii/views/messageConfig.php'), $filePath);
echo "Configuration file template created at '{$filePath}'.\n\n"; echo "Configuration file template created at '{$filePath}'.\n\n";
} }
/** /**
* Extracts messages to be translated from source code. * Extracts messages to be translated from source code.
* *
* This command will search through source code files and extract * This command will search through source code files and extract
* messages that need to be translated in different languages. * messages that need to be translated in different languages.
* *
* @param string $configFile the path or alias of the configuration file. * @param string $configFile the path or alias of the configuration file.
* You may use the "yii message/config" command to generate * You may use the "yii message/config" command to generate
* 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)
{ {
$configFile = Yii::getAlias($configFile); $configFile = Yii::getAlias($configFile);
if (!is_file($configFile)) { if (!is_file($configFile)) {
throw new Exception("The configuration file does not exist: $configFile"); throw new Exception("The configuration file does not exist: $configFile");
} }
$config = array_merge([ $config = array_merge(
'translator' => 'Yii::t', [
'overwrite' => false, 'translator' => 'Yii::t',
'removeUnused' => false, 'overwrite' => false,
'sort' => false, 'removeUnused' => false,
'format' => 'php', 'sort' => false,
], require($configFile)); 'format' => 'php',
],
require($configFile)
);
if (!isset($config['sourcePath'], $config['messagePath'], $config['languages'])) { if (!isset($config['sourcePath'], $config['messagePath'], $config['languages'])) {
throw new Exception('The configuration file must specify "sourcePath", "messagePath" and "languages".'); throw new Exception('The configuration file must specify "sourcePath", "messagePath" and "languages".');
} }
if (!is_dir($config['sourcePath'])) { if (!is_dir($config['sourcePath'])) {
throw new Exception("The source path {$config['sourcePath']} is not a valid directory."); throw new Exception("The source path {$config['sourcePath']} is not a valid directory.");
} }
if (!is_dir($config['messagePath'])) { if (in_array($config['format'], ['php', 'po'])) {
throw new Exception("The message path {$config['messagePath']} is not a valid directory."); if (!is_dir($config['messagePath'])) {
} throw new Exception("The message path {$config['messagePath']} is not a valid directory.");
if (empty($config['languages'])) { }
throw new Exception("Languages cannot be empty."); }
} if (empty($config['languages'])) {
if (empty($config['format']) || !in_array($config['format'], ['php', 'po'])) { throw new Exception("Languages cannot be empty.");
throw new Exception('Format should be either "php" or "po".'); }
} if (empty($config['format']) || !in_array($config['format'], ['php', 'po', 'db'])) {
throw new Exception('Format should be either "php", "po" or "db".');
}
$files = FileHelper::findFiles(realpath($config['sourcePath']), $config); $files = FileHelper::findFiles(realpath($config['sourcePath']), $config);
$messages = []; $messages = [];
foreach ($files as $file) { foreach ($files as $file) {
$messages = array_merge_recursive($messages, $this->extractMessages($file, $config['translator'])); $messages = array_merge_recursive($messages, $this->extractMessages($file, $config['translator']));
} }
if (in_array($config['format'], ['php', 'po'])) {
foreach ($config['languages'] as $language) {
$dir = $config['messagePath'] . DIRECTORY_SEPARATOR . $language;
if (!is_dir($dir)) {
@mkdir($dir);
}
foreach ($messages as $category => $msgs) {
$file = str_replace("\\", '/', "$dir/$category." . $config['format']);
$path = dirname($file);
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
$msgs = array_values(array_unique($msgs));
$this->generateMessageFile(
$msgs,
$file,
$config['overwrite'],
$config['removeUnused'],
$config['sort'],
$config['format']
);
}
}
}
if ($config['format'] === 'db') {
$dbConnection = \Yii::$app->getComponent(isset($config['connectionID']) ? $config['connectionID'] : 'db');
if (!$dbConnection instanceof \yii\db\Connection) {
$this->usageError('The "connectionID" must refer to a valid database application component.');
}
$sourceMessageTable = !isset($config['sourceMessageTable']) ? 'SourceMessage' : $config['sourceMessageTable'];
foreach ($config['languages'] as $language) {
foreach ($messages as $category => $msgs) {
$messages[$category] = array_values(array_unique($msgs));
$this->saveMessagesToDb($messages, $dbConnection, $sourceMessageTable, $config['removeUnused']);
}
}
}
}
foreach ($config['languages'] as $language) { /**
$dir = $config['messagePath'] . DIRECTORY_SEPARATOR . $language; * @param $messages
if (!is_dir($dir)) { * @param \yii\db\Connection $dbConnection
@mkdir($dir); * @param $sourceMessageTable
} * @param $removeUnused
foreach ($messages as $category => $msgs) { */
$file = str_replace("\\", '/', "$dir/$category." . $config['format']); protected function saveMessagesToDb($messages, $dbConnection, $sourceMessageTable, $removeUnused)
$path = dirname($file); {
if (!is_dir($path)) { $q = new \yii\db\Query;
mkdir($path, 0755, true); $current = [];
}
$msgs = array_values(array_unique($msgs));
$this->generateMessageFile($msgs, $file, $config['overwrite'], $config['removeUnused'], $config['sort'], $config['format']);
}
}
}
/** foreach ($q->select(['id', 'category', 'message'])
* Extracts messages from a file ->from($sourceMessageTable)
* ->all() as $row) {
* @param string $fileName name of the file to extract messages from $current[$row['category']][$row['id']] = $row['message'];
* @param string $translator name of the function used to translate messages }
* @return array
*/
protected function extractMessages($fileName, $translator)
{
echo "Extracting messages from $fileName...\n";
$subject = file_get_contents($fileName);
$messages = [];
if (!is_array($translator)) {
$translator = [$translator];
}
foreach ($translator as $currentTranslator) {
$n = preg_match_all(
'/\b' . $currentTranslator . '\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s',
$subject, $matches, PREG_SET_ORDER);
for ($i = 0; $i < $n; ++$i) {
if (($pos = strpos($matches[$i][1], '.')) !== false) {
$category = substr($matches[$i][1], $pos + 1, -1);
} else {
$category = substr($matches[$i][1], 1, -1);
}
$message = $matches[$i][2];
$messages[$category][] = eval("return $message;"); // use eval to eliminate quote escape
}
}
return $messages;
}
/** $new = [];
* Writes messages into file $obsoleted = [];
*
* @param array $messages foreach ($messages as $category => $msgs) {
* @param string $fileName name of the file to write to $msgs = array_unique($msgs);
* @param boolean $overwrite if existing file should be overwritten without backup
* @param boolean $removeUnused if obsolete translations should be removed if (isset($current[$category])) {
* @param boolean $sort if translations should be sorted $new[$category] = array_diff($msgs, $current[$category]);
* @param string $format output format $obsoleted = array_diff($current[$category], $msgs);
*/ } else {
protected function generateMessageFile($messages, $fileName, $overwrite, $removeUnused, $sort, $format) $new[$category] = $msgs;
{ }
echo "Saving messages to $fileName..."; }
if (is_file($fileName)) {
if($format === 'po'){ foreach (array_diff(array_keys($current), array_keys($messages)) as $category) {
$translated = file_get_contents($fileName); $obsoleted += $current[$category];
preg_match_all('/(?<=msgid ").*(?="\n(#*)msgstr)/', $translated, $keys); }
preg_match_all('/(?<=msgstr ").*(?="\n\n)/', $translated, $values);
$translated = array_combine($keys[0], $values[0]); if (!$removeUnused) {
} else { foreach ($obsoleted as $pk => $m) {
$translated = require($fileName); if (substr($m, 0, 2) === '@@' && substr($m, -2) === '@@') {
} unset($obsoleted[$pk]);
sort($messages); }
ksort($translated); }
if (array_keys($translated) == $messages) { }
echo "nothing new...skipped.\n";
return; $obsoleted = array_keys($obsoleted);
} echo "Inserting new messages...";
$merged = []; $savedFlag = false;
$untranslated = [];
foreach ($messages as $message) { foreach ($new as $category => $msgs) {
if($format === 'po'){ foreach ($msgs as $m) {
$message = preg_replace('/\"/', '\"', $message); $savedFlag = true;
}
if (array_key_exists($message, $translated) && strlen($translated[$message]) > 0) { $dbConnection->createCommand()
$merged[$message] = $translated[$message]; ->insert($sourceMessageTable, ['category' => $category, 'message' => $m])->execute();
} else { }
$untranslated[] = $message; }
}
} echo $savedFlag ? "saved.\n" : "nothing new...skipped.\n";
ksort($merged); echo $removeUnused ? "Deleting obsoleted messages..." : "Updating obsoleted messages...";
sort($untranslated);
$todo = []; if (empty($obsoleted)) {
foreach ($untranslated as $message) { echo "nothing obsoleted...skipped.\n";
$todo[$message] = ''; } else {
} if ($removeUnused) {
ksort($translated); $dbConnection->createCommand()
foreach ($translated as $message => $translation) { ->delete($sourceMessageTable, ['in', 'id', $obsoleted])->execute();
if (!isset($merged[$message]) && !isset($todo[$message]) && !$removeUnused) { echo "deleted.\n";
if (substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@') { } else {
$todo[$message] = $translation; $dbConnection->createCommand()
} else { ->update(
$todo[$message] = '@@' . $translation . '@@'; $sourceMessageTable,
} ['message' => new \yii\db\Expression("CONCAT('@@',message,'@@')")],
} ['in', 'id', $obsoleted]
} )->execute();
$merged = array_merge($todo, $merged); echo "updated.\n";
if ($sort) { }
ksort($merged); }
} }
if (false === $overwrite) {
$fileName .= '.merged'; /**
} * Extracts messages from a file
if ($format === 'po'){ *
$out_str = ''; * @param string $fileName name of the file to extract messages from
foreach ($merged as $k => $v){ * @param string $translator name of the function used to translate messages
$k = preg_replace('/(\")|(\\\")/', "\\\"", $k); * @return array
$v = preg_replace('/(\")|(\\\")/', "\\\"", $v); */
if (substr($v, 0, 2) === '@@' && substr($v, -2) === '@@') { protected function extractMessages($fileName, $translator)
$out_str .= "#msgid \"$k\"\n"; {
$out_str .= "#msgstr \"$v\"\n"; echo "Extracting messages from $fileName...\n";
} else { $subject = file_get_contents($fileName);
$out_str .= "msgid \"$k\"\n"; $messages = [];
$out_str .= "msgstr \"$v\"\n"; if (!is_array($translator)) {
} $translator = [$translator];
$out_str .= "\n"; }
} foreach ($translator as $currentTranslator) {
$merged = $out_str; $n = preg_match_all(
} '/\b' . $currentTranslator . '\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s',
echo "translation merged.\n"; $subject,
} else { $matches,
if ($format === 'po') { PREG_SET_ORDER
$merged = ''; );
sort($messages); for ($i = 0; $i < $n; ++$i) {
foreach($messages as $message) { if (($pos = strpos($matches[$i][1], '.')) !== false) {
$message = preg_replace('/(\")|(\\\")/', '\\\"', $message); $category = substr($matches[$i][1], $pos + 1, -1);
$merged .= "msgid \"$message\"\n"; } else {
$merged .= "msgstr \"\"\n"; $category = substr($matches[$i][1], 1, -1);
$merged .= "\n"; }
} $message = $matches[$i][2];
} else { $messages[$category][] = eval("return $message;"); // use eval to eliminate quote escape
$merged = []; }
foreach ($messages as $message) { }
$merged[$message] = ''; return $messages;
} }
ksort($merged);
} /**
echo "saved.\n"; * Writes messages into file
} *
if ($format === 'po') { * @param array $messages
$content = $merged; * @param string $fileName name of the file to write to
} else { * @param boolean $overwrite if existing file should be overwritten without backup
$array = str_replace("\r", '', var_export($merged, true)); * @param boolean $removeUnused if obsolete translations should be removed
$content = <<<EOD * @param boolean $sort if translations should be sorted
* @param string $format output format
*/
protected function generateMessageFile($messages, $fileName, $overwrite, $removeUnused, $sort, $format)
{
echo "Saving messages to $fileName...";
if (is_file($fileName)) {
if ($format === 'po') {
$translated = file_get_contents($fileName);
preg_match_all('/(?<=msgid ").*(?="\n(#*)msgstr)/', $translated, $keys);
preg_match_all('/(?<=msgstr ").*(?="\n\n)/', $translated, $values);
$translated = array_combine($keys[0], $values[0]);
} else {
$translated = require($fileName);
}
sort($messages);
ksort($translated);
if (array_keys($translated) == $messages) {
echo "nothing new...skipped.\n";
return;
}
$merged = [];
$untranslated = [];
foreach ($messages as $message) {
if ($format === 'po') {
$message = preg_replace('/\"/', '\"', $message);
}
if (array_key_exists($message, $translated) && strlen($translated[$message]) > 0) {
$merged[$message] = $translated[$message];
} else {
$untranslated[] = $message;
}
}
ksort($merged);
sort($untranslated);
$todo = [];
foreach ($untranslated as $message) {
$todo[$message] = '';
}
ksort($translated);
foreach ($translated as $message => $translation) {
if (!isset($merged[$message]) && !isset($todo[$message]) && !$removeUnused) {
if (substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@') {
$todo[$message] = $translation;
} else {
$todo[$message] = '@@' . $translation . '@@';
}
}
}
$merged = array_merge($todo, $merged);
if ($sort) {
ksort($merged);
}
if (false === $overwrite) {
$fileName .= '.merged';
}
if ($format === 'po') {
$out_str = '';
foreach ($merged as $k => $v) {
$k = preg_replace('/(\")|(\\\")/', "\\\"", $k);
$v = preg_replace('/(\")|(\\\")/', "\\\"", $v);
if (substr($v, 0, 2) === '@@' && substr($v, -2) === '@@') {
$out_str .= "#msgid \"$k\"\n";
$out_str .= "#msgstr \"$v\"\n";
} else {
$out_str .= "msgid \"$k\"\n";
$out_str .= "msgstr \"$v\"\n";
}
$out_str .= "\n";
}
$merged = $out_str;
}
echo "translation merged.\n";
} else {
if ($format === 'po') {
$merged = '';
sort($messages);
foreach ($messages as $message) {
$message = preg_replace('/(\")|(\\\")/', '\\\"', $message);
$merged .= "msgid \"$message\"\n";
$merged .= "msgstr \"\"\n";
$merged .= "\n";
}
} else {
$merged = [];
foreach ($messages as $message) {
$merged[$message] = '';
}
ksort($merged);
}
echo "saved.\n";
}
if ($format === 'po') {
$content = $merged;
} else {
$array = str_replace("\r", '', var_export($merged, true));
$content = <<<EOD
<?php <?php
/** /**
* Message translations. * Message translations.
...@@ -282,7 +389,8 @@ class MessageController extends Controller ...@@ -282,7 +389,8 @@ class MessageController extends Controller
return $array; return $array;
EOD; EOD;
} }
file_put_contents($fileName, $content); file_put_contents($fileName, $content);
} }
} }
...@@ -22,7 +22,7 @@ use yii\db\Query; ...@@ -22,7 +22,7 @@ use yii\db\Query;
* *
* ~~~ * ~~~
* CREATE TABLE tbl_source_message ( * CREATE TABLE tbl_source_message (
* id INTEGER PRIMARY KEY, * id INTEGER PRIMARY KEY AUTO_INCREMENT,
* category VARCHAR(32), * category VARCHAR(32),
* message TEXT * message TEXT
* ); * );
......
...@@ -42,6 +42,6 @@ return [ ...@@ -42,6 +42,6 @@ return [
'.hgkeep', '.hgkeep',
'/messages', '/messages',
], ],
// Generated file format. Can be either "php" or "po". // Generated file format. Can be either "db", "php" or "po".
'format' => 'php', '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