Commit 1a14ed21 by Sorokin Victor

Update MessageController.php

parent 4d4a3f08
...@@ -134,14 +134,14 @@ class MessageController extends Controller ...@@ -134,14 +134,14 @@ class MessageController extends Controller
throw new Exception('The "db" option 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'] : '{{%source_message}}'; $sourceMessageTable = isset($config['sourceMessageTable']) ? $config['sourceMessageTable'] : '{{%source_message}}';
$messageTable = isset($config['messageTable']) ? $config['messageTable'] : '{{%message}}'; $messageTable = isset($config['messageTable']) ? $config['messageTable'] : '{{%message}}';
$this->saveMessagesToDb( $this->saveMessagesToDb(
$messages, $messages,
$db, $db,
$sourceMessageTable, $sourceMessageTable,
$messageTable, $messageTable,
$config['removeUnused'], $config['removeUnused'],
$config['languages'] $config['languages']
); );
} }
} }
...@@ -152,9 +152,9 @@ class MessageController extends Controller ...@@ -152,9 +152,9 @@ class MessageController extends Controller
* @param array $messages * @param array $messages
* @param \yii\db\Connection $db * @param \yii\db\Connection $db
* @param string $sourceMessageTable * @param string $sourceMessageTable
* @param string $messageTable * @param string $messageTable
* @param boolean $removeUnused * @param boolean $removeUnused
* @param array $languages * @param array $languages
*/ */
protected function saveMessagesToDb($messages, $db, $sourceMessageTable, $messageTable, $removeUnused, $languages) protected function saveMessagesToDb($messages, $db, $sourceMessageTable, $messageTable, $removeUnused, $languages)
{ {
...@@ -195,16 +195,16 @@ class MessageController extends Controller ...@@ -195,16 +195,16 @@ class MessageController extends Controller
echo "Inserting new messages..."; echo "Inserting new messages...";
$savedFlag = false; $savedFlag = false;
foreach ($new as $category => $msgs) { foreach ($new as $category => $msgs) {
foreach ($msgs as $m) { foreach ($msgs as $m) {
$savedFlag = true; $savedFlag = true;
$db->createCommand() $db->createCommand()
->insert($sourceMessageTable, ['category' => $category, 'message' => $m])->execute(); ->insert($sourceMessageTable, ['category' => $category, 'message' => $m])->execute();
$lastId = $db->getLastInsertID(); $lastId = $db->getLastInsertID();
foreach ($languages as $language) { foreach ($languages as $language) {
$db->createCommand() $db->createCommand()
->insert($messageTable, ['id' => $lastId, 'language' => $language])->execute(); ->insert($messageTable, ['id' => $lastId, 'language' => $language])->execute();
} }
} }
} }
...@@ -217,19 +217,19 @@ class MessageController extends Controller ...@@ -217,19 +217,19 @@ class MessageController extends Controller
} else { } else {
if ($removeUnused) { if ($removeUnused) {
$db->createCommand() $db->createCommand()
->delete($sourceMessageTable, ['in', 'id', $obsolete])->execute(); ->delete($sourceMessageTable, ['in', 'id', $obsolete])->execute();
echo "deleted.\n"; echo "deleted.\n";
} else { } else {
$last_id = $db->getLastInsertID(); $last_id = $db->getLastInsertID();
$db->createCommand() $db->createCommand()
->update( ->update(
$sourceMessageTable, $sourceMessageTable,
['message' => new \yii\db\Expression("CONCAT('@@',message,'@@')")], ['message' => new \yii\db\Expression("CONCAT('@@',message,'@@')")],
['in', 'id', $obsolete] ['in', 'id', $obsolete]
)->execute(); )->execute();
foreach ($languages as $language) { foreach ($languages as $language) {
$db->createCommand() $db->createCommand()
->insert($messageTable, ['id' => $last_id, 'language' => $language])->execute(); ->insert($messageTable, ['id' => $last_id, 'language' => $language])->execute();
} }
echo "updated.\n"; echo "updated.\n";
} }
...@@ -283,7 +283,7 @@ class MessageController extends Controller ...@@ -283,7 +283,7 @@ class MessageController extends Controller
{ {
echo "Saving messages to $fileName..."; echo "Saving messages to $fileName...";
if (is_file($fileName)) { if (is_file($fileName)) {
if($format === 'po'){ if ($format === 'po') {
$translated = file_get_contents($fileName); $translated = file_get_contents($fileName);
preg_match_all('/(?<=msgid ").*(?="\n(#*)msgstr)/', $translated, $keys); preg_match_all('/(?<=msgid ").*(?="\n(#*)msgstr)/', $translated, $keys);
preg_match_all('/(?<=msgstr ").*(?="\n\n)/', $translated, $values); preg_match_all('/(?<=msgstr ").*(?="\n\n)/', $translated, $values);
...@@ -300,7 +300,7 @@ class MessageController extends Controller ...@@ -300,7 +300,7 @@ class MessageController extends Controller
$merged = []; $merged = [];
$untranslated = []; $untranslated = [];
foreach ($messages as $message) { foreach ($messages as $message) {
if($format === 'po'){ if ($format === 'po') {
$message = preg_replace('/\"/', '\"', $message); $message = preg_replace('/\"/', '\"', $message);
} }
if (array_key_exists($message, $translated) && strlen($translated[$message]) > 0) { if (array_key_exists($message, $translated) && strlen($translated[$message]) > 0) {
...@@ -332,9 +332,9 @@ class MessageController extends Controller ...@@ -332,9 +332,9 @@ class MessageController extends Controller
if (false === $overwrite) { if (false === $overwrite) {
$fileName .= '.merged'; $fileName .= '.merged';
} }
if ($format === 'po'){ if ($format === 'po') {
$output = ''; $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) === '@@') {
...@@ -353,7 +353,7 @@ class MessageController extends Controller ...@@ -353,7 +353,7 @@ class MessageController extends Controller
if ($format === 'po') { if ($format === 'po') {
$merged = ''; $merged = '';
sort($messages); sort($messages);
foreach($messages as $message) { foreach ($messages as $message) {
$message = preg_replace('/(\")|(\\\")/', '\\\"', $message); $message = preg_replace('/(\")|(\\\")/', '\\\"', $message);
$merged .= "msgid \"$message\"\n"; $merged .= "msgid \"$message\"\n";
$merged .= "msgstr \"\"\n"; $merged .= "msgstr \"\"\n";
......
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