Laravel - 使用 Ajax 更改语言环境

我正在尝试使用 ajax 更改系统的语言,但我不知道如何更改语言环境并使其永久化,直到用户决定再次更改。目前,语言环境app.php设置为'en',我想将其更改为'nl'使用选择菜单。不幸的是,我尝试使用App::setLocale($value),但这并没有做任何事情。我怎样才能做到这一点?


形式


<form method="POST" action="{{ route('change_language',['id' => Auth::user()->id]) }}" class="form-row my-3">

  {{ csrf_field() }}

  {{ method_field('PATCH') }}

  <div class="form-group col-9">

     <h4 class="mb-0">{{ __('Language') }}</h4>

     <p class="m-0 sub-text">{{ __('Change the language of the dashboard') }}</p>

  </div>

  <div class="form-group col-3 d-flex justify-content-end align-items-center">

    <select name="language" id="language" class="form-control">

       <option value="nl" {{ ( Auth::user()->language == 'nl') ? 'selected' : '' }}>{{ __('Dutch') }}</option>

        <option value="en" {{ ( Auth::user()->language == 'en') ? 'selected' : '' }}>{{ __('English') }}</option>

    </select>

  </div>

  <div class="col-md-12 text-right">

    <p class="text-success saved-message" style="display: none">{{ __('The language has changed!') }}</p>

  </div>

</form>

AJAX


$('#language').change(function() {

  $.ajax({

    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},

    url: '{{ route('change_language', ['id' => Auth::user()->id]) }}',

    type: 'PATCH',

    data: {"language": $(this).children("option:selected").val()},

    success: function(data) {

             $('.saved-message').fadeIn();

                setTimeout(function() {

                   $('.saved-message').fadeOut();

                }, 1000);

           }

      })

});

路线


Route::patch('/dashboard/user/{id}/language', 'PreferencesController@changeLanguage')->name('change_language');

控制器方法


public function changeLanguage(Request $request, $id)

    {

        $user = User::findOrFail($id);


        $user->language = $request->get('language');

        $user->save();


        App::setLocale($request->language);

    }



慕尼黑的夜晚无繁华
浏览 116回答 3
3回答

陪伴而非守候

您必须有某种中间件,因为会话(语言)不是持久的。您可以尝试这样的事情(语言中间件):public function handle($request, Closure $next)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (auth()->check()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $language = auth()->user()->language;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (empty($language)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $language = config('app.fallback_locale');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app()->setLocale($language);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session::put('applocale', $language);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; auth()->user()->language = $language;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; auth()->user()->save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $next($request);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (Session::has('applocale')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app()->setLocale(Session::get('applocale'));&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app()->setLocale(config('app.fallback_locale'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session::put('applocale', config('app.fallback_locale'));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $next($request);&nbsp; &nbsp; }如您所见,我将每个用户的语言存储在数据库中。在控制器中你可以有这样的东西:&nbsp; &nbsp; &nbsp; &nbsp; if (auth()->check()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; auth()->user()->language = $language;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; auth()->user()->save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return back();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session::put('applocale', $language);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return back();&nbsp; &nbsp; &nbsp; &nbsp; }尝试考虑这样的事情

呼啦一阵风

您没有描述任何错误,但您可能不熟悉语言环境在 Laravel 中的工作方式。区域设置是根据请求设置的。因此,如果您在访问站点时在此控制器方法中设置语言环境(假设它工作正常),那么语言环境将再次与配置文件中的设置相同。要在方法中解决此 AppServiceProvider,boot您可以添加如下代码:if&nbsp;($user&nbsp;=&nbsp;auth()->user())&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;App::setLocale($user->language);}这样,当用户登录并且每次访问站点(假设他已登录)时将区域设置保存在他的配置文件中时,他将在有效区域设置中看到站点。当然,当用户未登录时,它并不能解决问题。对于这种情况,您可能希望为用户创建额外的 cookie 以从 cookie 设置区域设置,以防用户未登录。

慕后森

您需要中间件来保持本地化class Language{&nbsp; &nbsp; protected $app;&nbsp; &nbsp; public function __construct(Application $app, Request $request) {&nbsp; &nbsp; &nbsp; &nbsp; $this->app = $app;&nbsp; &nbsp; }&nbsp; &nbsp; public function handle($request, Closure $next)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->app->setLocale(session('user_locale', config('app.locale')));&nbsp; &nbsp; &nbsp; &nbsp; return $next($request);&nbsp; &nbsp; }}这个中间件应该适用于每条路线:protected $middlewareGroups = [&nbsp; &nbsp; &nbsp; &nbsp; 'web' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \App\Http\Middleware\Language::class,&nbsp; &nbsp; &nbsp; &nbsp; ]];
打开App,查看更多内容
随时随地看视频慕课网APP