【问题标题】:Laravel localization not working in all routesLaravel 本地化不适用于所有路线
【发布时间】:2019-06-24 05:12:25
【问题描述】:

我在 Laravel v5.6 中写博客 翻译新路线时它对我不起作用 但是像登录和注册和登陆页面这样的旧路线在我进入新路线时也能正常工作,但不是

切换语言页面


    Route::get('locale/{locale}', function ($locale) {

        Session::put('locale', $locale);
        return redirect()->back();

        // this link will add session of language when they click to change langauge

    })->name('locale');
Route::get('/{username}', 'ProfileAccountController@index')->name('profile')->middleware('admin.user');

当我走这条路时它不起作用,导航栏和页脚又回到关键点,删除此页面中的所有代码并测试它也不起作用的语言。

我使用了所有这些命令,什么也没用

php artisan config:cache
php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan clear-compiled

谢谢。

【问题讨论】:

  • 您应该在应用程序的早期分配语言环境,可能在中间件或服务提供者中。 if(session()->get('locale')) app()->setLocale(session()->get('locale'))
  • 当我删除中间件时它也能正常工作

标签: php laravel laravel-5.6


【解决方案1】:

在路线关闭方面也有类似的问题。尝试将您的会话设置器/获取器移动到控制器,而不是在 Route 闭包内。

https://laravel.com/docs/5.7/session#using-the-session

这可能会解决您的问题。

【讨论】:

    【解决方案2】:

    我的建议: 使用中间服装更改语言并使用https://github.com/spatie/laravel-translatable 这样更舒适。 还请检查此Change language in Laravel 5 可能会对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-05-13
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      相关资源
      最近更新 更多