PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/cli/commands/MessageCommand.php

https://bitbucket.org/sammousa/valuematchbv-ls2
PHP | 211 lines | 152 code | 18 blank | 41 comment | 24 complexity | 0dae412ad357922cc1f3995efa932674 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * MessageCommand class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * MessageCommand extracts messages to be translated from source files.
  12. * The extracted messages are saved as PHP message source files
  13. * under the specified directory.
  14. *
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @version $Id: MessageCommand.php 3394 2011-09-14 21:31:30Z alexander.makarow $
  17. * @package system.cli.commands
  18. * @since 1.0
  19. */
  20. class MessageCommand extends CConsoleCommand
  21. {
  22. public function getHelp()
  23. {
  24. return <<<EOD
  25. USAGE
  26. yiic message <config-file>
  27. DESCRIPTION
  28. This command searches for messages to be translated in the specified
  29. source files and compiles them into PHP arrays as message source.
  30. PARAMETERS
  31. * config-file: required, the path of the configuration file. You can find
  32. an example in framework/messages/config.php.
  33. The file can be placed anywhere and must be a valid PHP script which
  34. returns an array of name-value pairs. Each name-value pair represents
  35. a configuration option.
  36. The following options are available:
  37. - sourcePath: string, root directory of all source files.
  38. - messagePath: string, root directory containing message translations.
  39. - languages: array, list of language codes that the extracted messages
  40. should be translated to. For example, array('zh_cn','en_au').
  41. - fileTypes: array, a list of file extensions (e.g. 'php', 'xml').
  42. Only the files whose extension name can be found in this list
  43. will be processed. If empty, all files will be processed.
  44. - exclude: array, a list of directory and file exclusions. Each
  45. exclusion can be either a name or a path. If a file or directory name
  46. or path matches the exclusion, it will not be copied. For example,
  47. an exclusion of '.svn' will exclude all files and directories whose
  48. name is '.svn'. And an exclusion of '/a/b' will exclude file or
  49. directory 'sourcePath/a/b'.
  50. - translator: the name of the function for translating messages.
  51. Defaults to 'Yii::t'. This is used as a mark to find messages to be
  52. translated.
  53. - overwrite: if message file must be overwritten with the merged messages.
  54. - removeOld: if message no longer needs translation it will be removed,
  55. instead of being enclosed between a pair of '@@' marks.
  56. EOD;
  57. }
  58. /**
  59. * Execute the action.
  60. * @param array command line parameters specific for this command
  61. */
  62. public function run($args)
  63. {
  64. if(!isset($args[0]))
  65. $this->usageError('the configuration file is not specified.');
  66. if(!is_file($args[0]))
  67. $this->usageError("the configuration file {$args[0]} does not exist.");
  68. $config=require_once($args[0]);
  69. $translator='Yii::t';
  70. extract($config);
  71. if(!isset($sourcePath,$messagePath,$languages))
  72. $this->usageError('The configuration file must specify "sourcePath", "messagePath" and "languages".');
  73. if(!is_dir($sourcePath))
  74. $this->usageError("The source path $sourcePath is not a valid directory.");
  75. if(!is_dir($messagePath))
  76. $this->usageError("The message path $messagePath is not a valid directory.");
  77. if(empty($languages))
  78. $this->usageError("Languages cannot be empty.");
  79. if(!isset($overwrite))
  80. $overwrite = false;
  81. if(!isset($removeOld))
  82. $removeOld = false;
  83. $options=array();
  84. if(isset($fileTypes))
  85. $options['fileTypes']=$fileTypes;
  86. if(isset($exclude))
  87. $options['exclude']=$exclude;
  88. $files=CFileHelper::findFiles(realpath($sourcePath),$options);
  89. $messages=array();
  90. foreach($files as $file)
  91. $messages=array_merge_recursive($messages,$this->extractMessages($file,$translator));
  92. foreach($languages as $language)
  93. {
  94. $dir=$messagePath.DIRECTORY_SEPARATOR.$language;
  95. if(!is_dir($dir))
  96. @mkdir($dir);
  97. foreach($messages as $category=>$msgs)
  98. {
  99. $msgs=array_values(array_unique($msgs));
  100. $this->generateMessageFile($msgs,$dir.DIRECTORY_SEPARATOR.$category.'.php',$overwrite,$removeOld);
  101. }
  102. }
  103. }
  104. protected function extractMessages($fileName,$translator)
  105. {
  106. echo "Extracting messages from $fileName...\n";
  107. $subject=file_get_contents($fileName);
  108. $n=preg_match_all('/\b'.$translator.'\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s',$subject,$matches,PREG_SET_ORDER);
  109. $messages=array();
  110. for($i=0;$i<$n;++$i)
  111. {
  112. if(($pos=strpos($matches[$i][1],'.'))!==false)
  113. $category=substr($matches[$i][1],$pos+1,-1);
  114. else
  115. $category=substr($matches[$i][1],1,-1);
  116. $message=$matches[$i][2];
  117. $messages[$category][]=eval("return $message;"); // use eval to eliminate quote escape
  118. }
  119. return $messages;
  120. }
  121. protected function generateMessageFile($messages,$fileName,$overwrite,$removeOld)
  122. {
  123. echo "Saving messages to $fileName...";
  124. if(is_file($fileName))
  125. {
  126. $translated=require($fileName);
  127. sort($messages);
  128. ksort($translated);
  129. if(array_keys($translated)==$messages)
  130. {
  131. echo "nothing new...skipped.\n";
  132. return;
  133. }
  134. $merged=array();
  135. $untranslated=array();
  136. foreach($messages as $message)
  137. {
  138. if(!empty($translated[$message]))
  139. $merged[$message]=$translated[$message];
  140. else
  141. $untranslated[]=$message;
  142. }
  143. ksort($merged);
  144. sort($untranslated);
  145. $todo=array();
  146. foreach($untranslated as $message)
  147. $todo[$message]='';
  148. ksort($translated);
  149. foreach($translated as $message=>$translation)
  150. {
  151. if(!isset($merged[$message]) && !isset($todo[$message]) && !$removeOld)
  152. $todo[$message]='@@'.$translation.'@@';
  153. }
  154. $merged=array_merge($todo,$merged);
  155. if($overwrite === false)
  156. $fileName.='.merged';
  157. echo "translation merged.\n";
  158. }
  159. else
  160. {
  161. $merged=array();
  162. foreach($messages as $message)
  163. $merged[$message]='';
  164. ksort($merged);
  165. echo "saved.\n";
  166. }
  167. $array=str_replace("\r",'',var_export($merged,true));
  168. $content=<<<EOD
  169. <?php
  170. /**
  171. * Message translations.
  172. *
  173. * This file is automatically generated by 'yiic message' command.
  174. * It contains the localizable messages extracted from source code.
  175. * You may modify this file by translating the extracted messages.
  176. *
  177. * Each array element represents the translation (value) of a message (key).
  178. * If the value is empty, the message is considered as not translated.
  179. * Messages that no longer need translation will have their translations
  180. * enclosed between a pair of '@@' marks.
  181. *
  182. * Message string can be used with plural forms format. Check i18n section
  183. * of the guide for details.
  184. *
  185. * NOTE, this file must be saved in UTF-8 encoding.
  186. *
  187. * @version \$Id: \$
  188. */
  189. return $array;
  190. EOD;
  191. file_put_contents($fileName, $content);
  192. }
  193. }