问题
I need to know. What is the proper way to store Locale for user. If for each users' request I change the language by
App::setLocale($newLocale);
Would not it change language for my whole project and for other requests as well? I mean when one user changes language it will be used as default for other users.
Thanks in advance
回答1:
If you set App::setLocale()
in for example in your AppServiceProvider.php
, it would change for all the users.
You could create a middleware for this. Something like:
<?php
namespace App\Http\Middleware;
use Closure;
class SetLocale
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
app()->setLocale($request->user()->getLocale());
return $next($request);
}
}
(You need to create a getLocale()
method on the User model for this to work.)
And then in your Kernel.php
create a middleware group for auth
:
'auth' => [
\Illuminate\Auth\Middleware\Authenticate::class,
\App\Http\Middleware\SetLocale::class,
],
And remove the auth
from the $routeMiddleware
array (in your Kernel.php
).
Now on every route that uses the auth
middleware, you will set the Locale of your Laravel application for each user.
回答2:
I solved this problem with a controller, middleware, and with session. This worked for me well, hope it helps you.
Handle the user request via the controller:
Simply set the language to the users session.
/**
* Locale switcher
*
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function switchLocale(Request $request)
{
if (!empty($request->userLocale)) {
Session::put('locale', $request->userLocale);
}
return redirect($request->header("referer"));
}
Route to switch locale:
Route::post('translations/switchLocale}', ['as' => 'translations.switch', 'uses' => 'Translation\TranslationController@switchLocale']);
Middleware to handle the required settings:
In the Middleware check the user's session for the language setting, if its pereset set it.
/**
* @param $request
* @param Closure $next
* @param null $guard
* @return mixed
*/
public function handle(Request $request, Closure $next, $guard = null)
{
if (Session::has('locale')) {
App::setLocale(Session::get('locale'));
}
}
Lastly the switching form:
{!! Form::open(["route" => "translations.switch", "id" => "sideBarLocaleSelectorForm"]) !!}
{!! Form::select("userLocale", $languages, Session::get("locale")) !!}
{!! Form::close() !!}
<script>
$(document).on("change", "select", function (e) {
e.preventDefault();
$(this).closest("form").submit();
})
</script>
回答3:
When someone loads your website it uses the default which is set in the config file.
The default language for your application is stored in the config/app.php configuration file.
Using the App::setLocale()
method would only change for a specific user which I assume would be set in the session, the config file value would not be altered.
You may also change the active language at runtime using the setLocale method on the App facade
You could see this in action yourself by opening your website in two different browsers (as they would be using two different sessions) then changing the locale in one and seeing the default load in the other.
https://laravel.com/docs/5.4/localization#introduction
来源:https://stackoverflow.com/questions/45433877/laravel-5-4-proper-way-to-store-locale-setlocale