php-gettext / Gettext

PHP library to collect and manipulate gettext (.po, .mo, .php, .json, etc)
MIT License
687 stars 134 forks source link

Twig i18n support #82

Open Langmans opened 8 years ago

Langmans commented 8 years ago

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).

oscarotero commented 8 years ago

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)

Langmans commented 8 years ago

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?

MazeChaZer commented 8 years ago

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

elcreator commented 1 week ago

https://github.com/elcreator/tempot can be used to extract without the need to compile twig to PHP (i.e. if you haven't proper PHP environment where you have the project files).