2

I'm using Symfony 2.6 and I'm trying to use translations inside my \Twig_Extension but translator always use and return the default_locale translated text. (in my case "EN")

I created a service in services.yml:

utils.twig_extension:
    class: Skaphandrus\AppBundle\Twig\UtilsExtension
    public: false
    arguments: ["@service_container", "@translator.default"]
    tags:
        - { name: twig.extension,  alias:  utils.twig_extension  }

and inside my extension:

private $container;
private $translator;
public function __construct($container, Translator $translator) {
    $this->container = $container;
    $this->translator = $translator;
}

And inside my method I have:

return $this->translator->trans('message');

But when I run the code it always return the "EN" locale message.

Thanks in advance for any help.

  • Is you're current locale correct in Twig? Just add a `{{ app.request.locale }}` to write it. – Yassine Guedidi Jul 14 '15 at 22:09
  • Are you telling me to pass the locale to the extension method? But the extension dont know the current locale from user? – Luis Miguens Jul 14 '15 at 22:17
  • I mean what's your current locale in your Twig template. What is the result of `{{ app.request.locale }}` in the Twig template that use your Twig extension. – Yassine Guedidi Jul 14 '15 at 22:57
  • My website have 3 languages, wich the users can change by changing the URL. Everyting works fine(all translated messages are working) except translated message inside my custom twig extension. In the twig template I use the EN, FR and PT, but the message translated inside twig template return the message allways in EN. – Luis Miguens Jul 14 '15 at 23:30

2 Answers2

0

You do not need a custom twig extension for translation. Simply place all translation files in the bundle under Resources > translations > BUNDLENAME.local.yml (AcmeBundle.en.yml). Based off your current default or set locale it will select the proper file.

You're getting the default locale which in this case is EN.

You need to set your locale to be something different in your parameters.yml OR specify what locale to use during the translation.

http://symfony.com/doc/current/components/translation/introduction.html

Shawn Abshire
  • 70
  • 2
  • 12
  • 1
    Yes, but I need to use translations inside methods in my custom extension and the method allways return EN translation. – Luis Miguens Jul 14 '15 at 22:13
0

First, you have to get the current locale of your user in your extension.

You could force the translator locale to the one used by you user in each trans method :

#translation for the french version
$this->translator->trans('message', array(), 'messages', 'fr_FR');

http://symfony.com/doc/current/components/translation/usage.html#forcing-the-translator-locale

You could also use the method setLocale in your extension. All your translations will match the locale without forcing it :

$this->translator->setLocale('fr_FR');
$this->translator->trans('message');
HypeR
  • 2,186
  • 16
  • 22