Timber and WPML do not translate text strings

0

I started using Timber with Wordpress and everything was fine until I got to do text translations. I use WPML but apparently it does not work with Timber.

To translate a text with WPML I usually use the following:

_e('Texto', 'theme');

Now with Timber I've tried this:

{{ _e('Read more', 'theme') }}
{{ _e('string', 'theme') }}

{{ function("icl_translate", 'theme', 'string_identifier', 'string) }}

{{ dump(ICL_LANGUAGE_CODE) }} // NULL

But it seems that none of this works. Can anybody help me?

Thanks !!!

    
asked by Mario Sanchez Maselli 21.02.2017 в 16:32
source

1 answer

0

Timber separates the logic from the template, this means that you can not use PHP functions, such as _e() or __() . To translate the theme, it would be necessary to create a twig filter or function to do, you can try the following:

add_filter('get_twig', 'add_to_twig');

function add_to_twig($twig) {

$twig->addExtension(new Twig_Extension_StringLoader());

$twig->addFilter(new Twig_SimpleFilter('translate', 'create_translated_string'));
   return $twig;
}

function create_translated_string($text) {
    return __($text, 'theme_name');
}

Afterwards you would only have to pass your text strings through your twig function.

{{ "Nombre" | translate }}

This should be enough, but you can find out more about this in the timber documentation itself:
link

    
answered by 24.02.2017 в 13:48