Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

870 Zeilen
33KB

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