Routing international URLs in Symfony

29 08 2007

Symfonys I18n support is pretty neat once you’ve got the hang of it. The one thing I always missed was to have a set of URLs per culture in order to name the urls in different languages.

Consider the following: You have to build an application that is served in English, French and German.

Now you would probably name your starting page ‘homepage’. But then it would be nice to call it ‘pagedaccueil’ for your french visitors and ’startseite’ for the german speaking, wouldn’t it?

For example with this routing you can achieve just that.

homepage:
  url:
    de:   /startseite
    en:   /homepage
    fr:   /pagedaccueil
  params: [ module: home, action: index ]

Well with a bit of tweaking I managed to find a way to achieve this. It’s currently not too easy to extend the originally shipped sfRouting,you have to use a filter to achieve that but from there it’s easy.

The slightly enhanced UrlHelper functions are not yet well documented and probably not very complete either but then again I will come round to better them as time will come and request will be made.

Oh and of course you can still have your old routes working as before. I18n routes are an optional feature you get with this plugin.

Find out more about csI18nRoutingPlugin here.

, , , , , , ,

Actions

Informations

7 responses to “Routing international URLs in Symfony”

4 10 2007
SfUser (20:01:31) :

WOW !!!

Incrediable !
THank you very very very much for that piece of code ! You saved me a LOT of time !

THanks SOOOO much !

13 03 2008
links for 2008-03-13 | xanders blog (06:25:22) :

[…] prototyp.ical.ly » Routing international URLs in Symfony Symfonys I18n support is pretty neat once you’ve got the hang of it. The one thing I always missed was to have a set of URLs per culture in order to name the urls in different languages. Consider the following: You have to build an application that is ser (tags: tradesinfo i18n php plugin symfony) « links for 2008-03-08 […]

21 06 2008
Piwaï (16:20:08) :

This is a nice plugin, really ! I was exactly looking for this way of handling i18n urls !

I think you should publish it to the symfony plugins page, here :

http://trac.symfony-project.com/wiki/SymfonyPlugins

By the way, what’s wrong with the url helper ?

21 06 2008
Piwaï (18:28:16) :

I started using it, and I would like to provide to patch to csI18nRouting class. Your own version creates an exception in the logs when the generate method is called. This is due to the fact that you catch the exception and then retry with a i18n route. I think it might be better to anticipate over the exception, so here is my patch to the generate method in csI18nRouting class :

public function generate($name, $params, $querydiv = '/', $divider = '/', $equals = '/')
{
if (sfConfig::get('sf_logging_enabled'))
{
sfLogger::getInstance()->info('{csI18nRouting} generate('.$name.').');
}
/try
{
return parent::generate($name, $params, $querydiv, $divider, $equals);
}
catch(sfConfigurationException $e)
{
$culture = sfContext::getInstance()->getUser()->getCulture();
return parent::generate(sprintf(self::I18N_NAME_SYNTAX, $name, $culture), $params, $querydiv, $divider, $equals);
}
/

if ($name && !isset($this->routes[$name]))
{
  $culture = sfContext::getInstance()->getUser()->getCulture();
  $i18nName = sprintf(self::I18N_NAME_SYNTAX, $name, $culture);
  if (isset($this->routes[$i18nName]))
  {
    $name=$i18Name;
  }
}
return parent::generate($name, $params, $querydiv, $divider, $equals);

}

Furthermore, this piece of code corrects another bug from your plugin, which was that if we did

if would generate the following exception :

The route "non_existing_rule_fr_” does not exist.

instead of the good one :

The route "non_existing_rule" does not exist.

What do you think of that patch ?

21 06 2008
Piwaï (18:31:19) :

ow the rendering in the comment is awful ! If you are interested, please contact me using my email to get the correct patch ;) .

By the way, if anyone else also need it, you may find my email on my website : http://www.piwai.info

24 07 2008
Piwaï (11:11:50) :

Your plugin is great, and I decided to extend it to correct some bugs, and add a few new features to use it for my open source web application.

Here is my project :
http://sourceforge.net/projects/plagiasi

Here is the new plugin :
https://plagiasi.svn.sourceforge.net/svnroot/plagiasi/tags/v1.0/plugins/csI18nRoutingPlugin-0.1.0-extended/

Here is an exemple of usage to enable switching language of pages and keeping the same page :

https://plagiasi.svn.sourceforge.net/svnroot/plagiasi/tags/v1.0/apps/plagiasi/templates/layout.php
=> starts line 24

What do you think about it ? I think maybe you should add the plugin to the symfony website, and many you could take advantage of my changes.

21 01 2010
sara (18:13:29) :

I instaled this plugin, but I have this error

“Fatal error: Declaration of csI18nRouting::generate() must be compatible with that of sfRouting::generate() “

does anyone can help me

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>