Bejkrools
Bejkrools

Reputation: 1157

Symfony2 localization with JMSI18nRoutingBundle

I installed with composer:

"jms/i18n-routing-bundle": "dev-master" and "jms/translation-bundle": "1.1.*"

Then i add them to AppKernel file

new JMS\I18nRoutingBundle\JMSI18nRoutingBundle(), 
new JMS\TranslationBundle\JMSTranslationBundle(),

After that I add this at the end of config.yml

jms_i18n_routing:
    default_locale: en
    locales: [de, en]
    strategy: prefix

Finally I have boundle with simply controller.

namespace Comflex\W2Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Comflex\W2Bundle\Models;

class HomeController extends Controller
{
    public function indexAction(Request $request)
    {
        $session = $request->getSession();

        $user = NULL;
        if($session->get('user')!== NULL){
            $user = $session->get('user');
        }
        $request->getSession()->set('_locale', 'en');
        $locale = $request->getLocale();

        return $this->render(
             'ComflexW2Bundle:Home:index.html.twig',
             array(
                'user' => $user,
                'locale' => $locale,
                'authFormPlaceholder' => array(
                    'login' => $this->get('translator')->trans('LOGIN'),
                    'password' => $this->get('translator')->trans('PASSWORD'),
                )
            )
            );
    }
}

routing.yml inside bundle

comflex_w2_home:
    path: /
    defaults: { _controller: ComflexW2Bundle:Home:index}

and two files with translations data (message.de.yml and message.en.yml)

When I go to http://localhost/symfony/web/app_dev.php/de/ or http://localhost/symfony/web/app_dev.php then both translation are correct, but when I add /en to URL then I get No route found for "GET /en". How to fix it? And how to add /en or /de (dependending on the users language value) everytime when it's missing?

example: http://localhost/symfony/web/app_dev.php -> http://localhost/symfony/web/app_dev.php/en when user language is english or http://localhost/symfony/web/app_dev.php/de when language is german.

Upvotes: 0

Views: 553

Answers (1)

Marino Di Clemente
Marino Di Clemente

Reputation: 3190

  1. if you change the strategy of jms_i18n settings you need to manually clear your cache. so i think it's still working with prefix_except_default instead of just prefix

  2. in your routing.yml

front.nolang:
    pattern:   /
    defaults:  { _controller: AppBundle:Default:nolang }
    options: { i18n: false }

front.index:
    pattern:   /
    defaults:  { _controller: AppBundle:Default:index }
    options: { i18n: true }

this to have two different routes, the first (just /) will do a redirect to the right route with something like this

public function nolangAction(Request $request){
    $locale = $request->getLocale();
    //...add your checks here
    return $this->redirectToRoute('front.index',['_locale' => $locale]);
}

PS. i did it long time ago so i'm not sure if all works fine now, please confirm

Upvotes: 1

Related Questions