Trong Laravel, ngôn ngữ được setup trong thư mục
resources/lang
Mỗi từ trong ngôn ngữ sẽ được lưu vào mảng ARRAY dạng KEY => VALUE.
Tạo một ngôn ngữ mới
Step 1: tạo folder tương ứng với ngôn ngữ trong thư mục resources/lang
1├───lang
2│ ├───en
3│ │ auth.php
4│ │ pagination.php
5│ │ passwords.php
6│ │ validation.php
7│ │ sunshine.php -> Các từ trong sunshine sử dung ngôn ngữ ENGLISH
8│ │
9│ └───vi
10│ auth.php
11│ pagination.php
12│ passwords.php
13│ validation.php
14│ sunshine.php -> Các từ trong sunshine sử dung ngôn ngữ VIỆT
Nội dung file
resources/lang/en/sunshine.php
1<?php
2
3return [
4
5 'welcome' => "Welcome everybody to SUNSHINE's SHOP!",
6
7];
Nội dung file
resources/lang/vi/sunshine.php
1<?php
2
3return [
4
5 'welcome' => "Chào mừng các bạn đến với cửa hàng SUNSHINE!",
6
7];
Step 2: setting ngôn ngữ mặc định được sử dụng trong Laravel
Hiệu chỉnh file
config/app.php
1/*
2|--------------------------------------------------------------------------
3| Application Locale Configuration
4|--------------------------------------------------------------------------
5|
6| The application locale determines the default locale that will be used
7| by the translation service provider. You are free to set this value
8| to any of the locales which will be supported by the application.
9|
10*/
11
12'locale' => 'vi',
13
14// Bổ sung thêm danh sách các ngôn ngữ
15'locales' => ['vi', 'en'],
16
17/*
18|--------------------------------------------------------------------------
19| Application Fallback Locale
20|--------------------------------------------------------------------------
21|
22| The fallback locale determines the locale to use when the current one
23| is not available. You may change the value to correspond to any of
24| the language folders that are provided through your application.
25|
26*/
27
28'fallback_locale' => 'en',
Sau khi hiệu chỉnh config, cần chạy câu lệnh sau để clear CONFIG CACHE của Laravel
1php artisan config:cache
Step 3: thay thế các từ bằng hàm magic function của Laravel __('TenFileArray.Key')
Ví dụ: bổ sung thêm dòng chào mừng, áp dụng Đa ngôn ngữ trên trang Index của Frontend
Hiệu chỉnh file
resources/views/frontend/index.blade.php
1{{-- View này sẽ kế thừa giao diện từ `frontend.layouts.master` --}}
2@extends('frontend.layouts.master')
3
4{{-- Thay thế nội dung vào Placeholder `title` của view `frontend.layouts.master` --}}
5@section('title')
6Shop Hoa tươi - Sunshine
7@endsection
8
9{{-- Thay thế nội dung vào Placeholder `custom-css` của view `frontend.layouts.master` --}}
10@section('custom-css')
11@endsection
12
13{{-- Thay thế nội dung vào Placeholder `main-content` của view `frontend.layouts.master` --}}
14@section('main-content')
15<div class="container text-center">
16 <h1>{{ __('sunshine.welcome') }}</h1>
17</div>
18
19<!-- Slider -->
20@include('frontend.widgets.homepage-slider')
21<!-- Banner -->
22@include('frontend.widgets.homepage-banner', [$data = $ds_top3_newest_loaisanpham])
23<!-- Product -->
24@include('frontend.widgets.product-list', [$data = $danhsachsanpham])
25@endsection
26
27{{-- Thay thế nội dung vào Placeholder `custom-scripts` của view `frontend.layouts.master` --}}
28@section('custom-scripts')
29@endsection
Step 4: Tạo middleware quản lý việc thay đổi ngôn ngữ
1php artisan make:middleware Locale
- Nội dung file
app/Http/Middleware/Locale.php
1<?php
2
3namespace App\Http\Middleware;
4
5use Closure;
6use App;
7use Config;
8use Session;
9
10class Locale
11{
12 /**
13 * Handle an incoming request.
14 *
15 * @param IlluminateHttpRequest $request
16 * @param Closure $next
17 * @return mixed
18 */
19 public function handle($request, Closure $next)
20 {
21 $raw_locale = Session::get('locale');
22 if (in_array($raw_locale, Config::get('app.locales'))) {
23 $locale = $raw_locale;
24 } else $locale = Config::get('app.locale');
25 App::setLocale($locale);
26 return $next($request);
27 }
28}
- Khai báo sử dụng Middleware
Localecho toàn chương trình
- Hiệu chỉnh file
app/Http/Kernel.php
1<?php
2
3namespace App\Http;
4
5use Illuminate\Foundation\Http\Kernel as HttpKernel;
6
7class Kernel extends HttpKernel
8{
9 ...
10
11 /**
12 * The application's route middleware groups.
13 *
14 * @var array
15 */
16 protected $middlewareGroups = [
17 'web' => [
18 \App\Http\Middleware\EncryptCookies::class,
19 \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
20 \Illuminate\Session\Middleware\StartSession::class,
21 // \Illuminate\Session\Middleware\AuthenticateSession::class,
22 \Illuminate\View\Middleware\ShareErrorsFromSession::class,
23 \App\Http\Middleware\VerifyCsrfToken::class,
24 \Illuminate\Routing\Middleware\SubstituteBindings::class,
25 \App\Http\Middleware\Locale::class,
26 ],
27
28 'api' => [
29 'throttle:60,1',
30 'bindings',
31 ],
32 ];
33
34 ...
35}
Step 5: Tạo route cho phép chuyển đổi ngôn ngữ
- Hiệu chỉnh file
routes/web.php
1Route::get('setLocale/{locale}', function ($locale) {
2 if (in_array($locale, Config::get('app.locales'))) {
3 Session::put('locale', $locale);
4 }
5 return redirect()->back();
6})->name('app.setLocale');
Step 6: Hiệu chỉnh giao diện cho phép Người dùng tự chuyển đổi ngôn ngữ
- Hiệu chỉnh file
resources\views\frontend\layouts\partials\header.blade.php
1<header class="header-v4">
2 <!-- Header desktop -->
3 <div class="container-menu-desktop">
4 <!-- Topbar -->
5 <div class="top-bar">
6 <div class="content-topbar flex-sb-m h-full container">
7 <div class="left-top-bar">
8 Free shipping for standard order over $100
9 </div>
10
11 <div class="right-top-bar flex-w h-full">
12 <a href="#" class="flex-c-m trans-04 p-lr-25">
13 Help & FAQs
14 </a>
15
16 <a href="#" class="flex-c-m trans-04 p-lr-25">
17 My Account
18 </a>
19
20 <a href="{{ route('app.setLocale', ['locale' => 'en']) }}" class="flex-c-m trans-04 p-lr-25">
21 EN
22 </a>
23
24 <a href="{{ route('app.setLocale', ['locale' => 'vi']) }}" class="flex-c-m trans-04 p-lr-25">
25 VI
26 </a>
27
28 <a href="#" class="flex-c-m trans-04 p-lr-25">
29 USD
30 </a>
31 </div>
32 </div>
33 </div>
34
35 ...
36</header>
Step 7: Kiểm tra
Chạy URL để kiểm tra:
http://sunshine.local/