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

404 error when caching routes with locales mapping #761

Open
jargoud opened this issue Nov 16, 2020 · 8 comments
Open

404 error when caching routes with locales mapping #761

jargoud opened this issue Nov 16, 2020 · 8 comments
Assignees
Labels

Comments

@jargoud
Copy link
Contributor

jargoud commented Nov 16, 2020

Describe the bug
When using localesMapping to rewrite pt-BR locale to simply pt, it seems to no longer work after I cache route using

php artisan route:trans:cache

command. If I clear the cache, it works fine.

The RouteServiceProvider is configured as well as the documentation.

To Reproduce

  1. In the configuration, enable several language (for me: de, en, fr, id, pt-BR)
  2. Map pt-BR to pt
  3. Cache routes with the command
  4. See error
  5. Clear routes with the command
  6. See no error

Expected behavior
Routes should be cached after the locale has been mapped.

More info:

  • Version of Laravel: 6.20.3
  • Version of the Laravel-localization package: 1.6.1
  • Which middleware is used in Route::groups: localeSessionRedirect, localizationRedirect, localeViewPath
  • Copy of the config file ( or at least setting of supportedLocales, useAcceptLanguageHeader and hideDefaultLocaleInURL):
return [

    'supportedLocales' => [
        'id' => [
            'name' => 'Indonesian',
            'script' => 'Latn',
            'native' => 'Bahasa Indonesia',
            'regional' => 'id_ID',
        ],
        'de' => [
            'name' => 'German',
            'script' => 'Latn',
            'native' => 'Deutsch',
            'regional' => 'de_DE',
        ],
        'en' => [
            'name' => 'English',
            'script' => 'Latn',
            'native' => 'English',
            'regional' => 'en_GB',
        ],
        'fr' => [
            'name' => 'French',
            'script' => 'Latn',
            'native' => 'français',
            'regional' => 'fr_FR',
        ],
        'pt-BR' => [
            'name' => 'Brazilian Portuguese',
            'script' => 'Latn',
            'native' => 'português do Brasil',
            'regional' => 'pt_BR',
        ],
    ],

    'useAcceptLanguageHeader' => true,
    'hideDefaultLocaleInURL' => false,

    'localesMapping' => [
        'pt-BR' => 'pt',
    ],
];
@jargoud
Copy link
Contributor Author

jargoud commented Nov 16, 2020

To ensure it it not a conflict with my project, I reproduced with a fresh Laravel 6 installation, only adding this dependency and following the installation steps from the readme.

@OGhawsi
Copy link

OGhawsi commented Nov 18, 2020

I got the same problem for my app...

@jargoud
Copy link
Contributor Author

jargoud commented Nov 19, 2020

I may have found a workaround, but I don't know if it's reliable.

In LoadsTranslatedCachedRoutes trait, line 27, if we replace:

$locale = $localization->setLocale();

with:

$locale = $localization->getInversedLocaleFromMapping(
    $localization->setLocale()
);

it seems to do the job!

getInversedLocaleFromMapping method returns the locale if it is not found in localesMapping mapping array from configuration.

You can override the method in your RouteServiceProvider class.

@relaypilot
Copy link

relaypilot commented Dec 6, 2020

@jargoud I am having the same issue, could you share an example of how I can rewrite it in the RouteServiceProvider? I have tried several ways with no success.

Update: Found out about this documentation, that solved my issues.

@ratatyiii
Copy link

ratatyiii commented Dec 16, 2020

@jargoud I am having the same issue, could you share an example of how I can rewrite it in the RouteServiceProvider? I have tried several ways with no success.

Update: Found out about this documentation, that solved my issues.

Hello, i have same problem, i use ur method and it's work, but when i use php artisan optimize, it't doesn't work

@iwasherefirst2
Copy link
Collaborator

The fix has been merged now! #763

@ctocph
Copy link

ctocph commented Aug 19, 2022

If someone else lands on this, php artisan optimize is not using this way of caching and thus crashes the application and shows a 404 if you are using mcamara/laravel-localization.

Solution if you are using this pack: Do not use "php artisan optimise". Clear cache by using:
php artisan cache:clear
php artisan config:clear

@rafalateneo
Copy link

rafalateneo commented Dec 2, 2024

Hi, same in my project. My routes/web.php look like this:

Route::group(['prefix' => LaravelLocalization::setLocale()], function() {
Route::get('', 'App\Http\Controllers\PageController@home');
...
});

After php artisan optimize my route e.g. www.domain.local/en respond with code 404

@iwasherefirst2 iwasherefirst2 reopened this Dec 2, 2024
@iwasherefirst2 iwasherefirst2 self-assigned this Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants