0%

Multi Language Switch In Laravel

Intergrate multi-language support in Laravel is prettry easy. This implementation is based on a video: Switch languages in Laravel with Localization in 5.8 made easy !
Thanks Bert De Swaef!

We can simply implement multi-language support in Laravel by the following steps.

Route set up

Open your route file routes\web.php and add the following code:

1
2
3
4
Route::get('/lang/{locale}', function ($locale) {
session()->put('locale', $locale);
return redirect()->back();
});

Add middleware

1
php artisan make:middleware Localization

Open the file app/Http/Middleware/Localization.php and edit as the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use App;

class Localization
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
//check if session has locale
if (session()->has('locale')) {
//set locale
App::setLocale(session('locale'));
}
return $next($request);
}
}

Then add the Localization middleware to the middlewareGroups section of app/Http/Kernel.php file as the following code:

1
2
3
4
5
6
protected $middlewareGroups = [
'web' => [
...
\App\Http\Middleware\Localization::class,
],

Add language files

Here I use Chinese language to demonstrate how to add language support.

  1. Download the zip file and decompress to the resources/lang directory,
  2. In the same directory resources/lang create a cn.json file and add the following code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    {
    "Language": "选择语言",
    "Email": "电子邮件",
    "Password": "密码",
    "Log in": "登录",
    "Log Out": "登出",
    "Sign Up": "注册",
    "Reset Password": "重置密码",
    "Confirm Password": "确认密码",
    "New Password": "新密码",
    "Confirm New Password": "确认新密码",
    "Send Password Reset Link": "发送密码重置链接",
    "Remember me": "记住我",
    "Forgot your password?": "忘记密码?",
    "Forgot your password? No problem. Just let us know your email address and we will email you a\n password reset link that will allow you to choose a new one.": "忘记密码?不用担心。请告诉我们你的电子邮件地址,我们将发送一个密码重置链接,这将允许你设置一个新的密码。",
    "Email Password Reset Link": "发送重置密码电子邮件",
    "Dashboard": "首页",
    "You're logged in!": "你已登录!",
    "A fresh verification link has been sent to your email address.": "新的验证链接已发送到您的 E-mail。",
    "All rights reserved.": "版本所有。",
    "Before proceeding, please check your email for a verification link.": "在继续之前请先验证您的 E-mail。",
    "click here to request another": "点击重新发送 E-mail",
    "E-Mail Address": "E-mail",
    "Error": "错误",
    "Forbidden": "访问被拒绝",
    "Forgot Your Password?": "忘记密码?",
    "Go Home": "回首页",
    "Hello!": "您好:",
    "hi": "嗨",
    "If you did not create an account, no further action is required.": "如果您未注册帐号,请忽略此邮件。",
    "If you did not receive the email": "如果您没有收到",
    "If you did not request a password reset, no further action is required.": "如果您未申请重设密码,请忽略此邮件。",
    "If you’re having trouble clicking the \":actionText\" button, copy and paste the URL below\ninto your web browser: [:actionURL](:actionURL)": "如果您点击「:actionText」按钮时出现问题,请复制下方链接到浏览器中开启: [:actionURL](:actionURL)",
    "Login": "登录",
    "Logout": "注销",
    "Name": "姓名",
    "Oh no": "不好了",
    "Page Expired": "页面会话已超时",
    "Page Not Found": "页面不存在",
    "Please click the button below to verify your email address.": "请点击下面按钮验证您的 E-mail:",
    "Regards": "致敬",
    "Register": "注册",
    "Remember Me": "记住我",
    "Reset Password Notification": "重设密码通知",
    "Service Unavailable": "暂时不提供服务",
    "Sorry, you are forbidden from accessing this page.": "很抱歉,你没有权限访问此页面。",
    "Sorry, the page you are looking for could not be found.": "很抱歉!您浏览的页面不存在。",
    "Sorry, you are making too many requests to our servers.": "很抱歉!您向我们的服务器发出太多请求了。",
    "Sorry, you are not authorized to access this page.": "很抱歉!您没有权限浏览此页面。",
    "Sorry, your session has expired. Please refresh and try again.": "很抱歉!您的 Session 已过期,请刷新后再试一次。",
    "Sorry, we are doing some maintenance. Please check back soon.": "很抱歉!我们正在维护网站,请稍候再回来。",
    "Toggle navigation": "切换导航",
    "Too Many Requests": "太多请求",
    "Unauthorized": "未授权",
    "Verify Email Address": "验证 E-mail",
    "Verify Your Email Address": "验证 E-mail",
    "You are receiving this email because we received a password reset request for your account.": "您收到此电子邮件是因为我们收到了您帐户的密码重设请求。",
    "Whoops!": "哎呀!",
    "Whoops, something went wrong on our servers.": "哎呀,我们的服务器出了问题。",
    "This password reset link will expire in :count minutes.": "此密码重置链接将会在:count分钟内失效。"
    }

Place the trigger to your page

Now you can just switch the language package from eachother by adding the following code to your page:

1
2
<a href="/lang/en">English</a>
<a href="/lang/cn">中文</a>

That’s it, you can read Laravel Localization for more details