You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i18n.rst 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. The i18n Extension
  2. ==================
  3. Configuration
  4. -------------
  5. The ``i18n`` extension adds `gettext`_ support to Twig. It defines one tag,
  6. ``trans``.
  7. To use it, first, :ref:`install the Extensions library<extensions-install>`.
  8. You need to register this extension before using the ``trans`` block::
  9. $twig->addExtension(new Twig_Extensions_Extension_I18n());
  10. Note that you must configure the ``gettext`` extension before rendering any
  11. internationalized template. Here is a simple configuration example from the
  12. PHP `documentation`_::
  13. // Set language to French
  14. putenv('LC_ALL=fr_FR');
  15. setlocale(LC_ALL, 'fr_FR');
  16. // Specify the location of the translation tables
  17. bindtextdomain('myAppPhp', 'includes/locale');
  18. bind_textdomain_codeset('myAppPhp', 'UTF-8');
  19. // Choose domain
  20. textdomain('myAppPhp');
  21. .. caution::
  22. The ``i18n`` extension only works if the PHP `gettext`_ extension is
  23. enabled.
  24. Usage
  25. -----
  26. Use the ``trans`` block to mark parts in the template as translatable:
  27. .. code-block:: jinja
  28. {% trans "Hello World!" %}
  29. {% trans string_var %}
  30. {% trans %}
  31. Hello World!
  32. {% endtrans %}
  33. In a translatable string, you can embed variables:
  34. .. code-block:: jinja
  35. {% trans %}
  36. Hello {{ name }}!
  37. {% endtrans %}
  38. During the gettext lookup these placeholders are converted. ``{{ name }}`` becomes ``%name%`` so the gettext ``msgid`` for this string would be ``Hello %name%!``.
  39. .. note::
  40. ``{% trans "Hello {{ name }}!" %}`` is not a valid statement.
  41. If you need to apply filters to the variables, you first need to assign the
  42. result to a variable:
  43. .. code-block:: jinja
  44. {% set name = name|capitalize %}
  45. {% trans %}
  46. Hello {{ name }}!
  47. {% endtrans %}
  48. To pluralize a translatable string, use the ``plural`` block:
  49. .. code-block:: jinja
  50. {% trans %}
  51. Hey {{ name }}, I have one apple.
  52. {% plural apple_count %}
  53. Hey {{ name }}, I have {{ count }} apples.
  54. {% endtrans %}
  55. The ``plural`` tag should provide the ``count`` used to select the right
  56. string. Within the translatable string, the special ``count`` variable always
  57. contain the count value (here the value of ``apple_count``).
  58. To add notes for translators, use the ``notes`` block:
  59. .. code-block:: jinja
  60. {% trans %}
  61. Hey {{ name }}, I have one apple.
  62. {% plural apple_count %}
  63. Hey {{ name }}, I have {{ count }} apples.
  64. {% notes %}
  65. This is shown in the user menu. This string should be shorter than 30 chars
  66. {% endtrans %}
  67. You can use ``notes`` with or without ``plural``. Once you get your templates compiled you should
  68. configure the ``gettext`` parser to get something like this: ``xgettext --add-comments=notes``
  69. Within an expression or in a tag, you can use the ``trans`` filter to translate
  70. simple strings or variables:
  71. .. code-block:: jinja
  72. {{ var|default(default_value|trans) }}
  73. Complex Translations within an Expression or Tag
  74. ------------------------------------------------
  75. Translations can be done with both the ``trans`` tag and the ``trans`` filter.
  76. The filter is less powerful as it only works for simple variables or strings.
  77. For more complex scenario, like pluralization, you can use a two-step
  78. strategy:
  79. .. code-block:: jinja
  80. {# assign the translation to a temporary variable #}
  81. {% set default_value %}
  82. {% trans %}
  83. Hey {{ name }}, I have one apple.
  84. {% plural apple_count %}
  85. Hey {{ name }}, I have {{ count }} apples.
  86. {% endtrans %}
  87. {% endset %}
  88. {# use the temporary variable within an expression #}
  89. {{ var|default(default_value|trans) }}
  90. Extracting Template Strings
  91. ---------------------------
  92. If you use the Twig I18n extension, you will probably need to extract the
  93. template strings at some point. Unfortunately, the ``xgettext`` utility does
  94. not understand Twig templates natively. But there is a simple workaround: as
  95. Twig converts templates to PHP files, you can use ``xgettext`` on the template
  96. cache instead.
  97. Create a script that forces the generation of the cache for all your
  98. templates. Here is a simple example to get you started::
  99. $tplDir = dirname(__FILE__).'/templates';
  100. $tmpDir = '/tmp/cache/';
  101. $loader = new Twig_Loader_Filesystem($tplDir);
  102. // force auto-reload to always have the latest version of the template
  103. $twig = new Twig_Environment($loader, array(
  104. 'cache' => $tmpDir,
  105. 'auto_reload' => true
  106. ));
  107. $twig->addExtension(new Twig_Extensions_Extension_I18n());
  108. // configure Twig the way you want
  109. // iterate over all your templates
  110. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tplDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
  111. {
  112. // force compilation
  113. if ($file->isFile()) {
  114. $twig->loadTemplate(str_replace($tplDir.'/', '', $file));
  115. }
  116. }
  117. Use the standard ``xgettext`` utility as you would have done with plain PHP
  118. code:
  119. .. code-block:: text
  120. xgettext --default-domain=messages -p ./locale --from-code=UTF-8 -n --omit-header -L PHP /tmp/cache/*.php
  121. Another workaround is to use `Twig Gettext Extractor`_ and extract the template
  122. strings right from `Poedit`_.
  123. .. _`gettext`: http://www.php.net/gettext
  124. .. _`documentation`: http://fr.php.net/manual/en/function.gettext.php
  125. .. _`Twig Gettext Extractor`: https://github.com/umpirsky/Twig-Gettext-Extractor
  126. .. _`Poedit`: http://www.poedit.net/