Symfony2 URL translate and structure for multilanguage site

6.9k Views Asked by At

How I can make a dynamic url structure according with the user's locale in Symfony2?

I explain better... The problem is not about the language-prefix changes into the URL. It works well. The problem is into the "static" part of the URL.

For example in EN i would like to have this url

http://www.mysite.com/en/about-us

in IT this

http://www.mysite.com/it/chi-siamo

Moreover it should change automatically like the URL prefix also in the twig links

<a href="{{ path('StaticsPagesBundle_about') }}"

Here, in fact, it adds automatically IT or EN according to the chosen language but, obviously, the static piece "about-us" remains always the same.

Maybe it could be a stupid question but i don't understand how i can put the bundle structure or whether there is some way to manage this thing.

Although about the translation there's no problem, in fact it changes the words into the twig according to the translations/messages.en.xliff file, my issue it's just present into the static-link-changes.

2

There are 2 best solutions below

2
On

For use multilanguage in site, you can set locale to router:

about_me:
  pattern: /{_locale}/about-me
  defaults: { .... }
  requirements: { _locale: en|it }

And control locale in controller:

$this->get('request')->getLocale();
0
On

For this case, you should look at the JMSI18nRoutingBundle. With it you can localize your routes (e.g. the 'about-me' part in you example), not only the sites.