Created
September 1, 2014 13:19
-
-
Save stepanselyuk/39f167b973648bbf1fd2 to your computer and use it in GitHub Desktop.
Collect strings for translation from *.js files
Generate JSON resources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* Author: Stepan Seliuk <stepan@selyuk.com> | |
* Date: 30/08/14 | |
* Time: 20:29 | |
*/ | |
namespace app\commands\system; | |
use app\components\helpers\DirHelper; | |
use app\components\yiiExt\ConsoleController; | |
use app\models\AppSettingsRecord; | |
use app\models\TextMessage; | |
use app\models\TextSourceMessage; | |
use yii\helpers\FileHelper; | |
use yii\helpers\Json; | |
/** | |
* Class JsTranslationsController | |
* @package app\commands\system | |
*/ | |
class JsTranslationsController extends ConsoleController | |
{ | |
protected $messages = [ ]; | |
public $languages = [ 'en-US', 'fr-CA' ]; | |
protected $dir = '@runtime/assets/generated'; | |
public function actionCollect() | |
{ | |
/** | |
* Получаем все *.js файлы проекта (исключая каталоги web, vendors) | |
*/ | |
$list = FileHelper::findFiles( | |
\Yii::$app->basePath, | |
[ | |
'only' => [ '*.js' ], | |
'except' => [ '/vendor/', '/web/', '_cache/', '/resources/composer' ] | |
] | |
); | |
$this->logInfo( __( 'Найдено {n} файлов.', [ 'n' => count( $list ) ] ) ); | |
foreach ($list as $path) { | |
$this->logTrace( '.', true ); | |
$this->getMessages( $path ); | |
} | |
foreach ($this->messages as $msg) { | |
$this->logProfile( __( 'Обработка сообщения: "{message}".', [ 'message' => $msg ] ) ); | |
$md5 = md5( $msg ); | |
$source = TextSourceMessage::findOne( [ 'msghash' => $md5, 'category' => 0 ] ); | |
// Если исходного сообщения нет, то добавляем его | |
if (is_null( $source )) { | |
$sM = new TextSourceMessage(); | |
$sM->category = 0; | |
$sM->message = $msg; | |
$sM->save(); | |
// Автоматический поиск уже переведенных сообщений, | |
// с таким же хешем, но в других категориях. | |
$sM->autoCheckAndInsertTranslations(); | |
} | |
} | |
} | |
protected function getMessages( $filepath ) | |
{ | |
$data = file_get_contents( $filepath ); | |
$tpl = '/\bMyApp.tt\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s'; | |
$n = preg_match_all( $tpl, $data, $matches, PREG_SET_ORDER ); | |
for ($i = 0; $i < $n; ++$i) { | |
$message = $matches[ $i ][ 1 ]; | |
$this->messages[ ] = eval( "return $message;" ); // use eval to eliminate quote escape | |
$this->logTrace( '+', true ); | |
} | |
} | |
public function actionGenerateJson() | |
{ | |
// Поиск сообщений с категорией 0 | |
$sources = TextSourceMessage::findAll( [ 'category' => 0 ] ); | |
foreach ($this->languages as $lang) { | |
$this->_generateFile( $lang, $sources ); | |
} | |
} | |
protected function _generateFile( $lang, $sources ) | |
{ | |
$this->logInfo( __( 'Генерация файла ресурсов для локализации {lang}', [ 'lang' => $lang ] ) ); | |
DirHelper::create( $this->dir ); | |
$file = \Yii::getAlias( $this->dir . '/' . $lang . '.messages.json' ); | |
$data = [ | |
'params' => [ | |
'lang' => $lang, | |
'source_lang' => \Yii::$app->sourceLanguage, | |
'currency' => AppSettingsRecord::getValue( 'system.currency' ) | |
], | |
'messages' => [ ] | |
]; | |
/** @var TextSourceMessage[] $sources */ | |
foreach ($sources as $sm) { | |
/** @var TextMessage $message */ | |
$message = TextMessage::findOne( [ 'source_message_id' => $sm->getPrimaryKey(), 'language' => $lang ] ); | |
if (!is_null( $message )) { | |
$data[ 'messages' ][ $sm->message ] = $message->translation; | |
$this->logTrace( '+', true ); | |
} else { | |
$this->logTrace( '.', true ); | |
} | |
} | |
$json = Json::encode( $data ); | |
file_put_contents( $file, $json ); | |
$this->logInfo( __( 'Сообщения сохранены в файл {file}', [ 'file' => $file ] ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment