JMSI18nRoutingBundle icon indicating copy to clipboard operation
JMSI18nRoutingBundle copied to clipboard

Request::getLocale always returns default_locale

Open simohammedhttp opened this issue 11 years ago • 11 comments

Hi,

The JMSI18nRoutingBundle works perfectly for the majority of my routes.

jms_i18n_routing: default_locale: fr locales: [fr, ar, en] strategy: prefix_except_default

But I had some issues with this route:

@Route("/{articlePath}", requirements={"articlePath"=".+"}) showAction($articlePath, Request $request)

@Route Is intended to match routes like category/subCategory/../articleSlug

$request->getLocale() always returns fr, inside the controller for these URLs:

/en/.. /ar/.. /fr/.. /.. (fr omitted)

And the route /fr/.. must not be matched according to the intended behaviour with the prefix_except_default strategy.

tks,

simohammedhttp avatar Aug 08 '14 13:08 simohammedhttp

We're seeing this too.

svperfecta avatar Jan 15 '15 18:01 svperfecta

We're having the same problem. Is there an solution?

ceesvanegmond avatar Oct 11 '17 09:10 ceesvanegmond

Since we're all developers here somebody might as well go and looking into final routes built by the library to discover the problem 😉 . No real need to wait for 3 years to create a solution.

aik099 avatar Oct 11 '17 09:10 aik099

Hi all! Happy to review any PRs, it shouldn't be too difficult to solve

acasademont avatar Oct 11 '17 09:10 acasademont

While looking at the library I've found out, that it takes all your routes prior to their dumping in compiled router and modifies them according to the strategy. This info should help to anyone looking at solving this problem.

aik099 avatar Oct 11 '17 09:10 aik099

I solved this a long time ago with a workaround, I mean the issue must be fixed at the bundle level for a reliable/sustainable solution.

I tweaked the @route.

@Route("{_locale}{articlePath}", requirements={"_locale"="en|ar|", "articlePath"=".+"})

.. time flies ... 3 years :o

simohammedhttp avatar Oct 11 '17 10:10 simohammedhttp

@simohammedhttp Yeah, that's basically not the fix I guess. Looking in to the library right now to fix it, but I don't know exactly where to look. Any hints? Probably somewhere in the code the $request->setLocale() must requested right?

ceesvanegmond avatar Oct 11 '17 10:10 ceesvanegmond

Unfortunately, I dont't have any hint.

simohammedhttp avatar Oct 11 '17 10:10 simohammedhttp

If the requirements={"articlePath"=".+"} part of annotation is removed, then it works?

aik099 avatar Oct 11 '17 12:10 aik099

@aik099 That doesn't solve the issue when requesting the locale from $request->getLocale(). I'm trying to solve this issue, kind of new go Symfony bundles, hadn't found an fix for it @aik099. Where do we need to fix this issue?

ceesvanegmond avatar Oct 11 '17 13:10 ceesvanegmond

@aik099

I just checked the code source of the solution I have implemented :

jms_i18n_routing:
    default_locale: fr
    locales: [fr, ar, en]
    strategy: prefix_except_default

I used this route:

     * @Route("{_locale}{articlePath}", requirements={"_locale"="en|ar|", "articlePath"=".+"})
     * @Template()

The default 'fr' locale is made optional "en|ar|", look at the last vertical bar.

Now the following pages work great for both locales:

https://www.afa-advisory.com/conseil/développement-web-et-mobile (without fr, otherwise route won't be matched) https://www.afa-advisory.com/en/consulting/mobile-and-web-development

simohammedhttp avatar Oct 11 '17 14:10 simohammedhttp