MessageController.php 38 KB

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