イントロダクション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 return an array of keyed strings. For example:
<?php
return [
'welcome' => 'Welcome to our application',
];
Note: {note} For languages that differ by territory, you should name the language directories according to the ISO 15897. For example, "en_GB" should be used for British English rather than "en-gb".
地域によって異なる言語では、ISO 15897に従った言語ディレクトリを名付けるべきでしょう。たとえば、ブリティッシュ・イングリッシュでは、"en-gb"の代わりに、"en_GB"を使用するべきです。
ロケールの設定Configuring The Locale
アプリケーションのデフォルト言語はconfig/app.php
設定ファイルで指定します。この値はアプリケーションに合うように変更できます。さらにApp
ファサードのsetLocale
メソッドを使い、実行時にアクティブな言語を変更することもできます。The default language for your
application is stored in the
config/app.php
configuration file. 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) {
if (! in_array($locale, ['en', 'es', 'fr'])) {
abort(400);
}
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
translation string. 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
ファサードのgetLocale
やisLocale
を使います。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')) {
//
}
翻訳文字列の定義Defining Translation Strings
短縮キーの使用Using Short Keys
通常、翻訳文字列はresources/lang
ディレクトリ下のファイルに保存されています。このディレクトリにはアプリケーションでサポートする各言語のサブディレクトリを用意します。Typically, translation 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 return an array of keyed strings. For example:
<?php
// resources/lang/en/messages.php
return [
'welcome' => 'Welcome to our application',
];
翻訳文字列のキー使用Using Translation Strings As Keys
たくさんの翻訳が必要なアプリケーションでは、すべての文字列に「短いキー」を付けようとすると、ビューで参照する際、すぐにこんがらがってきます。そのため、Laravelでは翻訳文字列を「デフォルト」翻訳の文字列をキーとして利用できます。For applications with heavy translation requirements, defining every string with a "short key" can become quickly confusing when referencing them in your views. For this reason, Laravel also provides support for defining translation strings using the "default" translation of the string as the key.
翻訳文字列をキーとして使用する翻訳ファイルは、resources/lang
ディレクトリ下にJSONファイルとして保存します。たとえば、アプリケーションにスペイン語の翻訳がある場合、resources/lang/es.json
ファイルを作成します。Translation files that use
translation strings as keys are stored as JSON files
in the resources/lang
directory. For
example, if your application has a Spanish
translation, you should create a
resources/lang/es.json
file:
{
"I love programming.": "Me encanta programar."
}
翻訳文字列の取得Retrieving Translation Strings
言語ファイルから行を取得するには、__
ヘルパ関数を使用します。__
メソッドは、最初の引数として翻訳文字列のファイルとキーを受け付けます。例として、resources/lang/messages.php
原語ファイルからwelcome
翻訳文字列を取得してみましょう。You may retrieve lines from
language files using the __
helper
function. The __
method accepts the
file and key of the translation string as its first
argument. For example, let's retrieve the
welcome
translation string from the
resources/lang/messages.php
language
file:
echo __('messages.welcome');
echo __('I love programming.');
Bladeテンプレートエンジンを使用している場合は、{{
}}
記法で翻訳文字列をechoするか、@lang
ディレクティブを使用します。If you are using the Blade
templating engine[/docs/{{version}}/blade],
you may use the {{ }}
syntax to echo
the translation string or use the @lang
directive:
{{ __('messages.welcome') }}
@lang('messages.welcome')
指定した翻訳文字列が存在しない場合、__
関数は翻訳文字列キーをそのまま返します。そのため、上記の例で__
関数は、翻訳文字列がない場合にmessages.welcome
を返します。If the specified translation
string does not exist, the __
function
will return the translation string key. So, using
the example above, the __
function
would return messages.welcome
if the
translation string does not exist.
Note:
@lang
ディレクティブは出力をまったくエスケープしません。このディレクティブ使用時の出力のエスケープは、すべて皆さんの責任です。{note} The@lang
directive does not escape any output. You are fully responsible for escaping your own output when using this directive.
翻訳文字列中のパラメータ置換Replacing Parameters In Translation Strings
お望みならば、翻訳文字列にプレースホルダを定義できます。全プレースホルダは:
のプレフィックスを付けます。例として、welcomeメッセージにnameプレースホルダを定義してみましょう。If you wish, you may define
placeholders in your translation strings. All
placeholders are prefixed with a :
. For
example, you may define a welcome message with a
placeholder name:
'welcome' => 'Welcome, :name',
翻訳文字列を取得する時に、プレースホルダを置き換えるには、__
関数の第2引数として、置換文字の配列を渡します。To replace the placeholders when
retrieving a translation string, pass an array of
replacements as the second argument to the
__
function:
echo __('messages.welcome', ['name' => 'dayle']);
プレースホルダを全部大文字にするか、最初の一文字を大文字にすると、その方法に合わせて値が大文字に変換されます。If your placeholder 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',
複数の範囲を翻訳行に指定することで、もっと便利な複数形化のルールも簡単に作成できます。You may even create more complex pluralization rules which specify translation strings for multiple number ranges:
'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many',
複数形化オプションの翻訳文字列を定義したら、trans_choice
関数に「数」を指定し、行を取得します。以下の例では数が1より大きので、複数形の翻訳行が返されます。After defining a translation
string 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 translation string is
returned:
echo trans_choice('messages.apples', 10);
複数形化の文字列の中でも、プレースホルダを定義可能です。プレースホルダはtrans_choice
関数の第3引数に配列として指定します。You may also define placeholder
attributes in pluralization strings. These
placeholders may be replaced by passing an array as
the third argument to the trans_choice
function:
'minutes_ago' => '{1} :value minute ago|[2,*] :value minutes ago',
echo trans_choice('time.minutes_ago', 5, ['value' => 5]);
trans_choice
関数へ渡した整数値を表示したい場合は、:count
プレースホルダを使用します。If you would like to display the
integer value that was passed to the
trans_choice
function, you may use the
:count
placeholder:
'apples' => '{0} There are none|{1} There is one|[2,*] There are :count',
パッケージの言語ファイルのオーバーライド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 translation strings 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
translation strings you wish to override. Any
translation strings you don't override will still be
loaded from the package's original language
files.