i18n.rst 4.5KB

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