i18n.rst 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. .. note::
  38. ``{% trans "Hello {{ name }}!" %}`` is not a valid statement.
  39. If you need to apply filters to the variables, you first need to assign the
  40. result to a variable:
  41. .. code-block:: jinja
  42. {% set name = name|capitalize %}
  43. {% trans %}
  44. Hello {{ name }}!
  45. {% endtrans %}
  46. To pluralize a translatable string, use the ``plural`` block:
  47. .. code-block:: jinja
  48. {% trans %}
  49. Hey {{ name }}, I have one apple.
  50. {% plural apple_count %}
  51. Hey {{ name }}, I have {{ count }} apples.
  52. {% endtrans %}
  53. The ``plural`` tag should provide the ``count`` used to select the right
  54. string. Within the translatable string, the special ``count`` variable always
  55. contain the count value (here the value of ``apple_count``).
  56. Within an expression or in a tag, you can use the ``trans`` filter to translate
  57. simple strings or variables:
  58. .. code-block:: jinja
  59. {{ var|default(default_value|trans) }}
  60. Complex Translations within an Expression or Tag
  61. ------------------------------------------------
  62. Translations can be done with both the ``trans`` tag and the ``trans`` filter.
  63. The filter is less powerful as it only works for simple variables or strings.
  64. For more complex scenario, like pluralization, you can use a two-step
  65. strategy:
  66. .. code-block:: jinja
  67. {# assign the translation to a temporary variable #}
  68. {% set default_value %}
  69. {% trans %}
  70. Hey {{ name }}, I have one apple.
  71. {% plural apple_count %}
  72. Hey {{ name }}, I have {{ count }} apples.
  73. {% endtrans %}
  74. {% endset %}
  75. {# use the temporary variable within an expression #}
  76. {{ var|default(default_value|trans) }}
  77. Extracting Template Strings
  78. ---------------------------
  79. If you use the Twig I18n extension, you will probably need to extract the
  80. template strings at some point. Unfortunately, the ``xgettext`` utility does
  81. not understand Twig templates natively. But there is a simple workaround: as
  82. Twig converts templates to PHP files, you can use ``xgettext`` on the template
  83. cache instead.
  84. Create a script that forces the generation of the cache for all your
  85. templates. Here is a simple example to get you started::
  86. $tplDir = dirname(__FILE__).'/templates';
  87. $tmpDir = '/tmp/cache/';
  88. $loader = new Twig_Loader_Filesystem($tplDir);
  89. // force auto-reload to always have the latest version of the template
  90. $twig = new Twig_Environment($loader, array(
  91. 'cache' => $tmpDir,
  92. 'auto_reload' => true
  93. ));
  94. $twig->addExtension(new Twig_Extensions_Extension_I18n());
  95. // configure Twig the way you want
  96. // iterate over all your templates
  97. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tplDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
  98. {
  99. // force compilation
  100. $twig->loadTemplate(str_replace($tplDir.'/', '', $file));
  101. }
  102. Use the standard ``xgettext`` utility as you would have done with plain PHP
  103. code:
  104. .. code-block:: text
  105. xgettext --default-domain=messages -p ./locale --from-code=UTF-8 -n --omit-header -L PHP /tmp/cache/*.php
  106. .. _`gettext`: http://www.php.net/gettext
  107. .. _`documentation`: http://fr.php.net/manual/en/function.gettext.php