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
That sounds like a job for
grep
and a regular expression. Search for this:Since the above is unfortunately unreadable, I 'll break it down a bit:
This obviously matches a call to
Yii::t
taking care to account for whitespace. The secret sauce is##PATTERN##
, which is repeated twice.##PATTERN##
isThe 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'
).There's no need to reinvent the wheel. You can use
yiic
for that (if you go to the framework folder and type inyiic help message
you will get all the info to need about it's usage). For convenience, I'm going to paste it here.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 :)
Create component components/Translation.php
}
Edit configuration file config/main.php
Result
You will end up with
translation.php
file in your log file directory and file contents will be something like: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.