Gettext icon indicating copy to clipboard operation
Gettext copied to clipboard

Twig i18n support

Open Langmans opened this issue 10 years ago • 4 comments

Is it possible to use the twig i18n extension or has anyone managed to make a different extension that allows us to use gettext/gettext and have php arrays as translation?

{% trans %}
    Hey {{ name }}, I have one apple.
{% plural apple_count %}
    Hey {{ name }}, I have {{ count }} apples.
{% notes %}
    This is shown in the user menu. This string should be shorter than 30 chars
{% endtrans %}

If not, I will have to refactor my code a bit, but I especially liked using the twig variables inside the trans block (gets converted with strtr automatically).

Langmans avatar Oct 01 '15 16:10 Langmans

Hi. I'm not familiarized with twig so I don't understand exactly what you want. There's a Twig extractor created by @exnor to capture the translations (see conversation: #71)

oscarotero avatar Oct 01 '15 17:10 oscarotero

I don't want to rely on the php gettext extension.

I'm using that extractor to compile all the twig files and then capture the compiled php classes. Extracting from twig templates and appending to a Gettext\Translations objects is easy and works just fine.

However the problem is that Twig_Extensions_Extension_I18n compiles twig nodes (trans, plural, notes) to use internal php gettext functions(gettext and ngettext), making Gettext\Translator unusable because Gettext\Translator::dpgettext is never being called inside Twig templates.

See I18n.php and Trans.php

I guess this is out of scope since it's really a twig question rather than a gettext question. @exnor have you by any chance found a solution?

Langmans avatar Oct 01 '15 18:10 Langmans

@rubenvincenten I submitted a pull request to fix this: twigphp/Twig-extensions#160

MazeChaZer avatar Dec 16 '15 10:12 MazeChaZer