laravel-localization icon indicating copy to clipboard operation
laravel-localization copied to clipboard

localesMapping not respected in getURLFromRouteNameTranslated()

Open jandol5 opened this issue 2 years ago • 0 comments

Describe the bug When localesMapping is set, some of translated urls are incorrect.

To Reproduce Steps to reproduce the behavior:

  1. Setup Laravel and laravel-localization
  2. Set localesMapping in config to something like: 'localesMapping' => [ 'sl' => 'si', ],

Expected behavior All urls should get localized according to new mapping. Instead, I experienced some strange results:

  • on english page, links to Slovenian page are /sl/slovenian-slug instead of /si/slovenian-slug
  • on slovenian page, links to English page are /en/slovenian-slug instead of /en/english-slug

More info:

  • Version of Laravel: 8.82.0

  • Version of the Laravel-localization package: 1.7.0

  • Which middleware is used in Route::groups: 'localeCookieRedirect', 'localeSessionRedirect', 'localize'

  • Copy of the config file ( or at least setting of supportedLocales, useAcceptLanguageHeader and hideDefaultLocaleInURL): 'supportedLocales' => [ 'en' => ['name' => 'English', 'script' => 'Latn', 'native' => 'English', 'regional' => 'en_GB'], 'hr' => ['name' => 'Croatian', 'script' => 'Latn', 'native' => 'hrvatski', 'regional' => 'hr_HR'], 'sl' => ['name' => 'Slovene', 'script' => 'Latn', 'native' => 'slovenščina', 'regional' => 'sl_SI'], 'sr' => ['name' => 'Serbian', 'script' => 'Latn', 'native' => 'Srpski', 'regional' => 'sr_RS'], ],
    'useAcceptLanguageHeader' => false,
    'hideDefaultLocaleInURL' => false,
    'localesMapping' => [ 'sl' => 'si', 'sr' => 'rs', ],

  • Route caching is enabled (https://github.com/mcamara/laravel-localization/blob/master/CACHING.md) and works as expected.

  • Minimal steps to reproduce on a clean Laravel installation: Install Laravel, Laravel-localization, setup test route, set config like stated above.

Possible fix I traced down the issue to the way $route is set in getURLFromRouteNameTranslated(). To fix the issue I changed it from $route = '/'.$locale to $route = '/'.$this->getLocaleFromMapping($locale);. Since getLocaleFromMapping() returns locale even when mapping is not set, this fix is also suitable for cases when no localesMapping is set in config.

jandol5 avatar May 10 '22 21:05 jandol5