Tuesday, October 19, 2010

Symfony - extract locale from url or user (sf_culture)

If your symfony supports different cultures like en, fr, ru, de, uk etc. you can set in the routing.yml the prefix_path: /:sf_culture/module

To get the value from the URL

In an action you can use:
$locale = $request->getParameter('sf_culture');
If you are not in an action, maybe you want to put this in a helper class or Filter.
$locale = $this->getContext()->getRequest()->getParameter('sf_culture');

To get this value from the user object instead

In an action you can use:
$locale = $this->getUser()->getCulture();
Outside of an action use:
$locale = $this->getContext()->getUser()->getCulture();
Furthermore, here are some more explanations about the culture support from Symfony explained with an example.

0 comments:

Post a Comment