MessageController.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\console\controllers;
  8. use Yii;
  9. use yii\console\Exception;
  10. use yii\db\Connection;
  11. use yii\db\Query;
  12. use yii\di\Instance;
  13. use yii\helpers\Console;
  14. use yii\helpers\FileHelper;
  15. use yii\helpers\VarDumper;
  16. use yii\i18n\GettextPoFile;
  17. /**
  18. * Extracts messages to be translated from source files.
  19. *
  20. * The extracted messages can be saved the following depending on `format`
  21. * setting in config file:
  22. *
  23. * - PHP message source files.
  24. * - ".po" files.
  25. * - Database.
  26. *
  27. * Usage:
  28. * 1. Create a configuration file using the 'message/config' command:
  29. * yii message/config /path/to/myapp/messages/config.php
  30. * 2. Edit the created config file, adjusting it for your web application needs.
  31. * 3. Run the 'message/extract' command, using created config:
  32. * yii message /path/to/myapp/messages/config.php
  33. *
  34. * @author Qiang Xue <qiang.xue@gmail.com>
  35. * @since 2.0
  36. */
  37. class MessageController extends \yii\console\Controller
  38. {
  39. /**
  40. * @var string controller default action ID.
  41. */
  42. public $defaultAction = 'extract';
  43. /**
  44. * @var string required, root directory of all source files.
  45. */
  46. public $sourcePath = '@yii';
  47. /**
  48. * @var string required, root directory containing message translations.
  49. */
  50. public $messagePath = '@yii/messages';
  51. /**
  52. * @var array required, list of language codes that the extracted messages
  53. * should be translated to. For example, ['zh-CN', 'de'].
  54. */
  55. public $languages = [];
  56. /**
  57. * @var string the name of the function for translating messages.
  58. * Defaults to 'Yii::t'. This is used as a mark to find the messages to be
  59. * translated. You may use a string for single function name or an array for
  60. * multiple function names.
  61. */
  62. public $translator = 'Yii::t';
  63. /**
  64. * @var bool whether to sort messages by keys when merging new messages
  65. * with the existing ones. Defaults to false, which means the new (untranslated)
  66. * messages will be separated from the old (translated) ones.
  67. */
  68. public $sort = false;
  69. /**
  70. * @var bool whether the message file should be overwritten with the merged messages
  71. */
  72. public $overwrite = true;
  73. /**
  74. * @var bool whether to remove messages that no longer appear in the source code.
  75. * Defaults to false, which means these messages will NOT be removed.
  76. */
  77. public $removeUnused = false;
  78. /**
  79. * @var bool whether to mark messages that no longer appear in the source code.
  80. * Defaults to true, which means each of these messages will be enclosed with a pair of '@@' marks.
  81. */
  82. public $markUnused = true;
  83. /**
  84. * @var array list of patterns that specify which files/directories should NOT be processed.
  85. * If empty or not set, all files/directories will be processed.
  86. * See helpers/FileHelper::findFiles() description for pattern matching rules.
  87. * If a file/directory matches both a pattern in "only" and "except", it will NOT be processed.
  88. */
  89. public $except = [
  90. '.svn',
  91. '.git',
  92. '.gitignore',
  93. '.gitkeep',
  94. '.hgignore',
  95. '.hgkeep',
  96. '/messages',
  97. '/BaseYii.php', // contains examples about Yii:t()
  98. ];
  99. /**
  100. * @var array list of patterns that specify which files (not directories) should be processed.
  101. * If empty or not set, all files will be processed.
  102. * See helpers/FileHelper::findFiles() description for pattern matching rules.
  103. * If a file/directory matches both a pattern in "only" and "except", it will NOT be processed.
  104. */
  105. public $only = ['*.php'];
  106. /**
  107. * @var string generated file format. Can be "php", "db", "po" or "pot".
  108. */
  109. public $format = 'php';
  110. /**
  111. * @var string connection component ID for "db" format.
  112. */
  113. public $db = 'db';
  114. /**
  115. * @var string custom name for source message table for "db" format.
  116. */
  117. public $sourceMessageTable = '{{%source_message}}';
  118. /**
  119. * @var string custom name for translation message table for "db" format.
  120. */
  121. public $messageTable = '{{%message}}';
  122. /**
  123. * @var string name of the file that will be used for translations for "po" format.
  124. */
  125. public $catalog = 'messages';
  126. /**
  127. * @var array message categories to ignore. For example, 'yii', 'app*', 'widgets/menu', etc.
  128. * @see isCategoryIgnored
  129. */
  130. public $ignoreCategories = [];
  131. /**
  132. * @inheritdoc
  133. */
  134. public function options($actionID)
  135. {
  136. return array_merge(parent::options($actionID), [
  137. 'sourcePath',
  138. 'messagePath',
  139. 'languages',
  140. 'translator',
  141. 'sort',
  142. 'overwrite',
  143. 'removeUnused',
  144. 'markUnused',
  145. 'except',
  146. 'only',
  147. 'format',
  148. 'db',
  149. 'sourceMessageTable',
  150. 'messageTable',
  151. 'catalog',
  152. 'ignoreCategories',
  153. ]);
  154. }
  155. /**
  156. * @inheritdoc
  157. * @since 2.0.8
  158. */
  159. public function optionAliases()
  160. {
  161. return array_merge(parent::optionAliases(), [
  162. 'c' => 'catalog',
  163. 'e' => 'except',
  164. 'f' => 'format',
  165. 'i' => 'ignoreCategories',
  166. 'l' => 'languages',
  167. 'u' => 'markUnused',
  168. 'p' => 'messagePath',
  169. 'o' => 'only',
  170. 'w' => 'overwrite',
  171. 'S' => 'sort',
  172. 't' => 'translator',
  173. 'm' => 'sourceMessageTable',
  174. 's' => 'sourcePath',
  175. 'r' => 'removeUnused',
  176. ]);
  177. }
  178. /**
  179. * Creates a configuration file for the "extract" command using command line options specified
  180. *
  181. * The generated configuration file contains parameters required
  182. * for source code messages extraction.
  183. * You may use this configuration file with the "extract" command.
  184. *
  185. * @param string $filePath output file name or alias.
  186. * @return int CLI exit code
  187. * @throws Exception on failure.
  188. */
  189. public function actionConfig($filePath)
  190. {
  191. $filePath = Yii::getAlias($filePath);
  192. if (file_exists($filePath)) {
  193. if (!$this->confirm("File '{$filePath}' already exists. Do you wish to overwrite it?")) {
  194. return self::EXIT_CODE_NORMAL;
  195. }
  196. }
  197. $array = VarDumper::export($this->getOptionValues($this->action->id));
  198. $content = <<<EOD
  199. <?php
  200. /**
  201. * Configuration file for 'yii {$this->id}/{$this->defaultAction}' command.
  202. *
  203. * This file is automatically generated by 'yii {$this->id}/{$this->action->id}' command.
  204. * It contains parameters for source code messages extraction.
  205. * You may modify this file to suit your needs.
  206. *
  207. * You can use 'yii {$this->id}/{$this->action->id}-template' command to create
  208. * template configuration file with detailed description for each parameter.
  209. */
  210. return $array;
  211. EOD;
  212. if (file_put_contents($filePath, $content) === false) {
  213. $this->stdout("Configuration file was NOT created: '{$filePath}'.\n\n", Console::FG_RED);
  214. return self::EXIT_CODE_ERROR;
  215. }
  216. $this->stdout("Configuration file created: '{$filePath}'.\n\n", Console::FG_GREEN);
  217. return self::EXIT_CODE_NORMAL;
  218. }
  219. /**
  220. * Creates a configuration file template for the "extract" command.
  221. *
  222. * The created configuration file contains detailed instructions on
  223. * how to customize it to fit for your needs. After customization,
  224. * you may use this configuration file with the "extract" command.
  225. *
  226. * @param string $filePath output file name or alias.
  227. * @return int CLI exit code
  228. * @throws Exception on failure.
  229. */
  230. public function actionConfigTemplate($filePath)
  231. {
  232. $filePath = Yii::getAlias($filePath);
  233. if (file_exists($filePath)) {
  234. if (!$this->confirm("File '{$filePath}' already exists. Do you wish to overwrite it?")) {
  235. return self::EXIT_CODE_NORMAL;
  236. }
  237. }
  238. if (!copy(Yii::getAlias('@yii/views/messageConfig.php'), $filePath)) {
  239. $this->stdout("Configuration file template was NOT created at '{$filePath}'.\n\n", Console::FG_RED);
  240. return self::EXIT_CODE_ERROR;
  241. }
  242. $this->stdout("Configuration file template created at '{$filePath}'.\n\n", Console::FG_GREEN);
  243. return self::EXIT_CODE_NORMAL;
  244. }
  245. /**
  246. * Extracts messages to be translated from source code.
  247. *
  248. * This command will search through source code files and extract
  249. * messages that need to be translated in different languages.
  250. *
  251. * @param string $configFile the path or alias of the configuration file.
  252. * You may use the "yii message/config" command to generate
  253. * this file and then customize it for your needs.
  254. * @throws Exception on failure.
  255. */
  256. public function actionExtract($configFile = null)
  257. {
  258. $configFileContent = [];
  259. if ($configFile !== null) {
  260. $configFile = Yii::getAlias($configFile);
  261. if (!is_file($configFile)) {
  262. throw new Exception("The configuration file does not exist: $configFile");
  263. }
  264. $configFileContent = require($configFile);
  265. }
  266. $config = array_merge(
  267. $this->getOptionValues($this->action->id),
  268. $configFileContent,
  269. $this->getPassedOptionValues()
  270. );
  271. $config['sourcePath'] = Yii::getAlias($config['sourcePath']);
  272. $config['messagePath'] = Yii::getAlias($config['messagePath']);
  273. if (!isset($config['sourcePath'], $config['languages'])) {
  274. throw new Exception('The configuration file must specify "sourcePath" and "languages".');
  275. }
  276. if (!is_dir($config['sourcePath'])) {
  277. throw new Exception("The source path {$config['sourcePath']} is not a valid directory.");
  278. }
  279. if (empty($config['format']) || !in_array($config['format'], ['php', 'po', 'pot', 'db'])) {
  280. throw new Exception('Format should be either "php", "po", "pot" or "db".');
  281. }
  282. if (in_array($config['format'], ['php', 'po', 'pot'])) {
  283. if (!isset($config['messagePath'])) {
  284. throw new Exception('The configuration file must specify "messagePath".');
  285. }
  286. if (!is_dir($config['messagePath'])) {
  287. throw new Exception("The message path {$config['messagePath']} is not a valid directory.");
  288. }
  289. }
  290. if (empty($config['languages'])) {
  291. throw new Exception('Languages cannot be empty.');
  292. }
  293. $files = FileHelper::findFiles(realpath($config['sourcePath']), $config);
  294. $messages = [];
  295. foreach ($files as $file) {
  296. $messages = array_merge_recursive($messages, $this->extractMessages($file, $config['translator'], $config['ignoreCategories']));
  297. }
  298. $catalog = isset($config['catalog']) ? $config['catalog'] : 'messages';
  299. if (in_array($config['format'], ['php', 'po'])) {
  300. foreach ($config['languages'] as $language) {
  301. $dir = $config['messagePath'] . DIRECTORY_SEPARATOR . $language;
  302. if (!is_dir($dir) && !@mkdir($dir)) {
  303. throw new Exception("Directory '{$dir}' can not be created.");
  304. }
  305. if ($config['format'] === 'po') {
  306. $this->saveMessagesToPO($messages, $dir, $config['overwrite'], $config['removeUnused'], $config['sort'], $catalog, $config['markUnused']);
  307. } else {
  308. $this->saveMessagesToPHP($messages, $dir, $config['overwrite'], $config['removeUnused'], $config['sort'], $config['markUnused']);
  309. }
  310. }
  311. } elseif ($config['format'] === 'db') {
  312. /** @var Connection $db */
  313. $db = Instance::ensure($config['db'], Connection::className());
  314. $sourceMessageTable = isset($config['sourceMessageTable']) ? $config['sourceMessageTable'] : '{{%source_message}}';
  315. $messageTable = isset($config['messageTable']) ? $config['messageTable'] : '{{%message}}';
  316. $this->saveMessagesToDb(
  317. $messages,
  318. $db,
  319. $sourceMessageTable,
  320. $messageTable,
  321. $config['removeUnused'],
  322. $config['languages'],
  323. $config['markUnused']
  324. );
  325. } elseif ($config['format'] === 'pot') {
  326. $this->saveMessagesToPOT($messages, $config['messagePath'], $catalog);
  327. }
  328. }
  329. /**
  330. * Saves messages to database
  331. *
  332. * @param array $messages
  333. * @param Connection $db
  334. * @param string $sourceMessageTable
  335. * @param string $messageTable
  336. * @param bool $removeUnused
  337. * @param array $languages
  338. * @param bool $markUnused
  339. */
  340. protected function saveMessagesToDb($messages, $db, $sourceMessageTable, $messageTable, $removeUnused, $languages, $markUnused)
  341. {
  342. $currentMessages = [];
  343. $rows = (new Query)->select(['id', 'category', 'message'])->from($sourceMessageTable)->all($db);
  344. foreach ($rows as $row) {
  345. $currentMessages[$row['category']][$row['id']] = $row['message'];
  346. }
  347. $currentLanguages = [];
  348. $rows = (new Query)->select(['language'])->from($messageTable)->groupBy('language')->all($db);
  349. foreach ($rows as $row) {
  350. $currentLanguages[] = $row['language'];
  351. }
  352. $missingLanguages = [];
  353. if (!empty($currentLanguages)) {
  354. $missingLanguages = array_diff($languages, $currentLanguages);
  355. }
  356. $new = [];
  357. $obsolete = [];
  358. foreach ($messages as $category => $msgs) {
  359. $msgs = array_unique($msgs);
  360. if (isset($currentMessages[$category])) {
  361. $new[$category] = array_diff($msgs, $currentMessages[$category]);
  362. $obsolete += array_diff($currentMessages[$category], $msgs);
  363. } else {
  364. $new[$category] = $msgs;
  365. }
  366. }
  367. foreach (array_diff(array_keys($currentMessages), array_keys($messages)) as $category) {
  368. $obsolete += $currentMessages[$category];
  369. }
  370. if (!$removeUnused) {
  371. foreach ($obsolete as $pk => $msg) {
  372. if (mb_substr($msg, 0, 2) === '@@' && mb_substr($msg, -2) === '@@') {
  373. unset($obsolete[$pk]);
  374. }
  375. }
  376. }
  377. $obsolete = array_keys($obsolete);
  378. $this->stdout('Inserting new messages...');
  379. $savedFlag = false;
  380. foreach ($new as $category => $msgs) {
  381. foreach ($msgs as $msg) {
  382. $savedFlag = true;
  383. $lastPk = $db->schema->insert($sourceMessageTable, ['category' => $category, 'message' => $msg]);
  384. foreach ($languages as $language) {
  385. $db->createCommand()
  386. ->insert($messageTable, ['id' => $lastPk['id'], 'language' => $language])
  387. ->execute();
  388. }
  389. }
  390. }
  391. if (!empty($missingLanguages)) {
  392. $updatedMessages = [];
  393. $rows = (new Query)->select(['id', 'category', 'message'])->from($sourceMessageTable)->all($db);
  394. foreach ($rows as $row) {
  395. $updatedMessages[$row['category']][$row['id']] = $row['message'];
  396. }
  397. foreach ($updatedMessages as $category => $msgs) {
  398. foreach ($msgs as $id => $msg) {
  399. $savedFlag = true;
  400. foreach ($missingLanguages as $language) {
  401. $db->createCommand()
  402. ->insert($messageTable, ['id' => $id, 'language' => $language])
  403. ->execute();
  404. }
  405. }
  406. }
  407. }
  408. $this->stdout($savedFlag ? "saved.\n" : "Nothing to save.\n");
  409. $this->stdout($removeUnused ? 'Deleting obsoleted messages...' : 'Updating obsoleted messages...');
  410. if (empty($obsolete)) {
  411. $this->stdout("Nothing obsoleted...skipped.\n");
  412. return;
  413. }
  414. if ($removeUnused) {
  415. $db->createCommand()
  416. ->delete($sourceMessageTable, ['in', 'id', $obsolete])
  417. ->execute();
  418. $this->stdout("deleted.\n");
  419. } elseif ($markUnused) {
  420. $rows = (new Query)
  421. ->select(['id', 'message'])
  422. ->from($sourceMessageTable)
  423. ->where(['in', 'id', $obsolete])
  424. ->all($db);
  425. foreach ($rows as $row) {
  426. $db->createCommand()->update(
  427. $sourceMessageTable,
  428. ['message' => '@@' . $row['message'] . '@@'],
  429. ['id' => $row['id']]
  430. )->execute();
  431. }
  432. $this->stdout("updated.\n");
  433. } else {
  434. $this->stdout("kept untouched.\n");
  435. }
  436. }
  437. /**
  438. * Extracts messages from a file
  439. *
  440. * @param string $fileName name of the file to extract messages from
  441. * @param string $translator name of the function used to translate messages
  442. * @param array $ignoreCategories message categories to ignore.
  443. * This parameter is available since version 2.0.4.
  444. * @return array
  445. */
  446. protected function extractMessages($fileName, $translator, $ignoreCategories = [])
  447. {
  448. $coloredFileName = Console::ansiFormat($fileName, [Console::FG_CYAN]);
  449. $this->stdout("Extracting messages from $coloredFileName...\n");
  450. $subject = file_get_contents($fileName);
  451. $messages = [];
  452. $tokens = token_get_all($subject);
  453. foreach ((array) $translator as $currentTranslator) {
  454. $translatorTokens = token_get_all('<?php ' . $currentTranslator);
  455. array_shift($translatorTokens);
  456. $messages = array_merge_recursive($messages, $this->extractMessagesFromTokens($tokens, $translatorTokens, $ignoreCategories));
  457. }
  458. $this->stdout("\n");
  459. return $messages;
  460. }
  461. /**
  462. * Extracts messages from a parsed PHP tokens list.
  463. * @param array $tokens tokens to be processed.
  464. * @param array $translatorTokens translator tokens.
  465. * @param array $ignoreCategories message categories to ignore.
  466. * @return array messages.
  467. */
  468. private function extractMessagesFromTokens(array $tokens, array $translatorTokens, array $ignoreCategories)
  469. {
  470. $messages = [];
  471. $translatorTokensCount = count($translatorTokens);
  472. $matchedTokensCount = 0;
  473. $buffer = [];
  474. $pendingParenthesisCount = 0;
  475. foreach ($tokens as $token) {
  476. // finding out translator call
  477. if ($matchedTokensCount < $translatorTokensCount) {
  478. if ($this->tokensEqual($token, $translatorTokens[$matchedTokensCount])) {
  479. $matchedTokensCount++;
  480. } else {
  481. $matchedTokensCount = 0;
  482. }
  483. } elseif ($matchedTokensCount === $translatorTokensCount) {
  484. // translator found
  485. // end of function call
  486. if ($this->tokensEqual(')', $token)) {
  487. $pendingParenthesisCount--;
  488. if ($pendingParenthesisCount === 0) {
  489. // end of translator call or end of something that we can't extract
  490. if (isset($buffer[0][0], $buffer[1], $buffer[2][0]) && $buffer[0][0] === T_CONSTANT_ENCAPSED_STRING && $buffer[1] === ',' && $buffer[2][0] === T_CONSTANT_ENCAPSED_STRING) {
  491. // is valid call we can extract
  492. $category = stripcslashes($buffer[0][1]);
  493. $category = mb_substr($category, 1, -1);
  494. if (!$this->isCategoryIgnored($category, $ignoreCategories)) {
  495. $message = stripcslashes($buffer[2][1]);
  496. $message = mb_substr($message, 1, -1);
  497. $messages[$category][] = $message;
  498. }
  499. $nestedTokens = array_slice($buffer, 3);
  500. if (count($nestedTokens) > $translatorTokensCount) {
  501. // search for possible nested translator calls
  502. $messages = array_merge_recursive($messages, $this->extractMessagesFromTokens($nestedTokens, $translatorTokens, $ignoreCategories));
  503. }
  504. } else {
  505. // invalid call or dynamic call we can't extract
  506. $line = Console::ansiFormat($this->getLine($buffer), [Console::FG_CYAN]);
  507. $skipping = Console::ansiFormat('Skipping line', [Console::FG_YELLOW]);
  508. $this->stdout("$skipping $line. Make sure both category and message are static strings.\n");
  509. }
  510. // prepare for the next match
  511. $matchedTokensCount = 0;
  512. $pendingParenthesisCount = 0;
  513. $buffer = [];
  514. } else {
  515. $buffer[] = $token;
  516. }
  517. } elseif ($this->tokensEqual('(', $token)) {
  518. // count beginning of function call, skipping translator beginning
  519. if ($pendingParenthesisCount > 0) {
  520. $buffer[] = $token;
  521. }
  522. $pendingParenthesisCount++;
  523. } elseif (isset($token[0]) && !in_array($token[0], [T_WHITESPACE, T_COMMENT])) {
  524. // ignore comments and whitespaces
  525. $buffer[] = $token;
  526. }
  527. }
  528. }
  529. return $messages;
  530. }
  531. /**
  532. * The method checks, whether the $category is ignored according to $ignoreCategories array.
  533. * Examples:
  534. *
  535. * - `myapp` - will be ignored only `myapp` category;
  536. * - `myapp*` - will be ignored by all categories beginning with `myapp` (`myapp`, `myapplication`, `myapprove`, `myapp/widgets`, `myapp.widgets`, etc).
  537. *
  538. * @param string $category category that is checked
  539. * @param array $ignoreCategories message categories to ignore.
  540. * @return bool
  541. * @since 2.0.7
  542. */
  543. protected function isCategoryIgnored($category, array $ignoreCategories)
  544. {
  545. if (!empty($ignoreCategories)) {
  546. if (in_array($category, $ignoreCategories, true)) {
  547. return true;
  548. }
  549. foreach ($ignoreCategories as $pattern) {
  550. if (strpos($pattern, '*') > 0 && strpos($category, rtrim($pattern, '*')) === 0) {
  551. return true;
  552. }
  553. }
  554. }
  555. return false;
  556. }
  557. /**
  558. * Finds out if two PHP tokens are equal
  559. *
  560. * @param array|string $a
  561. * @param array|string $b
  562. * @return bool
  563. * @since 2.0.1
  564. */
  565. protected function tokensEqual($a, $b)
  566. {
  567. if (is_string($a) && is_string($b)) {
  568. return $a === $b;
  569. }
  570. if (isset($a[0], $a[1], $b[0], $b[1])) {
  571. return $a[0] === $b[0] && $a[1] == $b[1];
  572. }
  573. return false;
  574. }
  575. /**
  576. * Finds out a line of the first non-char PHP token found
  577. *
  578. * @param array $tokens
  579. * @return int|string
  580. * @since 2.0.1
  581. */
  582. protected function getLine($tokens)
  583. {
  584. foreach ($tokens as $token) {
  585. if (isset($token[2])) {
  586. return $token[2];
  587. }
  588. }
  589. return 'unknown';
  590. }
  591. /**
  592. * Writes messages into PHP files
  593. *
  594. * @param array $messages
  595. * @param string $dirName name of the directory to write to
  596. * @param bool $overwrite if existing file should be overwritten without backup
  597. * @param bool $removeUnused if obsolete translations should be removed
  598. * @param bool $sort if translations should be sorted
  599. * @param bool $markUnused if obsolete translations should be marked
  600. */
  601. protected function saveMessagesToPHP($messages, $dirName, $overwrite, $removeUnused, $sort, $markUnused)
  602. {
  603. foreach ($messages as $category => $msgs) {
  604. $file = str_replace("\\", '/', "$dirName/$category.php");
  605. $path = dirname($file);
  606. FileHelper::createDirectory($path);
  607. $msgs = array_values(array_unique($msgs));
  608. $coloredFileName = Console::ansiFormat($file, [Console::FG_CYAN]);
  609. $this->stdout("Saving messages to $coloredFileName...\n");
  610. $this->saveMessagesCategoryToPHP($msgs, $file, $overwrite, $removeUnused, $sort, $category, $markUnused);
  611. }
  612. }
  613. /**
  614. * Writes category messages into PHP file
  615. *
  616. * @param array $messages
  617. * @param string $fileName name of the file to write to
  618. * @param bool $overwrite if existing file should be overwritten without backup
  619. * @param bool $removeUnused if obsolete translations should be removed
  620. * @param bool $sort if translations should be sorted
  621. * @param string $category message category
  622. * @param bool $markUnused if obsolete translations should be marked
  623. * @return int exit code
  624. */
  625. protected function saveMessagesCategoryToPHP($messages, $fileName, $overwrite, $removeUnused, $sort, $category, $markUnused)
  626. {
  627. if (is_file($fileName)) {
  628. $rawExistingMessages = require($fileName);
  629. $existingMessages = $rawExistingMessages;
  630. sort($messages);
  631. ksort($existingMessages);
  632. if (array_keys($existingMessages) === $messages && (!$sort || array_keys($rawExistingMessages) === $messages)) {
  633. $this->stdout("Nothing new in \"$category\" category... Nothing to save.\n\n", Console::FG_GREEN);
  634. return self::EXIT_CODE_NORMAL;
  635. }
  636. unset($rawExistingMessages);
  637. $merged = [];
  638. $untranslated = [];
  639. foreach ($messages as $message) {
  640. if (array_key_exists($message, $existingMessages) && $existingMessages[$message] !== '') {
  641. $merged[$message] = $existingMessages[$message];
  642. } else {
  643. $untranslated[] = $message;
  644. }
  645. }
  646. ksort($merged);
  647. sort($untranslated);
  648. $todo = [];
  649. foreach ($untranslated as $message) {
  650. $todo[$message] = '';
  651. }
  652. ksort($existingMessages);
  653. foreach ($existingMessages as $message => $translation) {
  654. if (!$removeUnused && !isset($merged[$message]) && !isset($todo[$message])) {
  655. if (!empty($translation) && (!$markUnused || (strncmp($translation, '@@', 2) === 0 && substr_compare($translation, '@@', -2, 2) === 0))) {
  656. $todo[$message] = $translation;
  657. } else {
  658. $todo[$message] = '@@' . $translation . '@@';
  659. }
  660. }
  661. }
  662. $merged = array_merge($todo, $merged);
  663. if ($sort) {
  664. ksort($merged);
  665. }
  666. if (false === $overwrite) {
  667. $fileName .= '.merged';
  668. }
  669. $this->stdout("Translation merged.\n");
  670. } else {
  671. $merged = [];
  672. foreach ($messages as $message) {
  673. $merged[$message] = '';
  674. }
  675. ksort($merged);
  676. }
  677. $array = VarDumper::export($merged);
  678. $content = <<<EOD
  679. <?php
  680. /**
  681. * Message translations.
  682. *
  683. * This file is automatically generated by 'yii {$this->id}/{$this->action->id}' command.
  684. * It contains the localizable messages extracted from source code.
  685. * You may modify this file by translating the extracted messages.
  686. *
  687. * Each array element represents the translation (value) of a message (key).
  688. * If the value is empty, the message is considered as not translated.
  689. * Messages that no longer need translation will have their translations
  690. * enclosed between a pair of '@@' marks.
  691. *
  692. * Message string can be used with plural forms format. Check i18n section
  693. * of the guide for details.
  694. *
  695. * NOTE: this file must be saved in UTF-8 encoding.
  696. */
  697. return $array;
  698. EOD;
  699. if (file_put_contents($fileName, $content) === false) {
  700. $this->stdout("Translation was NOT saved.\n\n", Console::FG_RED);
  701. return self::EXIT_CODE_ERROR;
  702. }
  703. $this->stdout("Translation saved.\n\n", Console::FG_GREEN);
  704. return self::EXIT_CODE_NORMAL;
  705. }
  706. /**
  707. * Writes messages into PO file
  708. *
  709. * @param array $messages
  710. * @param string $dirName name of the directory to write to
  711. * @param bool $overwrite if existing file should be overwritten without backup
  712. * @param bool $removeUnused if obsolete translations should be removed
  713. * @param bool $sort if translations should be sorted
  714. * @param string $catalog message catalog
  715. * @param bool $markUnused if obsolete translations should be marked
  716. */
  717. protected function saveMessagesToPO($messages, $dirName, $overwrite, $removeUnused, $sort, $catalog, $markUnused)
  718. {
  719. $file = str_replace("\\", '/', "$dirName/$catalog.po");
  720. FileHelper::createDirectory(dirname($file));
  721. $this->stdout("Saving messages to $file...\n");
  722. $poFile = new GettextPoFile();
  723. $merged = [];
  724. $todos = [];
  725. $hasSomethingToWrite = false;
  726. foreach ($messages as $category => $msgs) {
  727. $notTranslatedYet = [];
  728. $msgs = array_values(array_unique($msgs));
  729. if (is_file($file)) {
  730. $existingMessages = $poFile->load($file, $category);
  731. sort($msgs);
  732. ksort($existingMessages);
  733. if (array_keys($existingMessages) == $msgs) {
  734. $this->stdout("Nothing new in \"$category\" category...\n");
  735. sort($msgs);
  736. foreach ($msgs as $message) {
  737. $merged[$category . chr(4) . $message] = $existingMessages[$message];
  738. }
  739. ksort($merged);
  740. continue;
  741. }
  742. // merge existing message translations with new message translations
  743. foreach ($msgs as $message) {
  744. if (array_key_exists($message, $existingMessages) && $existingMessages[$message] !== '') {
  745. $merged[$category . chr(4) . $message] = $existingMessages[$message];
  746. } else {
  747. $notTranslatedYet[] = $message;
  748. }
  749. }
  750. ksort($merged);
  751. sort($notTranslatedYet);
  752. // collect not yet translated messages
  753. foreach ($notTranslatedYet as $message) {
  754. $todos[$category . chr(4) . $message] = '';
  755. }
  756. // add obsolete unused messages
  757. foreach ($existingMessages as $message => $translation) {
  758. if (!$removeUnused && !isset($merged[$category . chr(4) . $message]) && !isset($todos[$category . chr(4) . $message])) {
  759. if (!empty($translation) && (!$markUnused || (substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@'))) {
  760. $todos[$category . chr(4) . $message] = $translation;
  761. } else {
  762. $todos[$category . chr(4) . $message] = '@@' . $translation . '@@';
  763. }
  764. }
  765. }
  766. $merged = array_merge($todos, $merged);
  767. if ($sort) {
  768. ksort($merged);
  769. }
  770. if ($overwrite === false) {
  771. $file .= '.merged';
  772. }
  773. } else {
  774. sort($msgs);
  775. foreach ($msgs as $message) {
  776. $merged[$category . chr(4) . $message] = '';
  777. }
  778. ksort($merged);
  779. }
  780. $this->stdout("Category \"$category\" merged.\n");
  781. $hasSomethingToWrite = true;
  782. }
  783. if ($hasSomethingToWrite) {
  784. $poFile->save($file, $merged);
  785. $this->stdout("Translation saved.\n", Console::FG_GREEN);
  786. } else {
  787. $this->stdout("Nothing to save.\n", Console::FG_GREEN);
  788. }
  789. }
  790. /**
  791. * Writes messages into POT file
  792. *
  793. * @param array $messages
  794. * @param string $dirName name of the directory to write to
  795. * @param string $catalog message catalog
  796. * @since 2.0.6
  797. */
  798. protected function saveMessagesToPOT($messages, $dirName, $catalog)
  799. {
  800. $file = str_replace("\\", '/', "$dirName/$catalog.pot");
  801. FileHelper::createDirectory(dirname($file));
  802. $this->stdout("Saving messages to $file...\n");
  803. $poFile = new GettextPoFile();
  804. $merged = [];
  805. $hasSomethingToWrite = false;
  806. foreach ($messages as $category => $msgs) {
  807. $msgs = array_values(array_unique($msgs));
  808. sort($msgs);
  809. foreach ($msgs as $message) {
  810. $merged[$category . chr(4) . $message] = '';
  811. }
  812. $this->stdout("Category \"$category\" merged.\n");
  813. $hasSomethingToWrite = true;
  814. }
  815. if ($hasSomethingToWrite) {
  816. ksort($merged);
  817. $poFile->save($file, $merged);
  818. $this->stdout("Translation saved.\n", Console::FG_GREEN);
  819. } else {
  820. $this->stdout("Nothing to save.\n", Console::FG_GREEN);
  821. }
  822. }
  823. }