0
votes

I'm having trouble with locale routes in symfony 2.5. Let's say my default language ist english. My example route is /user/register/. If I request it without any locale it should appear the english translation. If I request it with a locale (e.g. /fr/user/register/) it should appear with it. This can be done with the following:

/**
 * @Route("/{_locale}/user/register/")
 * @Route("/user/register/")
 * @Template()
 */
public function registerAction(Request $request)
{
    $locale = $request->getLocale();
    $translated = $this->get('translator')->trans('test.output');

    echo $locale."<br>".$translated;
    exit();
}

Now I have to questions to this:

  • How can I simply avoid that a user can call the route with the default language as a locale? In my example english is the default language, so /en/user/register/ would have the same output as /user/register/. I don't want to make a 301-redirect in each controller-action!
  • Is there no simple way to annotate a route with and without locale with one route-annotation?
2

2 Answers

1
votes

I know for the fact that this can be achieved via JMSI18nRoutingBundle. Not only that, but you could use case such as:

/contact       # For English language
/de/kontakt    # For German  
1
votes

You can add requirements for the routes, for example:

 * @Route("/{_locale}/user/register/", requirements={"_locale" = "fr|it"})

And with this, only those options, what are listed there can be placed into the _locale placeholder

EDIT:

I understand now your problem. Then here is the solution for you: JsmI18n