I'm trying to get the current user locale in controller
Found many references that answers this but for react
or vue
that didn't work in my case
I tried
Lang::locale();
Config::get('app.locale');
app()->getLocale()
Nothing worked, though when I change language I see in console
i18next: languageChanged de
triggered by:
// Re-init translation service
i18next.on('languageChanged', function() {
selector.forEach(function(item) {
item.innerHTML = i18next.t(item.getAttribute("data-i18n"));
});
});
How to get this value in controller?
You can do this in another way without using i18Next
Create a new column
in database table and call it user_locale
On language dropdown items href
tag add route on each item as your needs:
href="{{ route('users.setUserLocale') }}/en"
Define new route
Route::get('users/{user_locale?}','UsersController@setUserLocale')->name('users.setUserLocale');
Add in controller
use DB;
use Auth;
public function setUserLocale($user_locale = NULL) {
$thisUser = Auth::user();
$dbQry = DB::table('users')->where('id', $thisUser->id)->update(
[
'user_locale' => $user_locale,
]
);
return back();
}
This way each time a user changes the language the value will be updated in user_locale
column with the current language code
To get the locale
, you can simply do
thisUser = Auth::user();
$locale = $thisUser->user_locale;