jeudi 9 juillet 2020

App::setLocale is setting the local with null value in Laravel 5.2

I am trying to add translation in my website but it didn't translate. I made controller for localization that is given below

class LocalizationController extends Controller {

    public function index($locale) {
        
        App::setLocale($locale);
        
        //store the locale in session so that the middleware can register it
        session()->put('locale', $locale);
        return redirect()->back();
    }

}

Also I made a middleware for localization

Localization.php

class Localization {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        App::setLocale(env('APP_LOCALE'));
        return $next($request);
    }

}

I made folders of languages en,ro,pk etc in my resources/langfolder and in that languages folders, I made files in which corresponding translation is given. For Example, for romanian language, the translation is given below

ro.json file

{
    "Hi there!": "Salut",
    "How are you doing?": "Ce mai faci?",
    "This is basic example of how you use Laravel Localizations": "Acesta este un exemplu de bază despre cum puteți utiliza localizarea pârghiei"
}

and my route is :

Route::get('lang/{locale}', 'LocalizationController@index');

But I can't find the exact problem that why It is not setting the locale that I choose in dropdown?

Any help would be highly appreciated...Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire