use __($str) to translate strings (symfony/twig)

by Robin Schambach   Last Updated December 07, 2017 10:08 AM

I usually use frameworks like Yii(2), Zend or Laravel to build pages but a customer forced us to use Wordpress this time.

I integrated Symfony/Twig as my template engine but now I have trouble with localization/translation. Because no matter what I do my strings won't be translated or even found by Wordpress.

Like in Laravel I created a Twig extension to translate the messages

class TranslateExtension extends \Twig_Extension {
    public function getFunctions(){
        return array(
            '__' => new \Twig_SimpleFunction('__', array($this, 'translate'))

    public function getName(){
        return 'TranslateExtension';

    public function translate($string, $handle){
        return __($string, $handle);

So I can do this in my template {{ __('Some strings here', 'plugin-handle') }} but these are not translated or even found by Loco translate creating a custom entry in the .pofile and compiling them into .mo files does not work either.

Can someone please explain me how this works? If there is no way to use the WP method I'll include my custom functions to translate the strings without Wordpress

Related Questions

Return Value of load_plugin_textdomain

Updated May 27, 2015 01:03 AM

How to translate WP plugin name?

Updated September 21, 2017 15:08 PM

esc_html__() and __() not working within arrays

Updated December 02, 2017 12:08 PM

Build a translation correcter plugin

Updated December 05, 2017 13:08 PM

Translate strings not recognised in plugin

Updated November 13, 2018 18:08 PM