Search code examples
twigmultilingual

How to implement multilanguage in Twig?


How to implement multilanguage in Twig templates?

Before using Twig, I used gettext function to implement multilanguage, but I don't know if there is any way to call this function from a twig template.

The most related I found about this, is this question, that talks about twig and symfony, but in my case I'm using only Twig lib:

Multilanguage in Symfony2 and Twig

I've looked for functions/attributes "trans" in twig documentation but I found nothing.

Is there any function? Or I have to duplicate every template for every language?


Solution

  • This seems to be what you are looking for:

    The i18n Extension

    "The i18n extension adds gettext support to Twig. It defines one tag, trans."

    Usage

    {% trans "Hello World!" %}
    
    {% trans string_var %}
    
    {% trans %}
        Hello World!
    {% endtrans %}