イントロダクションIntroduction
LaravelのLangファサードは、アプリケーションを多言語に対応させるため、様々な言語の翻訳済み文字列を取得する方法を提供します。The Laravel Lang
facade provides a convenient way of retrieving
strings in various languages, allowing you to easily
support multiple languages within your
application.
言語ファイルLanguage Files
言語の文字列はresources/lang
ディレクトリー下のファイルに保存します。このディレクトリーの中にアプリケーションでサポートする言語のディレクトリーを設置します。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
言語ファイルのサンプルExample Language File
言語ファイルはキーと文字列の配列をリターンします。例えば:Language files simply return an array of keyed strings. For example:
<?php
return array(
'welcome' => 'Welcome to our application'
);
実行中にデフォルト言語を切り換えるChanging The Default Language At Runtime
デフォルトの言語はconfig/app.php
設定ファイルで指定されています。App::setLocale
メソッドを使用し、現時点で有効な言語を変更することができます。The default language for your
application is stored in the
config/app.php
configuration file. You
may change the active language at any time using the
App::setLocale
method:
App::setLocale('es');
フォールバック言語の設定Setting The Fallback Language
指定された言語行がアクティブな言語で存在しない場合に使用される、「フォールバック言語」を設定することもできます。デフォルト言語と同様に、フォールバック言語もconfig/app.php
設定ファイルで指定されます。You may also 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',
基本的な使用法Basic Usage
言語ファイルから文字列を取得するRetrieving Lines From A Language File
echo Lang::get('messages.welcome');
get
メソッドに渡している文字列の、最初の部分は言語ファイルの名前です。2番めの部分は取得しようとしている行の名前です。The first segment of the string
passed to the get
method is the name of
the language file, and the second is the name of the
line that should be retrieved.
注目: もし言語行が存在していない場合、
get
メソッドはキーの値をリターンします。Note: If a language line does not exist, the key will be returned by theget
method.
Lang::get
メソッドのエイリアスである、trans
ヘルパ関数も使用できます。You may also use the
trans
helper function, which is an
alias for the Lang::get
method.
echo trans('messages.welcome');
行の一部を置き換えるMaking Replacements In Lines
言語行の中にプレースホルダーを定義することもできます。You may also define place-holders in your language lines:
'welcome' => 'Welcome, :name',
それから、Lang::get
メソッドの第2引数として置換文字列を渡してください。Then, pass a second argument of
replacements to the Lang::get
method:
echo Lang::get('messages.welcome', array('name' => 'Dayle'));
言語ファイルに行が存在するか調べるDetermine If A Language File Contains A Line
if (Lang::has('messages.welcome'))
{
//
}
複数形化Pluralization
複数形化は複雑な問題であり、異なった言語において多種複雑な複数形化のルールが存在しています。あなたの言語ファイルでこれを簡単に管理できます。「パイプ」記号、縦線の文字を使うことで、単数形の文字列と、複数形の文字列を分けることができます。Pluralization is a complex problem, as different languages have a variety of complex rules for pluralization. You may easily manage this in your language files. By using a "pipe" character, you may separate the singular and plural forms of a string:
'apples' => 'There is one apple|There are many apples',
この形式の行を取得するにはLang::choice
メソッドを使用します。You may then use the
Lang::choice
method to retrieve the
line:
echo Lang::choice('messages.apples', 10);
言語を指定するために、ローカル引数を渡すこともできます。例えば、ロシア語(ru)を利用したい場合は:You may also supply a locale argument to specify the language. For example, if you want to use the Russian (ru) language:
echo Lang::choice('товар|товара|товаров', $count, array(), 'ru');
Laravelの翻訳にはSymfony Translation componentを使用しているため、もっと便利な複数形化のルールも簡単に作成できます。Since the Laravel translator is powered by the Symfony Translation component, you may also create more explicit pluralization rules easily:
'apples' => '{0} There are none|[1,19] There are some|[20,Inf] There are many',
バリデーションのローカリゼーションValidation
バリデーションのエラーとメッセージをローカライズする方法は、バリデーションの章を参照してください。For localization for validation errors and messages, take a look at the documentation on Validation[/docs/master/validation#localization].
パッケージの言語ファイルをオーバーライドするOverriding Package Language Files
多くのパッケージがそれ自身の言語ファイルと共に提供されています。それらを調整するためにパッケージのコアをハックする代わりに、resources/lang/packages/{ローカルコード}/{パッケージ}
ディレクトリーにファイルを設置することでオーバーライドできます。例えば、skyrim/hearthfire
パッケージの英語の言語行をオーバーライドする必要があるなら、resources/lang/packages/en/hearthfire/messages.php
に言語ファイルを設置します。このファイルには置き換えたい言語行のみを定義することができます。オーバーライドしなかった言語行は、パッケージの言語ファイル中の定義のままロードされます。Many packages ship with their own
language lines. Instead of hacking the package's
core files to tweak these lines, you may override
them by placing files in the
resources/lang/packages/{locale}/{package}
directory. So, for example, if you need to override
the English language lines in
messages.php
for a package named
skyrim/hearthfire
, you would place a
language file at:
resources/lang/packages/en/hearthfire/messages.php
.
In this file you would define only the language
lines you wish to override. Any language lines you
don't override will still be loaded from the
package's language files.