From 1eef779e00a851a15d69f6530f2d70e80d7d128d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 13 Aug 2010 19:15:57 +0200 Subject: [PATCH] added a trans filter (closes #85) --- CHANGELOG | 1 + doc/02-Twig-for-Template-Designers.markdown | 6 ++++++ doc/06-Recipes.markdown | 20 ++++++++++++++++++++ lib/Twig/Extension/I18n.php | 12 ++++++++++++ 4 files changed, 39 insertions(+), 0 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 295252e..30cea44 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,7 @@ Backward incompatibilities: * the odd and even filters are now tests: {{ foo|odd }} must now be written {{ foo is(odd) }} + * added a "trans" filter * added "test" feature (accessible via the "is" operator) * removed the debug tag (should be done in an extension) * fixed trans tag when no vars are used in plural form diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index fa646db..2eefee4 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -881,6 +881,12 @@ The `plural` tag should provide the `count` used to select the right string. Within the translatable string, the special `count` variable always contain the count value (here the value of `apple_count`). +Within an expression or in a tag, you can use the `trans` filter to translate +simple strings or variables: + + [twig] + {{ var|default(default_value|trans) }} + Expressions ----------- diff --git a/doc/06-Recipes.markdown b/doc/06-Recipes.markdown index e618a1f..1ef180f 100644 --- a/doc/06-Recipes.markdown +++ b/doc/06-Recipes.markdown @@ -243,3 +243,23 @@ 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 + +Complex Translations within an Expression or Tag +------------------------------------------------ + +Translations can be done with both the `trans` tag and the `trans` filter. The +filter is less powerful as it only works for simple variables or strings. For +more complex scenario, like pluralization, you can use a two-step strategy: + + [twig] + {# assign the translation to a temporary variable #} + {% set default_value %} + {% trans %} + Hey {{ name }}, I have one apple. + {% plural apple_count %} + Hey {{ name }}, I have {{ count }} apples. + {% endtrans %} + {% endset %} + + {# use the temporary variable within an expression #} + {{ var|default(default_value|trans) }} diff --git a/lib/Twig/Extension/I18n.php b/lib/Twig/Extension/I18n.php index 0e22c24..d1d7a10 100644 --- a/lib/Twig/Extension/I18n.php +++ b/lib/Twig/Extension/I18n.php @@ -21,6 +21,18 @@ class Twig_Extension_I18n extends Twig_Extension } /** + * Returns a list of filters to add to the existing list. + * + * @return array An array of filters + */ + public function getFilters() + { + return array( + 'trans' => new Twig_Filter_Function('gettext'), + ); + } + + /** * Returns the name of the extension. * * @return string The extension name -- 1.7.2.5