Readouble

Laravel master 多言語化

イントロダクションIntroduction

Laravelのローカリゼーション機能はアプリケーションで多言語をサポートできるように、様々な言語の文字列を便利に取得できる方法を提供します。 言語の文字列はresources/langディレクトリ下のファイルに保存します。このディレクトリの中にアプリケーションでサポートする言語のディレクトリを設置します。Laravel's localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application. Language strings are stored in files within the resources/lang directory. Within this directory there should be a subdirectory for each language supported by the application:

/resources
    /lang
        /en
            messages.php
        /es
            messages.php

全ての言語ファイルはシンプルにキーと文字列の配列をリターンします。例を見てください。All language files simply return an array of keyed strings. For example:

<?php

return [
    'welcome' => 'Welcome to our application'
];

ローカルの設定Configuring The Locale

アプリケーションのデフォルト言語はconfig/app.php設定ファイルで指定します。もちろんこの値はアプリケーションに合うように変更できます。さらにAppファサードのsetLocaleメソッドを使い、実行時にアクティブな言語を変更することもできます。The default language for your application is stored in the config/app.php configuration file. Of course, you may modify this value to suit the needs of your application. You may also change the active language at runtime using the setLocale method on the App facade:

Route::get('welcome/{locale}', function ($locale) {
    App::setLocale($locale);

    //
});

現時点のローカルとして指定した言語の言語行が存在しない場合に使用される、「フォールバック言語」を設定することもできます。デフォルト言語と同様に、フォールバック言語もconfig/app.php設定ファイルで指定されます。You may configure a "fallback language", which will be used when the active language does not contain a given language line. Like the default language, the fallback language is also configured in the config/app.php configuration file:

'fallback_locale' => 'en',

現在のローカル判定Determining The Current Locale

現在のローカルを調べたり、特定のローカルであるかを判定したりするには、AppファサードのgetLocaleisLocaleを使います。You may use the getLocale and isLocale methods on the App facade to determine the current locale or check if the locale is a given value:

$locale = App::getLocale();

if (App::isLocale('en')) {
    //
}

言語行の取得Retrieving Language Lines

言語ファイルから行を取得するにはtransヘルパ関数を使用します。transメソッドはファイルと言語行を第1引数として受け取ります。たとえばresources/lang/messages.php言語ファイルのwelcome言語行を取得してみましょう。You may retrieve lines from language files using the trans helper function. The trans method accepts the file and key of the language line as its first argument. For example, let's retrieve the welcome language line from the resources/lang/messages.php language file:

echo trans('messages.welcome');

もちろん、Bladeテンプレートエンジンを使っていれば、翻訳行を表示するために{{ }}記法が使えますし、さらに@langも使用できます。Of course if you are using the Blade templating engine[/docs/{{version}}/blade], you may use the {{ }} syntax to echo the language line or use the @lang directive:

{{ trans('messages.welcome') }}

@lang('messages.welcome')

指定した言語行が存在していない場合、trans関数はただ言語行のキーを返します。ですから前記の例で言語行が存在していなければ、trans関数はmessages.welcomeをリターンします。If the specified language line does not exist, the trans function will simply return the language line key. So, using the example above, the trans function would return messages.welcome if the language line does not exist.

言語行のパラメータ置換Replacing Parameters In Language Lines

お望みなら言語行でプレースホルダを定義することもできます。全プレースホルダは:のプレフィックスが付きます。たとえばnameプレースホルダ付きの歓迎メッセージの例をご覧ください。If you wish, you may define place-holders in your language lines. All place-holders are prefixed with a :. For example, you may define a welcome message with a place-holder name:

'welcome' => 'Welcome, :name',

取得する言語行のプレースホルダを置き換えるには、trans関数の第2引数に対象の配列を指定してください。To replace the place-holders when retrieving a language line, pass an array of replacements as the second argument to the trans function:

echo trans('messages.welcome', ['name' => 'dayle']);

プレースホルダを全部大文字にするか、最初の一文字を大文字にすると、その方法に合わせて値が大文字に変換されます。If your place-holder contains all capital letters, or only has its first letter capitalized, the translated value will be capitalized accordingly:

'welcome' => 'Welcome, :NAME', // Welcome, DAYLE
'goodbye' => 'Goodbye, :Name', // Goodbye, Dayle

複数形化Pluralization

複数形化は複雑な問題であり、異なった言語において多種複雑な複数形化のルールが存在しています。「パイプ」記号の縦線を使うことで、単数形の文字列と複数形の文字列を分けることができます。Pluralization is a complex problem, as different languages have a variety of complex rules for pluralization. By using a "pipe" character, you may distinguish singular and plural forms of a string:

'apples' => 'There is one apple|There are many apples',

言語行に複数形化オプションが存在する場合は、trans_choice関数に「数」を指定し、行を取得します。以下の例では数が1より大きので、複数形の言語行が返されます。After defining a language line that has pluralization options, you may use the trans_choice function to retrieve the line for a given "count". In this example, since the count is greater than one, the plural form of the language line is returned:

echo trans_choice('messages.apples', 10);

Laravelの翻訳にはパワフルなSymfony Translationコンポーネントを使用しているため、複数の範囲を言語行に指定することで、もっと便利な複数形化のルールも簡単に作成できます。Since the Laravel translator is powered by the Symfony Translation component, you may create even more complex pluralization rules which specify language lines for multiple number ranges:

'apples' => '{0} There are none|[1,19] There are some|[20,Inf] There are many',

パッケージの言語ファイルのオーバーライドOverriding Package Language Files

いくつかのパッケージではそれ自身の言語ファイルが提供されています。出力される文言を調整するためパッケージのコアをハックする代わりに、resources/lang/vendor/{パッケージ}/{ローカルコード}ディレクトリにファイルを設置することで、オーバーライドできます。Some packages may ship with their own language files. Instead of changing the package's core files to tweak these lines, you may override them by placing files in the resources/lang/vendor/{package}/{locale} directory.

たとえばskyrim/hearthfireという名前のパッケージが持っているmessages.phpの英語の言語行をオーバーライドする必要があるなら、resources/lang/vendor/hearthfire/en/messages.phpに言語ファイルを設置します。このファイルには置き換えたい言語行の定義だけを設置することができます。オーバーライドしなかった言語行は、パッケージのオリジナルな言語ファイル中の定義のままロードされます。So, for example, if you need to override the English language lines in messages.php for a package named skyrim/hearthfire, you should place a language file at: resources/lang/vendor/hearthfire/en/messages.php. Within this file, you should only define the language lines you wish to override. Any language lines you don't override will still be loaded from the package's original language files.

章選択

開発環境
ビューとテンプレート
公式パッケージ

設定

明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

Pagination和文
ペジネーション
ペギネーション
ページネーション
ページ付け
Scaffold和文
スカフォールド
スキャフォールド
型枠生成
本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作