
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() 函式和 @lang Blade 指令。三者都接收翻譯鍵和可選替換參數。在 PHP 程式碼和 Blade 範本中使用 __();在 Blade 中無需轉義 HTML 時使用 @lang。
// 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' => '有一個蘋果|有多個蘋果'。對於顯式範圍,使用 'apples' => '{0} 沒有蘋果|{1} 一個蘋果|[2,*] :count 個蘋果'。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 助手,或在 CI/CD 管線中使用 i18n Agent CLI。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
或點擊選擇檔案
目標語言