Laravel-使用Ajax更改语言环境 [英] Laravel - Change locale using Ajax
问题描述
我正在尝试使用Ajax更改系统的语言,但是在用户决定再次更改之前,我不知道如何更改语言环境并使之成为永久语言.当前,app.php
中的语言环境设置为'en'
,我想使用选择菜单将其更改为'nl'
.我尝试使用App::setLocale($value)
,但是不幸的是,它没有任何作用.我该怎么做?
I am trying to change the language of my system using ajax but I don't know how to change the locale and make it permanent until the user decides to change again. Currently, the locale in the app.php
is set to 'en'
and I want to change it to 'nl'
using a select menu. I tried using App::setLocale($value)
but that does not do anything, unfortunately. How can I accomplish this?
表格
<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);
}
推荐答案
您必须具有某种中间件,因为会话(语言)不是持久性的.您可以尝试这样的操作(语言中间件):
You must have some kind of middleware because session (language) is not persistent. You can try something like this (Language Middleware):
public function handle($request, Closure $next)
{
if (auth()->check()) {
$language = auth()->user()->language;
if (empty($language)) {
$language = config('app.fallback_locale');
}
app()->setLocale($language);
Session::put('applocale', $language);
auth()->user()->language = $language;
auth()->user()->save();
return $next($request);
}
if (Session::has('applocale')) {
app()->setLocale(Session::get('applocale'));
} else {
app()->setLocale(config('app.fallback_locale'));
Session::put('applocale', config('app.fallback_locale'));
}
return $next($request);
}
如您所见,我将每个用户的语言存储在db中.在控制器中,您可能会遇到这样的情况:
as you can see I store language per user in db. In controller you could have something like this:
if (auth()->check()) {
auth()->user()->language = $language;
auth()->user()->save();
return back();
}
Session::put('applocale', $language);
return back();
}
尝试考虑这样的事情
这篇关于Laravel-使用Ajax更改语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!