
Laravel i18n : คู่มือการรองรับหลายภาษาและโลคัลไลเซชันอย่างครบถ้วน
ตั้งแต่ไฟล์ภาษาแรกจนถึงระบบจริง กำหนดระบบแปล Laravel จัดการพหูพจน์ แก้ข้อบกพร่องค่าสำรอง JSON และเพิ่มลำดับภาษาสำรองอัจฉริยะสำหรับรูปแบบภูมิภาค
ทำความเข้าใจระบบแปล Laravel
Laravel มีระบบแปลในตัวที่รองรับสองรูปแบบคืออาร์เรย์ PHP และ JSON ไฟล์ PHP ใช้คีย์ซ้อนจัดตามฟีเจอร์ เช่น auth.failed, validation.required ส่วน JSON ใช้ข้อความต้นฉบับเป็นคีย์ ซึ่งง่ายกว่าแต่ไม่รองรับการซ้อน
composer create-project laravel/laravel my-app
cd my-app
# Laravel includes i18n out of the box
# No extra packages needed for basic usageกำหนดค่าภาษา
ตั้งภาษาเริ่มต้นและภาษาสำรองของแอปใน config/app.php ภาษาสำรองใช้เมื่อไม่มีคีย์ในภาษาปัจจุบัน กำหนดภาษาที่รองรับและเพิ่มมิดเดิลแวร์เพื่อตรวจและตั้งภาษาที่ผู้ใช้ต้องการ
// config/app.php
return [
'locale' => 'en', // Default locale
'fallback_locale' => 'en', // Fallback when key is missing
'faker_locale' => 'en_US',
// Available locales (for your language switcher)
'available_locales' => ['en', 'de', 'ja', 'es', 'fr', 'pt-BR'],
];// app/Http/Middleware/SetLocale.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class SetLocale
{
public function handle(Request $request, Closure $next)
{
$locale = $request->segment(1); // e.g. /de/about
if (in_array($locale, config('app.available_locales'))) {
app()->setLocale($locale);
}
return $next($request);
}
}ใช้ฟังก์ชันแปล
Laravel มีสามวิธี ได้แก่ ฟังก์ชันช่วย __() ซึ่งแนะนำ ฟังก์ชัน trans() และคำสั่ง Blade @lang ทั้งหมดรับคีย์และพารามิเตอร์แทนค่าเสริม ใช้ __() ในโค้ด PHP กับเทมเพลต Blade และ @lang ใน Blade เมื่อไม่ต้องหลีก HTML
// lang/en/messages.php
return [
'welcome' => 'Welcome to our application',
'greeting' => 'Hello, :name!',
'nav' => [
'home' => 'Home',
'about' => 'About',
'settings' => 'Settings',
],
];
// lang/de/messages.php
return [
'welcome' => 'Willkommen in unserer Anwendung',
'greeting' => 'Hallo, :name!',
'nav' => [
'home' => 'Startseite',
'about' => 'Über uns',
'settings' => 'Einstellungen',
],
];จัดการพหูพจน์
Laravel ใช้ไวยากรณ์คั่นด้วยไปป์สำหรับรูปพหูพจน์ รูปง่ายคือ 'apples' => 'There is one apple|There are many apples' สำหรับช่วงชัดเจนใช้ 'apples' => '{0} No apples|{1} One apple|[2,*] :count apples' ฟังก์ชัน trans_choice() หรือ Str::plural() จะเลือกตามจำนวน
// lang/en.json
{
"Welcome back!": "Welcome back!",
"You have :count new notifications": "You have :count new notifications",
"Copyright :year :company": "Copyright :year :company"
}
// lang/de.json
{
"Welcome back!": "Willkommen zurück!",
"You have :count new notifications": "Sie haben :count neue Benachrichtigungen",
"Copyright :year :company": "Copyright :year :company"
}เพิ่มลำดับการใช้ภาษาสำรอง
ค่าสำรองในตัวของ Laravel ไปจากภาษาปัจจุบันถึง fallback_locale โดยไม่มีขั้นกลาง ผู้ใช้ pt-BR ที่ไม่มีคีย์จึงเห็นอังกฤษแทน pt-PT laravel-locale-chain แก้ด้วยการผสานคำแปลเชิงลึกจากลำดับที่กำหนดค่าได้ตอนโหลด
{{-- Simple translation --}}
<h1>{{ __('messages.welcome') }}</h1>
{{-- With variables --}}
<p>{{ __('messages.greeting', ['name' => $user->name]) }}</p>
{{-- Using @lang directive --}}
<h2>@lang('messages.nav.home')</h2>
{{-- JSON translations (use the string itself as key) --}}
<p>{{ __('Welcome back!') }}</p>
{{-- Pluralization --}}
{{ trans_choice('{0} No items|{1} One item|[2,*] :count items', $count) }}
{{-- Inside Blade components --}}
<x-button>{{ __('messages.nav.settings') }}</x-button>ทำให้การแปลเป็นอัตโนมัติ
เมื่อตั้งค่า Laravel i18n เสร็จแล้ว ให้แปลไฟล์ภาษาด้วย AI ชี้ผู้ช่วย AI ไปยังไฟล์ต้นฉบับ หรือใช้ CLI ของ i18n Agent ในไปป์ไลน์ CI/CD รองรับทั้งไฟล์ PHP และ JSON
// Install locale chain package
composer require i18n-agent/laravel-locale-chain
// config/locale-chain.php
return [
'chains' => [
'pt-BR' => ['pt-BR', 'pt', 'en'],
'zh-Hant-TW' => ['zh-Hant-TW', 'zh-Hant', 'zh', 'en'],
'es-419' => ['es-419', 'es', 'en'],
],
];
// In AppServiceProvider::boot()
use I18nAgent\LocaleChain\LocaleChainServiceProvider;
// The package automatically deep-merges translations
// across the chain: pt-BR -> pt -> enข้อผิดพลาดที่พบบ่อย
ตรรกะเอกพจน์/พหูพจน์ที่ฮาร์ดโค้ด
คำแปล JSON ไม่ถอยไปใช้ภาษาอื่น
ผสม PHP กับ JSON โดยไม่เข้าใจลำดับความสำคัญ
ลองใช้ i18n Agent ตอนนี้
ลากและวางไฟล์แปลของคุณที่นี่
JSON, YAML, PO, XML, CSV, Markdown, Properties
หรือคลิกเพื่อเลือกไฟล์
ภาษาเป้าหมาย