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 code
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)
I could give you input how to start and you can write your own script. I found this good for me for now :)
public function missing($messageEvent) {
Yii::log(
"'".$messageEvent->message."' => '',",
'translation',
$messageEvent->category.'.'.$messageEvent->language
);
}
}
'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'
),
)
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.
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'
).