Laravel Multiple Language
Laravel Multiple Language
Depend on HTML tag "language' and 'lang' directory package
Use Middleware Set Session locale
Generate each language standard Url
Depend on HTML tag "language' and 'lang' directory package
<html lang="{{app()->getLocale()}}">
2. Use Middleware Set Session locale
php artisan make:middleware Localization
if (Session::has('locale')) {
//validation Regex
switch (substr($request->path(), 0, 2)) {
case 'en':
Session::put('locale', 'en');
\App::setlocale(Session::get('locale'));
break;
default:
Session::put('locale', 'zh-CN');
\App::setlocale(Session::get('locale'));
}
}
App\Http\kernel
3. Generate each language standard Url
Use Help function
function multiple_url($lang, $url, $target = "") {
switch($lang) {
case "en":
return $url . '/en' . $target;
break;
default:
return $url . $target;
}
return $url;
}
Reference