Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 proper way to store Locale setLocale()

Tags:

laravel

locale

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

like image 972
Sagynbek Kenzhebaev Avatar asked Aug 01 '17 09:08

Sagynbek Kenzhebaev


People also ask

What is {{ __ }} In laravel?

Laravel 5 Translation Using the Double Underscore (__) Helper Function. The __ helper function can be used to retrieve lines of text from language files.

How do I get current locale in laravel?

To fetch current locale you should use App::getLocale() or App::isLocale('...') . Localization. You can also use app()->getLocale() which in Blade would be {{ app()->getLocale() }} .

What is @lang in laravel?

Laravel-lang is a collection of over 68 language translations for Laravel by developer Fred Delrieu (caouecs), including authentication, pagination, passwords, and validation rules. This package also includes JSON files for many languages.


2 Answers

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.

like image 112
Dees Oomens Avatar answered Oct 19 '22 03:10

Dees Oomens


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>
like image 28
Lyimmi Avatar answered Oct 19 '22 05:10

Lyimmi