A plugin I maintain uses Twig templates to produce HTML (and some other outputs). I’ve tried using __()
within these (first adding the i18n functions with $twig->addFunction()
) but it doesn’t work. The strings are output correctly, but not picked up for inclusion in the .pot
file.
For example the following in base.twig
should be translatable:
{{__('Search', 'tabulate')}}
I’m obviously off on the wrong track here. Anyone got any pointers for doing this?
2 Answers
Here’s how the Timber plugin handles this:
$twig->addFunction( '__', new Twig_SimpleFunction( '__', function ( $text, $domain = 'default' ) {
return __( $text, $domain );
} ) );
I’m guessing you’re doing something similar, in which case the reason these are being skipped is that you’re using a variable for the text domain. Have you tried hard-coding the text domain?
$twig->addFunction( '__', new Twig_SimpleFunction( '__', function ( $text ) {
return __( $text, 'your-text-domain' );
} ) );
If that’s not an option or it doesn’t work you’ll need to handle all of the translation in PHP and pass the translated text to your Twig templates as suggested by another commenter.