Support the ongoing development of Laravel.io →
Configuration Views Forms
Last updated 1 year ago.
0

You need to set the app's locale to the choosen language, e.g.: \App::setLocale($language);

In our applications all urls are prefixed with the language, for instance: /en/user/list And a custom middleware just sets the locale on every request using the first URL segment.

0

Thanks for the reply. Please break it down for me because I'm not a Laravel or PHP expert.

I want to have two idioms: EN and PT.

Have set the middleware in App/Html/MiddlewareApp.php:

<?php namespace App\Http\Middleware;

use Closure;

class App {

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
	if(!\Session::has('locale'))
    {
       \Session::put('locale', \Config::get('app.locale'));
    }

    app()->setLocale(\Session::get('locale'));

    return $next($request);
}

}

In kernel.php:

 'App\Http\Middleware\App',

LanguageController.php:

<?php namespace App\Http\Controllers;

use App\Http\Requests;

use App\Http\Controllers\Controller; use Auth; use Session; use Input; use Redirect; use Illuminate\Http\Request;

class LanguageController extends Controller {

public function index()
{
	Session::set('locale', Input::get('locale'));
	return Redirect::back();
}

}

Routes.php:

Route::post('/language', array(

'before' => 'csrf',
'as'	=>	'language-choose',
'uses'	=>	'LanguageController@choose'

));

In Config/App.php I have:

'locale' => 'en',

Do I need to add 'pt' ?

Thank you.

Last updated 9 years ago.
0

You are triying to reinvent the wheel dude. Check out this package: https://github.com/mcamara/laravel-localization

0

jartaud said:

You are triying to reinvent the wheel dude. Check out this package: https://github.com/mcamara/laravel-localization

Yes, have worked on that package since yesterday. It's not fully working but I'm getting there.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Perseus0 perseus0 Joined 23 Feb 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.