Commit 4fe0eeb1 by Qiang Xue

refacotring.

parent 92215a2f
...@@ -129,20 +129,14 @@ class MessageController extends Controller ...@@ -129,20 +129,14 @@ class MessageController extends Controller
} }
} }
} elseif ($config['format'] === 'db') { } elseif ($config['format'] === 'db') {
$dbConnection = \Yii::$app->getComponent(isset($config['connectionID']) ? $config['connectionID'] : 'db'); $db = \Yii::$app->getComponent(isset($config['db']) ? $config['db'] : 'db');
if (!$dbConnection instanceof \yii\db\Connection) { if (!$db instanceof \yii\db\Connection) {
throw new Exception('The "connectionID" must refer to a valid database application component.'); throw new Exception('The "db" option must refer to a valid database application component.');
}
$sourceMessageTable = isset($config['sourceMessageTable']) ? $config['sourceMessageTable'] : 'SourceMessage';
$res = [];
foreach ($config['languages'] as $language) {
foreach ($messages as $category => $msgs) {
$res[$category] = $msgs;
}
} }
$sourceMessageTable = isset($config['sourceMessageTable']) ? $config['sourceMessageTable'] : '{{%source_message}}';
$this->saveMessagesToDb( $this->saveMessagesToDb(
$res, $messages,
$dbConnection, $db,
$sourceMessageTable, $sourceMessageTable,
$config['removeUnused'] $config['removeUnused']
); );
...@@ -153,11 +147,11 @@ class MessageController extends Controller ...@@ -153,11 +147,11 @@ class MessageController extends Controller
* Saves messages to database * Saves messages to database
* *
* @param array $messages * @param array $messages
* @param \yii\db\Connection $dbConnection * @param \yii\db\Connection $db
* @param string $sourceMessageTable * @param string $sourceMessageTable
* @param boolean $removeUnused * @param boolean $removeUnused
*/ */
protected function saveMessagesToDb($messages, $dbConnection, $sourceMessageTable, $removeUnused) protected function saveMessagesToDb($messages, $db, $sourceMessageTable, $removeUnused)
{ {
$q = new \yii\db\Query; $q = new \yii\db\Query;
$current = []; $current = [];
...@@ -200,7 +194,7 @@ class MessageController extends Controller ...@@ -200,7 +194,7 @@ class MessageController extends Controller
foreach ($msgs as $m) { foreach ($msgs as $m) {
$savedFlag = true; $savedFlag = true;
$dbConnection->createCommand() $db->createCommand()
->insert($sourceMessageTable, ['category' => $category, 'message' => $m])->execute(); ->insert($sourceMessageTable, ['category' => $category, 'message' => $m])->execute();
} }
} }
...@@ -212,11 +206,11 @@ class MessageController extends Controller ...@@ -212,11 +206,11 @@ class MessageController extends Controller
echo "nothing obsoleted...skipped.\n"; echo "nothing obsoleted...skipped.\n";
} else { } else {
if ($removeUnused) { if ($removeUnused) {
$dbConnection->createCommand() $db->createCommand()
->delete($sourceMessageTable, ['in', 'id', $obsolete])->execute(); ->delete($sourceMessageTable, ['in', 'id', $obsolete])->execute();
echo "deleted.\n"; echo "deleted.\n";
} else { } else {
$dbConnection->createCommand() $db->createCommand()
->update( ->update(
$sourceMessageTable, $sourceMessageTable,
['message' => new \yii\db\Expression("CONCAT('@@',message,'@@')")], ['message' => new \yii\db\Expression("CONCAT('@@',message,'@@')")],
...@@ -324,20 +318,20 @@ class MessageController extends Controller ...@@ -324,20 +318,20 @@ class MessageController extends Controller
$fileName .= '.merged'; $fileName .= '.merged';
} }
if ($format === 'po'){ if ($format === 'po'){
$out_str = ''; $output = '';
foreach ($merged as $k => $v){ foreach ($merged as $k => $v){
$k = preg_replace('/(\")|(\\\")/', "\\\"", $k); $k = preg_replace('/(\")|(\\\")/', "\\\"", $k);
$v = preg_replace('/(\")|(\\\")/', "\\\"", $v); $v = preg_replace('/(\")|(\\\")/', "\\\"", $v);
if (substr($v, 0, 2) === '@@' && substr($v, -2) === '@@') { if (substr($v, 0, 2) === '@@' && substr($v, -2) === '@@') {
$out_str .= "#msgid \"$k\"\n"; $output .= "#msgid \"$k\"\n";
$out_str .= "#msgstr \"$v\"\n"; $output .= "#msgstr \"$v\"\n";
} else { } else {
$out_str .= "msgid \"$k\"\n"; $output .= "msgid \"$k\"\n";
$out_str .= "msgstr \"$v\"\n"; $output .= "msgstr \"$v\"\n";
} }
$out_str .= "\n"; $output .= "\n";
} }
$merged = $out_str; $merged = $output;
} }
echo "translation merged.\n"; echo "translation merged.\n";
} else { } else {
......
...@@ -44,6 +44,7 @@ return [ ...@@ -44,6 +44,7 @@ return [
], ],
// Generated file format. Can be either "php", "po" or "db". // Generated file format. Can be either "php", "po" or "db".
'format' => 'php', 'format' => 'php',
// Connection component ID for "db". // When format is "db", you may specify the following two options
//'connectionID' => 'db', //'db' => 'db',
//'sourceMessageTable' => '{{%source_message}}',
]; ];
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