From: Fabien Potencier Date: Wed, 7 Apr 2010 13:56:38 +0000 (+0200) Subject: [doc] added a recipe about i18n and xgettext X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=8651608f21985d657c4749a1244dbd8fc1e666f4;p=konrad%2Ftwig.git [doc] added a recipe about i18n and xgettext --- diff --git a/doc/06-Recipes.markdown b/doc/06-Recipes.markdown index c1207db..1425cc5 100644 --- a/doc/06-Recipes.markdown +++ b/doc/06-Recipes.markdown @@ -229,3 +229,40 @@ you can use the `set` tag: {% endset %} {{ form.row('Label', theinput) }} + +Extracting Template Strings for Internationalization +---------------------------------------------------- + +If you use the Twig I18n extension, you will probably need to extract the +template strings at some point. Unfortunately, the `xgettext` utility does not +understand Twig templates natively. But there is a simple workaround: as Twig +converts templates to PHP files, you can use `xgettext` on the template cache +instead. + +Create a script that forces the generation of the cache for all your +templates. Here is a simple example to get you started: + + [php] + $tplDir = dirname(__FILE__).'/templates'; + $tmpDir = '/tmp/cache/'; + $loader = new Twig_Loader_Filesystem($tplDir); + + // force auto-reload to always have the latest version of the template + $twig = new Twig_Environment($loader, array( + 'cache' => $tmpDir, + 'auto_reload' => true + )); + $twig->addExtension(new Twig_Extension_I18n()); + // configure Twig the way you want + + // iterate over all your templates + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tplDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) + { + // force compilation + $twig->loadTemplate(str_replace($tplDir.'/', '', $file)); + } + +Use the standard `xgettext` utility as you would have done with plain PHP +code: + + xgettext --default-domain=messages -p ./locale --from-code=UTF-8 -n --omit-header -L PHP /tmp/cache/*.php