multi language with Laravel 5.2

0

Greetings to everyone in the community

I have a problem when I want to try to place a multi-language site, I have downloaded the language packs and put them in the resources / lang folder of Laravel 5.2

I have configured my view like this:

 <li><a href="{{url('lang',['es'])}}"><img src="digitales/RecursoSistema/imagenes/spain.png"> Español</a></li>
 <li class="divider"></li>
 <li><a href="{{url('lang',['en'])}}"><img src="digitales/RecursoSistema/imagenes/united-states.png"> English</a></li>
 <li class="divider"></li>
 <li><a href="{{url('lang',['zh'])}}"><img src="digitales/RecursoSistema/imagenes/china.png"> 中国</a></li>

I have configured my route like this:

 Route::get('lang/{lang}',function ($lang){
    session(['lang' => $lang]);
    return \Redirect::back();
     })->where([
        'lang' => 'en|es|zh'
     ]);

I have created a Middleware which is called as LangMiddleware like this:

 public function handle($request, Closure $next)
{

    if(!empty(session('lang')))
    {
        \App::setLocale('lang');

    }
    return $next($request);
}

Register my Middleware as well in the Kernel.php file:

protected $middlewareGroups = [
    'web' => [
        \reconsafe\Http\Middleware\LangMiddleware::class,
        \reconsafe\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \reconsafe\Http\Middleware\VerifyCsrfToken::class,
    ],

And I have placed the routes in a main Middleware group telling you to point to the group called 'web'asi:

Route::group(['middleware' => ['web']], function(){
Route::get('lang/{lang}',function ($lang){
    session(['lang' => $lang]);
    return \Redirect::back();
})->where([
    'lang' => 'en|es|zh'
]);

 Route::group(['prefix'=>'principal','namespace'=>'\Principal'],function(){
    Route::resource('paises','PaisController');
    Route::resource('provincias','ProvinciaController');
    });
    });

The problem is that when I click on the links in the view, the language does not change ... it keeps showing the default one in the config / app.php the funny thing is that if I click on the links it goes away by default in the fallback_locale ... and check the session variable and if you save the values I sent you.

To make sure that Laravel is taking the values of the language, if I manually put the value in the config / app.php if it works, the problem is if I click on the links ...

Honestly I do not know what I'm doing wrong ... something tells me it's Middleware but I'm not sure, I need guidance to see what is wrong.

I hope you can help me.

    
asked by jose angarita 09.11.2018 в 14:44
source

1 answer

0

It worked for me this way, using Laravel 5.4. The resources / lang folder contains localization files. The name of the file corresponds to the view that will be used. To obtain a value from this file, you can simply use the following code:

Lang :: get ('localization_file_name.variable_name');

1- In config/app.php adds:

'locale' => 'ru',
'locales' => ['ru', 'en'],

2- Create a middleware called Locale.php with this code:

namespace App \ Http \ Middleware;

use Closure;
use App;
use Config;
use Session;

class Locale
{
  /**
   * Handle an incoming request.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \Closure  $next
   * @return mixed
   */
   public function handle($request, Closure $next)
   {
     $raw_locale = Session::get('locale');
     if (in_array($raw_locale, Config::get('app.locales'))) {
       $locale = $raw_locale;
     }
     else $locale = Config::get('app.locale');
       App::setLocale($locale);
       return $next($request);
   }
 }

3- I add in app/Http/Kernel.php in $middlewareGroups=[ ... ] the following line:

\App\Http\Middleware\Locale::class,

4- In routes/web.php adiciono:

Route::get('setlocale/{locale}', function ($locale) {
  if (in_array($locale, \Config::get('app.locales'))) {
    Session::put('locale', $locale);
  }
  return redirect()->back();
});

I hope it serves as a reference.

    
answered by 09.11.2018 в 15:38