Last Updated: February 25, 2016
·
269
· raulsilvamx

symfony2 (2.6.4) - Translations

Hello again!

I've worked with translations this days. So after many attempt of use translations. i've found the solution.

1 config.yml - "locale" is language default example "en"

framework:
    translator:      { fallbacks: ["%locale%"] }
    default_locale:  "%locale%"

with this you enable translation services in twig.

2 twig template file

{{ 'tu quieres esto en ingles' | trans }}

with this "you want this in english"

3 php terminal (command)
console translation:update --force --output-format="xlf" en AcmeMyBundle

"en" is language of your choice.

4 In src/Acme/MyBundle/Resources/translations you will get the translation file with extension .xlf

In this file search or you could find:

<trans-unit id="super number" resname="tu quieres esto en ingles">
  <source>tu quieres esto en ingles</source>
  <target>__tu quieres esto en ingles</target>
</trans-unit>

change to:

<trans-unit id="super number" resname="tu quieres esto en ingles">
  <source>tu quieres esto en ingles</source>
  <target>you want this in english</target>
</trans-unit>

5 almost done... in Controller's Bundle / function add this:

$locale = substr($request->server->get('HTTP_ACCEPT_LANGUAGE'), 0, 2);
//$locale = 'en';
$this->get('translator')->setLocale($locale);

with this you get the language of browser's user.

i hope this could useful to you...