基本的な使い方
インストール
次のコンソールコマンドでパッケージをインストールします。
composer require laravel-lang/common:^6.9
ロケールの管理
ロケールを追加する
利用できるロケールはロケールの一覧で確認できます。
コンソールコマンド lang:add でロケールを追加できます。
php artisan lang:add <locale>
ロケールを更新する
既存のロケールは、コンソールコマンド lang:update で更新できます。
php artisan lang:update
composer.json の scripts ブロックにある post-update-cmd セクションに、コマンドの呼び出しを追加することも推奨します。
{
"scripts": {
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force",
"@php artisan lang:update"
]
}
}
これにより、依存関係を更新するとローカライズファイルも自動で更新されます。php artisan lang:update を手動で実行する手間が省けます。
ルート
ローカライズルートグループを使う
ルートのグループ化を使わずに localizedRoute ヘルパーを呼び出すと、ロケールはクエリパラメーターになります。次の URL はその例です。
https://example.com/foo?locale=fr
パラメーター名、ルート名のプレフィックス、リダイレクトの設定は、config/localization.php ファイルの routes セクションで変更できます。
use Illuminate\Support\Facades\Route;
app('router')->localizedGroup(function () {
app('router')->get('foo/{bar}', function (string $bar) {
return response()->json([
$bar => __('Foo'),
]);
});
});
Route::localizedGroup(function () {
app('router')->get('foo/{bar}', function (string $bar) {
return response()->json([
$bar => __('Foo'),
]);
});
});
このメソッドを使うと、ローカライズされたルートグループのプレフィックスを簡単に管理できます。
このグループの主ミドルウェア LocalizationByParameterPrefix に加えて、LocalizationByCookie、LocalizationByHeader、LocalizationBySession が使われます。
以下の 3 つの方法のいずれかを使用できます。
use Illuminate\Support\Facades\Route;
use LaravelLang\Routes\Facades\LocalizationRoute;
app('router')->localizedGroup(fn () => ...)
Route::localizedGroup(fn () => ...)
LocalizationRoute::group(fn () => ...)
アプリケーションには {locale} のプレフィックス付きルートと、プレフィックスなしのルートの 2 種類が登録されます。次の表に例を示します。
| URL | パターン | 名前 |
|---|---|---|
| https://example.com/fr/foo/show | {locale}/foo/{bar} | localized.show |
| https://example.com/foo/show | foo/{bar} | show |
ルート名のプレフィックスは config/localization.php ファイルの routes セクションで変更できます。
localizedRoute('show', ['bar' => 'show']);
// https://example.com/fr/foo/show
localizedRoute('show', ['bar' => 'show', 'locale' => 'de']);
// https://example.com/de/foo/show
localizedRoute('localized.show', ['bar' => 'show']);
// https://example.com/fr/foo/show
localizedRoute('localized.show', ['bar' => 'show', 'locale' => 'de']);
// https://example.com/de/foo/show
route('localized.show', ['bar' => 'show', 'locale' => app()->getLocale()]);
// https://example.com/fr/foo/show
この機能は Laravel Idea に対応しています。Laravel Idea は PhpStorm 用のプラグインです。

