3
* MessageCommand class file.
5
* @author Qiang Xue <qiang.xue@gmail.com>
6
* @link http://www.yiiframework.com/
7
* @copyright Copyright © 2008-2011 Yii Software LLC
8
* @license http://www.yiiframework.com/license/
12
* MessageCommand extracts messages to be translated from source files.
13
* The extracted messages are saved as PHP message source files
14
* under the specified directory.
16
* @author Qiang Xue <qiang.xue@gmail.com>
17
* @version $Id: MessageCommand.php 3212 2011-05-12 23:00:19Z alexander.makarow $
18
* @package system.cli.commands
21
class MessageCommand extends CConsoleCommand
23
public function getHelp()
27
yiic message <config-file>
30
This command searches for messages to be translated in the specified
31
source files and compiles them into PHP arrays as message source.
34
* config-file: required, the path of the configuration file. You can find
35
an example in framework/messages/config.php.
37
The file can be placed anywhere and must be a valid PHP script which
38
returns an array of name-value pairs. Each name-value pair represents
39
a configuration option.
41
The following options are available:
43
- sourcePath: string, root directory of all source files.
44
- messagePath: string, root directory containing message translations.
45
- languages: array, list of language codes that the extracted messages
46
should be translated to. For example, array('zh_cn','en_au').
47
- fileTypes: array, a list of file extensions (e.g. 'php', 'xml').
48
Only the files whose extension name can be found in this list
49
will be processed. If empty, all files will be processed.
50
- exclude: array, a list of directory and file exclusions. Each
51
exclusion can be either a name or a path. If a file or directory name
52
or path matches the exclusion, it will not be copied. For example,
53
an exclusion of '.svn' will exclude all files and directories whose
54
name is '.svn'. And an exclusion of '/a/b' will exclude file or
55
directory 'sourcePath/a/b'.
56
- translator: the name of the function for translating messages.
57
Defaults to 'Yii::t'. This is used as a mark to find messages to be
59
- overwrite: if message file must be overwritten with the merged messages.
67
* @param array command line parameters specific for this command
69
public function run($args)
72
$this->usageError('the configuration file is not specified.');
73
if(!is_file($args[0]))
74
$this->usageError("the configuration file {$args[0]} does not exist.");
76
$config=require_once($args[0]);
80
if(!isset($sourcePath,$messagePath,$languages))
81
$this->usageError('The configuration file must specify "sourcePath", "messagePath" and "languages".');
82
if(!is_dir($sourcePath))
83
$this->usageError("The source path $sourcePath is not a valid directory.");
84
if(!is_dir($messagePath))
85
$this->usageError("The message path $messagePath is not a valid directory.");
87
$this->usageError("Languages cannot be empty.");
89
if(!isset($overwrite))
94
$options['fileTypes']=$fileTypes;
96
$options['exclude']=$exclude;
97
$files=CFileHelper::findFiles(realpath($sourcePath),$options);
100
foreach($files as $file)
101
$messages=array_merge_recursive($messages,$this->extractMessages($file,$translator));
103
foreach($languages as $language)
105
$dir=$messagePath.DIRECTORY_SEPARATOR.$language;
108
foreach($messages as $category=>$msgs)
110
$msgs=array_values(array_unique($msgs));
111
$this->generateMessageFile($msgs,$dir.DIRECTORY_SEPARATOR.$category.'.php',$overwrite);
116
protected function extractMessages($fileName,$translator)
118
echo "Extracting messages from $fileName...\n";
119
$subject=file_get_contents($fileName);
120
$n=preg_match_all('/\b'.$translator.'\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s',$subject,$matches,PREG_SET_ORDER);
124
if(($pos=strpos($matches[$i][1],'.'))!==false)
125
$category=substr($matches[$i][1],$pos+1,-1);
127
$category=substr($matches[$i][1],1,-1);
128
$message=$matches[$i][2];
129
$messages[$category][]=eval("return $message;"); // use eval to eliminate quote escape
134
protected function generateMessageFile($messages,$fileName,$overwrite)
136
echo "Saving messages to $fileName...";
137
if(is_file($fileName))
139
$translated=require($fileName);
142
if(array_keys($translated)==$messages)
144
echo "nothing new...skipped.\n";
148
$untranslated=array();
149
foreach($messages as $message)
151
if(!empty($translated[$message]))
152
$merged[$message]=$translated[$message];
154
$untranslated[]=$message;
159
foreach($untranslated as $message)
162
foreach($translated as $message=>$translation)
164
if(!isset($merged[$message]) && !isset($todo[$message]))
165
$todo[$message]='@@'.$translation.'@@';
167
$merged=array_merge($todo,$merged);
168
if($overwrite === false)
169
$fileName.='.merged';
170
echo "translation merged.\n";
175
foreach($messages as $message)
176
$merged[$message]='';
180
$array=str_replace("\r",'',var_export($merged,true));
184
* Message translations.
186
* This file is automatically generated by 'yiic message' command.
187
* It contains the localizable messages extracted from source code.
188
* You may modify this file by translating the extracted messages.
190
* Each array element represents the translation (value) of a message (key).
191
* If the value is empty, the message is considered as not translated.
192
* Messages that no longer need translation will have their translations
193
* enclosed between a pair of '@@' marks.
195
* Message string can be used with plural forms format. Check i18n section
196
* of the guide for details.
198
* NOTE, this file must be saved in UTF-8 encoding.
205
file_put_contents($fileName, $content);
b'\\ No newline at end of file'