Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

867 lines
32KB

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