Commit c3976b0f by Digimon

added possibility to save messages to be translated

parent 9b723baa
......@@ -16,8 +16,8 @@ use yii\helpers\FileHelper;
/**
* This command extracts messages to be translated from source files.
* 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.
* or ".po" files under the specified directory or to a database. Format
* depends on `format` setting in config file.
*
* Usage:
* 1. Create a configuration file using the 'message/config' command:
......@@ -77,13 +77,16 @@ class MessageController extends Controller
throw new Exception("The configuration file does not exist: $configFile");
}
$config = array_merge([
$config = array_merge(
[
'translator' => 'Yii::t',
'overwrite' => false,
'removeUnused' => false,
'sort' => false,
'format' => 'php',
], require($configFile));
],
require($configFile)
);
if (!isset($config['sourcePath'], $config['messagePath'], $config['languages'])) {
throw new Exception('The configuration file must specify "sourcePath", "messagePath" and "languages".');
......@@ -91,14 +94,16 @@ class MessageController extends Controller
if (!is_dir($config['sourcePath'])) {
throw new Exception("The source path {$config['sourcePath']} is not a valid directory.");
}
if (in_array($config['format'], ['php', 'po'])) {
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['format']) || !in_array($config['format'], ['php', 'po'])) {
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);
......@@ -107,7 +112,7 @@ class MessageController extends Controller
foreach ($files as $file) {
$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)) {
......@@ -120,7 +125,106 @@ class MessageController extends Controller
mkdir($path, 0755, true);
}
$msgs = array_values(array_unique($msgs));
$this->generateMessageFile($msgs, $file, $config['overwrite'], $config['removeUnused'], $config['sort'], $config['format']);
$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']);
}
}
}
}
/**
* @param $messages
* @param \yii\db\Connection $dbConnection
* @param $sourceMessageTable
* @param $removeUnused
*/
protected function saveMessagesToDb($messages, $dbConnection, $sourceMessageTable, $removeUnused)
{
$q = new \yii\db\Query;
$current = [];
foreach ($q->select(['id', 'category', 'message'])
->from($sourceMessageTable)
->all() as $row) {
$current[$row['category']][$row['id']] = $row['message'];
}
$new = [];
$obsoleted = [];
foreach ($messages as $category => $msgs) {
$msgs = array_unique($msgs);
if (isset($current[$category])) {
$new[$category] = array_diff($msgs, $current[$category]);
$obsoleted = array_diff($current[$category], $msgs);
} else {
$new[$category] = $msgs;
}
}
foreach (array_diff(array_keys($current), array_keys($messages)) as $category) {
$obsoleted += $current[$category];
}
if (!$removeUnused) {
foreach ($obsoleted as $pk => $m) {
if (substr($m, 0, 2) === '@@' && substr($m, -2) === '@@') {
unset($obsoleted[$pk]);
}
}
}
$obsoleted = array_keys($obsoleted);
echo "Inserting new messages...";
$savedFlag = false;
foreach ($new as $category => $msgs) {
foreach ($msgs as $m) {
$savedFlag = true;
$dbConnection->createCommand()
->insert($sourceMessageTable, ['category' => $category, 'message' => $m])->execute();
}
}
echo $savedFlag ? "saved.\n" : "nothing new...skipped.\n";
echo $removeUnused ? "Deleting obsoleted messages..." : "Updating obsoleted messages...";
if (empty($obsoleted)) {
echo "nothing obsoleted...skipped.\n";
} else {
if ($removeUnused) {
$dbConnection->createCommand()
->delete($sourceMessageTable, ['in', 'id', $obsoleted])->execute();
echo "deleted.\n";
} else {
$dbConnection->createCommand()
->update(
$sourceMessageTable,
['message' => new \yii\db\Expression("CONCAT('@@',message,'@@')")],
['in', 'id', $obsoleted]
)->execute();
echo "updated.\n";
}
}
}
......@@ -143,7 +247,10 @@ class MessageController extends Controller
foreach ($translator as $currentTranslator) {
$n = preg_match_all(
'/\b' . $currentTranslator . '\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s',
$subject, $matches, PREG_SET_ORDER);
$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);
......@@ -171,7 +278,7 @@ class MessageController extends Controller
{
echo "Saving messages to $fileName...";
if (is_file($fileName)) {
if($format === 'po'){
if ($format === 'po') {
$translated = file_get_contents($fileName);
preg_match_all('/(?<=msgid ").*(?="\n(#*)msgstr)/', $translated, $keys);
preg_match_all('/(?<=msgstr ").*(?="\n\n)/', $translated, $values);
......@@ -188,7 +295,7 @@ class MessageController extends Controller
$merged = [];
$untranslated = [];
foreach ($messages as $message) {
if($format === 'po'){
if ($format === 'po') {
$message = preg_replace('/\"/', '\"', $message);
}
if (array_key_exists($message, $translated) && strlen($translated[$message]) > 0) {
......@@ -220,9 +327,9 @@ class MessageController extends Controller
if (false === $overwrite) {
$fileName .= '.merged';
}
if ($format === 'po'){
if ($format === 'po') {
$out_str = '';
foreach ($merged as $k => $v){
foreach ($merged as $k => $v) {
$k = preg_replace('/(\")|(\\\")/', "\\\"", $k);
$v = preg_replace('/(\")|(\\\")/', "\\\"", $v);
if (substr($v, 0, 2) === '@@' && substr($v, -2) === '@@') {
......@@ -241,7 +348,7 @@ class MessageController extends Controller
if ($format === 'po') {
$merged = '';
sort($messages);
foreach($messages as $message) {
foreach ($messages as $message) {
$message = preg_replace('/(\")|(\\\")/', '\\\"', $message);
$merged .= "msgid \"$message\"\n";
$merged .= "msgstr \"\"\n";
......@@ -286,3 +393,4 @@ EOD;
file_put_contents($fileName, $content);
}
}
......@@ -22,7 +22,7 @@ use yii\db\Query;
*
* ~~~
* CREATE TABLE tbl_source_message (
* id INTEGER PRIMARY KEY,
* id INTEGER PRIMARY KEY AUTO_INCREMENT,
* category VARCHAR(32),
* message TEXT
* );
......
......@@ -42,6 +42,6 @@ return [
'.hgkeep',
'/messages',
],
// Generated file format. Can be either "php" or "po".
// Generated file format. Can be either "db", "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