例
- 既定のロケール
- 既定以外のロケール
- 未インストール
- 未知のロケール
リクエスト
curl -XGET https://example.com/fr/foo/show
レスポンス
- リダイレクト有効
- リダイレクト無効
### 302 Redirect
Location: https://example.com/foo/show
{
"show": "Toutes les données ont été chargées."
}
リクエスト
curl -XGET https://example.com/de/foo/show
レスポンス
{
"show": "Alle Ressourcen geladen."
}
リクエスト
curl -XGET https://example.com/as/foo/show
レスポンス
### 302 Redirect
Location: https://example.com/foo/show
リクエスト
curl -XGET https://example.com/qwerty/foo/show
レスポンス
### 302 Redirect
Location: https://example.com/foo/show
モデル
翻訳を取得する
現在のロケールの値を取得するには、モデルのプロパティを参照します。以下は例です。
use App\Models\Article;
$page = Article::first();
$page->title;
現在のロケールの値が見つからない場合、フォールバックロケールの値を返します。
getTranslation メソッドも使用できます。
use App\Models\Article;
$page = Article::first();
$page->getTranslation('title');
特定のロケールの値を取得するには、ロケールコードを第 2 引数に指定します。以下は例です。
use App\Models\Article;
$page = Article::first();
$page->getTranslation('title', 'fr');
// または
$page->getTranslation('title', Locale::French);
モデルの変更をデータベースに書き込んでいない場合でも、これらのメソッドは変更後の値を返します。
翻訳を設定する
1 つの値
アプリケーションの現在のロケールに翻訳を追加または更新するには、モデルのプロパティへ値を代入します。以下は例です。
use App\Models\Article;
$page = Article::first();
$page->title = 'New value';
新しい値を setTranslation メソッドで設定することもできます。
use App\Models\Article;
$page = Article::first();
$page->setTranslation('title', 'New value');
これらの方法は現在のロケールに新しい値を設定します。特定のロケールに値を設定するには、そのコードをメソッドの第 3 引数に渡します。以下は例です。
use App\Models\Article;
use LaravelLang\LocaleList\Locale;
$page = Article::first();
$page->setTranslation('title', 'New value', 'fr');
// または
$page->setTranslation('title', 'New value', Locale::French);
一括代入も可能です。
use App\Models\Article;
$page = Article::first();
$page->fill([
'title' => 'New value',
]);
これらのメソッドを呼び出すと、新しい値はモデルオブジェクトに設定されます。変更をデータベースに書き込むには、親モデルの save メソッドを呼び出します。
$page->save();
モデルの作成時にも値を設定できます。以下は例です。
use App\Models\Article;
$page = Article::first();
Article::create([
'title' => 'New value',
]);
複数の値
1 つのプロパティに複数のロケールの翻訳をすばやく設定するには、以下のいずれかの方法を使用します。
use App\Models\Article;
use Illuminate\Database\Eloquent\Model;
use LaravelLang\LocaleList\Locale;
use LaravelLang\Models\HasTranslations;
/** @var Model|HasTranslations $post */
$post = Article::first();
$titles = [
'fr' => 'Mettre à jour & continuer à éditer',
'en' => 'Update & Continue Editing',
'de' => 'Aktualisieren & Weiterbearbeiten',
];
$descriptions = collect([
Locale::French->value => 'Met à jour les informations et continue d\'éditer le message',
Locale::German->value => 'Aktualisiert die Informationen und bearbeitet den Beitrag weiter',
]);
// 方法 1
foreach ($titles as $locale => $value) {
$post->setTranslation('title', $value, $locale);
}
foreach ($descriptions as $locale => $value) {
$post->setTranslation('description', $value, $locale);
}
// 方法 2
$post->setTranslations('title', $titles);
$post->setTranslations('description', $descriptions);
一括代入では iterable なオブジェクトも渡せます。以下は例です。
$post->fill([
'title' => $titles,
'description' => $descriptions,
]);
Article::create([
'title' => $titles,
'description' => $descriptions,
]);
すべてのフィールドに同じロケールを指定する必要はありません。上記の例では、title にフランス語、英語、ドイツ語の値を、description にはフランス語とドイツ語の値のみを設定します。
JSON フォールバック
Laravel 11 の config/app.php 設定ファイルには providers セクションがありません。手動で追加できます。
インストール後、config/app.php 設定ファイルの providers セクションにサービスプロバイダーへの参照を追加します。
<?php
declare(strict_types=1);
use Illuminate\Support\ServiceProvider;
use Illuminate\Translation\TranslationServiceProvider as BaseTranslation;
use LaravelLang\JsonFallback\TranslationServiceProvider as JsonTranslation;
return [
'providers' => ServiceProvider::defaultProviders()->merge([
// その他のサービスプロバイダー
])->replace([
BaseTranslation::class => JsonTranslation::class,
])->toArray(),
];
これで JSON キーは選択したロケールに応じた値を返すようになります。