Add locale and requirements to all routes - Symfony2 Add locale and requirements to all routes - Symfony2 symfony symfony

Add locale and requirements to all routes - Symfony2


Use JMS18nRoutingBundle (documentation) for this purpose. No custom loader, no coding ...

The bundle is able to prefix all your routes with the locale without changing anything except some configuration for the bundle. It's the quickest ( and my recommended ) solution to get you started.

You can even translate existing routes for different locales.

A quick introduction can be found in this coderwall post.


You can do it this way, in your global configuration file

customsite:  resource: "@CustomSiteBundle/Resources/config/routing.yml"  prefix:   /{_locale}  requirements:    _locale: fr|en  defaults: { _locale: fr}

Quite slow reaction, but had some similar issue.

Nice to know is that you can also drop the {_locale} prefix when importing the resource. You would then be required to add {_locale} to every specific route.

This way you can catch the www.domain.com without a locale from inside your bundle, without having to rewrite the requirements and the defaults.

You could however, always define the www.domain.com route in your global routing configuration.


Configure symfony for localization:

Add localization to the session(please note that the convention is /locale/action):

goodbye:    pattern: /{_locale}/goodbye    defaults: { _controller: AcmeBudgetTrackerBundle:Goodbye:goodbye, _locale: en }    requirements:        _locale: en|bg

Alternatively locale can be set manually:

$this->get('session')->set('_locale', 'en_US');

app/config/config.yml

framework:    translator: { fallback: en }

In your response:

use Symfony\Component\HttpFoundation\Response;public function indexAction(){    $translated = $this->get('translator')->trans('Symfony2 is great');    return new Response($translated);}

Configure localization messages localized files:

messages.bg

<?xml version="1.0"?><xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">    <file source-language="en" datatype="plaintext" original="file.ext">        <body>            <trans-unit id="1">                <source>Symfony2 is great</source>                <target>Symfony2 е супер</target>            </trans-unit>            <trans-unit id="2">                <source>symfony2.great</source>                <target>Symfony2 е супер</target>            </trans-unit>        </body>    </file></xliff>

messages.fr

<?xml version="1.0"?><xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">    <file source-language="en" datatype="plaintext" original="file.ext">        <body>            <trans-unit id="1">                <source>Symfony2 is great</source>                <target>J'aime Symfony2</target>            </trans-unit>            <trans-unit id="2">                <source>symfony2.great</source>                <target>J'aime Symfony2</target>            </trans-unit>        </body>    </file></xliff>

More on the topic: Official symfony documentation