Generate Yii translation message files

烈酒焚心 提交于 2019-12-20 09:04:19

问题


I am interested to know is there a script or otherway available to collect and generate Yii translation messages in a controller/project

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


回答1:


There's no need to reinvent the wheel. You can use yiic for that (if you go to the framework folder and type in yiic help message you will get all the info to need about it's usage). For convenience, I'm going to paste it here.

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.

You should modify (and move) the example config file and you're all set. Be sure to use full paths (ie. C:\path\to\project on Windows or /var/www/your/project on *nix)




回答2:


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

public function missing($messageEvent) {        
    Yii::log(
        "'".$messageEvent->message."' => '',",
        'translation',
        $messageEvent->category.'.'.$messageEvent->language
    );
}

}

Edit configuration file 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.php file in your log file directory and file contents will be something like:

 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.

This is helpful in development process because it will grow translation.log file with missing translation (repeatedly) until you translate these.

Hope it gives you idea.




回答3:


That sounds like a job for grep and a regular expression. Search for this:

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::t taking care to account for whitespace. The secret sauce is ##PATTERN##, which is repeated twice. ##PATTERN## is

('(?:[^']|(?<=\\)')*'|"(?:[^"]|(?<=\\)")*")

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').



来源:https://stackoverflow.com/questions/11232039/generate-yii-translation-message-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!