Choosing routing.yml based on user culture?
In my Symfony application I would like to choose the routing.yml based on the current user's culture;
'en' => routing.en.yml
'no' => routing.no.yml
and so forth.
Any suggestion to how this can be done?
Edit: I am not trying to do i18n this way - I use Symfony's built-in methods for that. I simply want "static" urls to reflect the user's language:
/en/projects/internal/project-name
/no/prosjekter/interne/prosjektnavn
/fr/baguette/champs-elysee/foux-de-fafa
Where "projects" is a module,开发者_如何学运维 the category "internal" and "project-name" are stored in the database.
I had the same problem with a recent website I did. I, however, did not find a proper solution and ended up making all URLs english.
I think you should have a look at the ysfDimensionsPlugin - I haven't checked it out but it might be of use to you.
I wanted to achieve the same thing. In Symfony 1.4 here is what I've did:
Created a domain => culture map in app.yml
all:
languages:
domain_map:
www.example.com: en
www.example.it: it
www.example.es: es
Created a myPatternRouting
class extending the sfPatternRouting
class myPatternRouting extends sfPatternRouting
{
public function getConfigFileName()
{
$domain_map = sfConfig::get('app_languages_domain_map');
$domain = $_SERVER['SERVER_NAME'];
$culture = isset($domain_map[$domain]) ? $domain_map[$domain] : 'en';
$routing = sprintf('config/routing.%s.yml', $culture);
return sfContext::getInstance()->getConfigCache()->checkConfig($routing, true);
}
}
Changed the factory for routing in factories.yml
all:
routing:
class: myPatternRouting
Created a config handler entry for the new pattern of the routing.yml files into config_handlers.yml
config/routing.*.yml:
class: sfRoutingConfigHandler
And then created the routing files as routing.[culture].yml
And it works :)
There may not be a way to do this without dynamically loading the routing using a filter. You could override sfPatternRouting and write a custom loadConfiguration function, but you'd need to know the user's culture when the routing class gets instantiated*. If you go the filter route, simply load the proper routing file on the first half of the filter chain.
*If you go this route, make sure you change factories.yml as well.
I am not sure this is a correct way to implement i18n. What goal are you trying to achieve with this solution? Symfony has all i18n tools built-in and you should have no problems to use integrated ways.
Look here: http://www.symfony-project.org/book/1_2/13-I18n-and-L10n and scroll down to the "Culture in the URL" box. It should solve your problem.
i use this plugin. My routes look like this:
news_ro:
url: /ro/stiri/:slug
requirements: { sf_culture: (?:ro), page: \d+ }
param: { module: news, action: index, page: 1, sf_culture: ro}
news_en:
url: /en/news/:slug
requirements: { sf_culture: (?:en), page: \d+ }
param: { module: news, action: index, page: 1, sf_culture: en }
I take the slug basen on the current culture.
精彩评论