bash 生成 Yii 翻译消息文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11232039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate Yii translation message files
提问by Gihan
I am interested to know is there a script or otherway available to collect and generate Yii translation messages in a controller/project
我很想知道是否有脚本或其他方式可用于在控制器/项目中收集和生成 Yii 翻译消息
Eg. If I have the following codes in a controller
例如。如果我在控制器中有以下代码
Yii::t('blog', 'Your name');
Yii::t('category', 'Category name');
It should generate English translation message files as blog.php and category.php with above strings in messages directory. Please let me know if somebody knows a way. Thanks
它应该在消息目录中生成带有上述字符串的英文翻译消息文件 blog.php 和 category.php。如果有人知道方法,请告诉我。谢谢
回答by adamors
There's no need to reinvent the wheel. You can use yiicfor that (if you go to the framework folder and type in yiic help messageyou will get all the info to need about it's usage). For convenience, I'm going to paste it here.
没有必要重新发明轮子。您可以使用yiic它(如果您转到框架文件夹并输入,yiic help message您将获得有关其使用的所有信息)。为了方便,我把它贴在这里。
USAGE yiic message path/to/config/file
DESCRIPTION This command searches for messages to be translated in the specified source files and compiles them into PHP arrays as message source.
PARAMETERS * config-file: required, the path of the configuration file. You can find an example in framework/messages/config.php.
The file can be placed anywhere and must be a valid PHP script which returns an array of name-value pairs. Each name-value pair represents a configuration option.
The following options are available:
- sourcePath: string, root directory of all source files.
- messagePath: string, root directory containing message translations.
- languages: array, list of language codes that the extracted messages should be translated to. For example, array('zh_cn','en_au').
- fileTypes: array, a list of file extensions (e.g. 'php', 'xml'). Only the files whose extension name can be found in this list will be processed. If empty, all files will be processed.
- exclude: array, a list of directory and file exclusions. Each exclusion can be either a name or a path. If a file or directory name or path matches the exclusion, it will not be copied. For example, an exclusion of '.svn' will exclude all files and directories whose name is '.svn'. And an exclusion of '/a/b' will exclude file or directory 'sourcePath/a/b'.
- translator: the name of the function for translating messages. Defaults to 'Yii::t'. This is used as a mark to find messages to be translated.
- overwrite: if message file must be overwritten with the merged messages.
- removeOld: if message no longer needs translation it will be removed, instead of being enclosed between a pair of '@@' marks.
用法 yiic 消息路径/to/config/file
描述 该命令在指定的源文件中搜索要翻译的消息,并将它们编译成 PHP 数组作为消息源。
参数 * config-file:必填,配置文件的路径。您可以在 framework/messages/config.php 中找到示例。
该文件可以放在任何地方,并且必须是一个有效的 PHP 脚本,它返回一个名称-值对数组。每个名称-值对代表一个配置选项。
以下选项可用:
- sourcePath:字符串,所有源文件的根目录。
- messagePath:字符串,包含消息翻译的根目录。
- 语言:数组,提取的消息应翻译成的语言代码列表。例如,array('zh_cn','en_au')。
- fileTypes:数组,文件扩展名列表(例如'php'、'xml')。只有扩展名可以在此列表中找到的文件才会被处理。如果为空,则将处理所有文件。
- exclude: 数组,目录和文件排除列表。每个排除项可以是名称或路径。如果文件或目录名称或路径与排除项匹配,则不会复制它。例如,排除“.svn”将排除名称为“.svn”的所有文件和目录。排除“/a/b”将排除文件或目录“sourcePath/a/b”。
- 翻译器:翻译消息的函数名称。默认为'Yii::t'。这用作查找要翻译的消息的标记。
- 覆盖:如果消息文件必须被合并的消息覆盖。
- removeOld:如果消息不再需要翻译,它将被删除,而不是包含在一对“@@”标记之间。
You should modify (and move) the example config file and you're all set. Be sure to use full paths (ie. C:\path\to\projecton Windows or /var/www/your/projecton *nix)
您应该修改(并移动)示例配置文件,一切就绪。确保使用完整路径(即C:\path\to\project在 Windows 或/var/www/your/project*nix 上)
回答by briiC
I could give you input how to start and you can write your own script. I found this good for me for now :)
我可以给你输入如何开始,你可以编写自己的脚本。我现在觉得这对我有好处:)
Create component components/Translation.php
创建组件 components/Translation.php
public function missing($messageEvent) {
Yii::log(
"'".$messageEvent->message."' => '',",
'translation',
$messageEvent->category.'.'.$messageEvent->language
);
}
}
}
Edit configuration file config/main.php
编辑配置文件 config/main.php
'components' => array(
//...
'log' => array(
array(
'class'=>'CFileLogRoute',
'levels'=>'translation',
'logFile'=>'translations.log',
),
//...
),
'messages' => array(
//'class' => 'CDbMessageSource',
'onMissingTranslation' => array('Translation', 'missing'),
//'sourceMessageTable' => 'source_message',
//'translatedMessageTable' => 'message'
),
)
Result
结果
You will end up with translation.phpfile in your log file directory and file contents will be something like:
您将translation.php在日志文件目录中得到文件,文件内容将类似于:
2012/06/28 09:45:00 [translation] [Site.lv] 'MyStringInSource' => '',
....
depending on your configuration.
So you can copy 'MyStringInSource' => '',part and put in corresponding translation file.
取决于您的配置。这样您就可以复制'MyStringInSource' => '',部分并放入相应的翻译文件中。
This is helpful in development process because it will grow translation.log file with missing translation (repeatedly) until you translate these.
这在开发过程中很有帮助,因为它会增加 translation.log 文件并丢失翻译(重复),直到您翻译这些文件。
Hope it gives you idea.
希望它给你想法。
回答by Jon
That sounds like a job for grepand a regular expression. Search for this:
这听起来像是grep一个正则表达式的工作。搜索这个:
Yii::t\s*\(\s*('(?:[^']|(?<=\)')*'|"(?:[^"]|(?<=\)")*")\s*,\s*('(?:[^']|(?<=\)')*'|"(?:[^"]|(?<=\)")*")\s*\)
Since the above is unfortunately unreadable, I 'll break it down a bit:
由于不幸无法阅读上述内容,我将对其进行分解:
Yii::t\s*\(\s*##PATTERN##\s*,\s*##PATTERN##\s*\)
This obviously matches a call to Yii::ttaking care to account for whitespace. The secret sauce is ##PATTERN##, which is repeated twice. ##PATTERN##is
这显然与Yii::t注意考虑空格的呼吁相匹配。秘诀是##PATTERN##,重复两次。##PATTERN##是
('(?:[^']|(?<=\)')*'|"(?:[^"]|(?<=\)")*")
The above matches either '([^']|(?<=\\)')*'(a singly quoted string) or "([^"]|(?<=\\)")*"(a doubly quoted string). Non-capturing groups (?:) have been used to disregard interim results that are of no interest.
以上匹配'([^']|(?<=\\)')*'(单引号字符串)或"([^"]|(?<=\\)")*"(双引号字符串)。非捕获组 ( ?:) 已被用于忽略不感兴趣的中期结果。
After matching with this regex, capturing group #1 will hold the translation file name (e.g. 'blog') and group #2 will hold the string name (e.g. 'Your name').
与此正则表达式匹配后,捕获组 #1 将保存翻译文件名(例如'blog'),组 #2 将保存字符串名称(例如'Your name')。

