Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LaravelLocalizationRedirectFilter ignores config.laravellocalization.localesMapping #781

Open
j0eii opened this issue Apr 11, 2021 · 2 comments

Comments

@j0eii
Copy link

j0eii commented Apr 11, 2021

Describe the bug
The LaravelLocalizationRedirectFilter::class middleware ignores config.laravellocalization.localesMapping if the mapping exists.

To Reproduce
Steps to reproduce the behavior:

  1. Add localesMapping with default value of each languages
  2. Add LaravelLocalizationRedirectFilter
  3. Goto default language url.
  4. You should see the url doesnt redirect to empty default langauge url.

Expected behavior
The locale in url should be extracted.

More info:

  • Version of Laravel : ^8.12
  • Version of the Laravel-localization package : ^1.6.1
  • Which middleware is used in : [LaravelLocalizationRedirectFilter::class,], 'prefix' => LaravelLocalization::setLocale()]
  • Copy of the config file ( or at least setting of supportedLocales, useAcceptLanguageHeader and hideDefaultLocaleInURL).
    'en' => ['name' => 'English', 'script' => 'Latn', 'native' => 'English', 'regional' => 'en_GB'],
    'zh_HK' => ['name' => 'Chinese (Traditional)', 'script' => 'Hant', 'native' => '繁體中文', 'regional' => 'zh_HK'],
],
  'useAcceptLanguageHeader' => false,
  'hideDefaultLocaleInURL' => true,
  'localesMapping' => [
    'zh_HK' => 'tc'
  ],

Additional context

src/vendor/mcamara/laravel-localization/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationRedirectFilter.php:31

Add the following Code:

if (count(config('laravellocalization.localesMapping'))){
foreach(config('laravellocalization.localesMapping') as $orgLocale => $shortFormLocale){
if ($orgLocale ==  $shortFormLocale){
$locale = $orgLocale;
break;
}
}

It should be fixed after that.

@lcharrie
Copy link

lcharrie commented May 20, 2021

I encountered the same problem. Parameter hideDefaultLocaleInURL does not work if a localeMapping list is used.
Also your fix doesn't work. $orgLocale will never equal $shortFormLocale.

I modified the isHiddenDefault function to take into account localeMapping

# src/vendor/mcamara/laravel-localization/src/Mcamara/LaravelLocalization/LaravelLocalization.php:217

public function isHiddenDefault($locale)
{
  return  ($this->getDefaultLocale() === $this->getInversedLocaleFromMapping($locale) && $this->hideDefaultLocaleInURL());
}

It works for me now.

@kilianweisl
Copy link

I encountered the same issue. As far as I can tell this is not fixed yet. Have you considered opening a PR with your fix @lcharrie?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants