aBhijit aBhijit - 1 month ago 20
Twig Question

Translate using CakePHP + TwigView + i18n

How do I use translate block in my

.tpl
file so that cakephp's extract command would understand it?

I want to translate my CakePHP web application. I am using the TwigView plugin. TwigView has i18n extract command to generate POT files. My question is why it won't scan my
.tpl
(Twig File) which has a
{% trans %} Hello {% endtrans %}
block.

You can see the Translation functionality in the Readme file of TwigView.

Answer

I don't have experience with cakePHP, but as far as generating POT files goes, cakePHP doesn't need to scan the .tpl files.

The Twig engine has a compiler which basically "converts" each twig view to a PHP script, which is held in the Twig cache folder. At least, that should happen if it's properly configured. These cache files will contain the regular gettext functions which can be recognized.

You must make sure that each view is properly compiled before scanning the cache. This script will allow you to generate the twig cache for all views. After that, you can scan the resulting PHP files. http://twig.sensiolabs.org/doc/extensions/i18n.html#extracting-template-strings

I'm not sure why you're using cakePHP to generate POT files, but POEDIT (which in turn uses the command line tool xgettext) will definately work this way.