Mario Legenda
Mario Legenda

Reputation: 759

Symfony2 routing multilingual site

I have a routing problem for a multilingual site. Here's the routing.yml (only for one bundle, but the others are the same)

icoo_pitanja_route:
pattern:  /{_locale}/najcesca-pitanja
defaults: { _controller: IcooPitanjaBundle:Pitanja:pitanja, _locale:hr }
requirements:
    _locale: hr|en|de

icoo_pitanja_route:
pattern:  /{_locale}/common-questions
defaults: { _controller: IcooPitanjaBundle:Pitanja:pitanja, _locale:en }
requirements:
    _locale: hr|en|de

O the top of my page, I have 3 links to change the languages to croatian (default), english and german. The code for making link to change them is this...

{% set requestParams = app.request.attributes.get('_route_params') %}
    {% set requestRoute = app.request.attributes.get('_route') %}
    <div class='languages col-xs-4'>
        <a href="{{ path(requestRoute, requestParams|merge({'_locale' : 'hr'})) }}" class='col-xs-3'><span class='real-link'>croatian</span><span></span></a>
        <a href="{{ path(requestRoute, requestParams|merge({'_locale' : 'en'})) }}" class='col-xs-3'><span class='real-link'>english</span><span>|</span></a>
        <a href="{{ path(requestRoute, requestParams|merge({'_locale' : 'de'})) }}" class='col-xs-3'><span class='real-link'>deutch</span><span>|</span></a>
    </div>

The real problem is in the path() twig method that makes link for a navigation that the above route controls. It creates the paths only for the english version. I presume beacuse it's the last one so when I put a route for german language, it will generate the path for that route. Since the path name is icoo_pitanja_route and its the same route name for english and croatian version, the path() is defined like this...

path('icoo_naslovna_route')

What I want, is when I click on croatian, that the url be www.example.com/hr/najcesca-pitanja, for english www.example.com/en/common-questions. But the paht() method chooses the last one, the english one.

Now, there are several ways to solve this. One of them would be to make diffrent routes for every language. The other one would be to make it like this...

pattern: (_locale}/{language-specific-link} 

and handle it in the controller with an array of valid links for each language then, if the url is valid, return the proper response. If not, throw not found exception but I think that that invalidates the purpose of Symfony2 routing.

Does anyone have any ideas on how to handle this problem?

Upvotes: 1

Views: 664

Answers (1)

Debreczeni Andr&#225;s
Debreczeni Andr&#225;s

Reputation: 1678

Because your route names are the same. Try changing the route names as such:

icoo_pitanja_route_hr:
pattern:  /{_locale}/najcesca-pitanja
defaults: { _controller: IcooPitanjaBundle:Pitanja:pitanja, _locale:hr }
requirements:
  _locale: hr|en|de

icoo_pitanja_route_en:
pattern:  /{_locale}/common-questions
defaults: { _controller: IcooPitanjaBundle:Pitanja:pitanja, _locale:en }
requirements:
   _locale: hr|en|de

Then change the corresponding path function calls, too.

This is needed because the _locale parameter is special.

Upvotes: 3

Related Questions