The Intl Component

This component provides access to the localization data of the ICU library.

このコンポーネントは、ICU ライブラリのローカリゼーション データへのアクセスを提供します。

Caution

注意

The replacement layer is limited to the en locale. If you want to use other locales, you should install the intl extension. There is no conflict between the two because, even if you use the extension, this package can still be useful to access the ICU data.

置換レイヤーは en ロケールに限定されます。他のロケールを使用する場合は、intl 拡張機能をインストールする必要があります。拡張機能を使用したとしても、このパッケージは ICU データへのアクセスに役立つ可能性があるため、両者の間に競合はありません。

See also

こちらもご覧ください

This article explains how to use the Intl features as an independent component in any PHP application. Read the Translations article to learn about how to internationalize and manage the user locale in Symfony applications.

この記事では、任意の PHP アプリケーションで Intl 機能を独立したコンポーネントとして使用する方法について説明します。 Translations の記事を読んで、Symfony アプリケーションでユーザー ロケールを国際化および管理する方法を学んでください。

Installation

1
$ composer require symfony/intl

Note

ノート

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

このコンポーネントを Symfony アプリケーションの外部にインストールする場合は、Composer が提供するクラス自動ロード メカニズムを有効にするために、コード内に vendor/autoload.php ファイルを必要とする必要があります。詳細については、この記事をお読みください。

Accessing ICU Data

This component provides the following ICU data:

このコンポーネントは、次の ICU データを提供します。

Language and Script Names

The Languages class provides access to the name of all languages according to the ISO 639-1 alpha-2 list and the ISO 639-2 alpha-3 (2T) list:

Languages クラスは、ISO 639-1 alpha-2 リストおよび ISO 639-2 alpha-3 (2T) リストに従って、すべての言語の名前へのアクセスを提供します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use Symfony\Component\Intl\Languages;

\Locale::setDefault('en');

$languages = Languages::getNames();
// ('languageCode' => 'languageName')
// => ['ab' => 'Abkhazian', 'ace' => 'Achinese', ...]

$languages = Languages::getAlpha3Names();
// ('languageCode' => 'languageName')
// => ['abk' => 'Abkhazian', 'ace' => 'Achinese', ...]

$language = Languages::getName('fr');
// => 'French'

$language = Languages::getAlpha3Name('fra');
// => 'French'

All methods accept the translation locale as the last, optional parameter, which defaults to the current default locale:

すべてのメソッドは、翻訳ロケールを最後のオプション パラメータとして受け入れます。これは、現在のデフォルト ロケールにデフォルト設定されます。
1
2
3
4
5
6
7
8
9
10
11
$languages = Languages::getNames('de');
// => ['ab' => 'Abchasisch', 'ace' => 'Aceh', ...]

$languages = Languages::getAlpha3Names('de');
// => ['abk' => 'Abchasisch', 'ace' => 'Aceh', ...]

$language = Languages::getName('fr', 'de');
// => 'Französisch'

$language = Languages::getAlpha3Name('fra', 'de');
// => 'Französisch'

If the given locale doesn't exist, the methods trigger a MissingResourceException. In addition to catching the exception, you can also check if a given language code is valid:

指定されたロケールが存在しない場合、メソッドは aMissingResourceException をトリガーします。例外をキャッチするだけでなく、特定の言語コードが有効かどうかを確認することもできます。
1
$isValidLanguage = Languages::exists($languageCode);

Or if you have an alpha3 language code you want to check:

または、確認したい alpha3 言語コードがある場合:
1
$isValidLanguage = Languages::alpha3CodeExists($alpha3Code);

You may convert codes between two-letter alpha2 and three-letter alpha3 codes:

2 文字の alpha2 コードと 3 文字の alpha3 コードの間でコードを変換できます。
1
2
3
$alpha3Code = Languages::getAlpha3Code($alpha2Code);

$alpha2Code = Languages::getAlpha2Code($alpha3Code);

The Scripts class provides access to the optional four-letter script code that can follow the language code according to the Unicode ISO 15924 Registry (e.g. HANS in zh_HANS for simplified Chinese and HANT in zh_HANT for traditional Chinese):

Scripts クラスは、オプションの 4 文字のスクリプト コードへのアクセスを提供します。これは、Unicode ISO 15924 レジストリに従って言語コードに従うことができます (例: 簡体字中国語の場合は zh_HANS の HANS、繁体字中国語の場合は zh_HANT の HANT)。
1
2
3
4
5
6
7
8
9
10
use Symfony\Component\Intl\Scripts;

\Locale::setDefault('en');

$scripts = Scripts::getNames();
// ('scriptCode' => 'scriptName')
// => ['Adlm' => 'Adlam', 'Afak' => 'Afaka', ...]

$script = Scripts::getName('Hans');
// => 'Simplified'

All methods accept the translation locale as the last, optional parameter, which defaults to the current default locale:

すべてのメソッドは、翻訳ロケールを最後のオプション パラメータとして受け入れます。これは、現在のデフォルト ロケールにデフォルト設定されます。
1
2
3
4
5
$scripts = Scripts::getNames('de');
// => ['Adlm' => 'Adlam', 'Afak' => 'Afaka', ...]

$script = Scripts::getName('Hans', 'de');
// => 'Vereinfacht'

If the given script code doesn't exist, the methods trigger a MissingResourceException. In addition to catching the exception, you can also check if a given script code is valid:

指定されたスクリプト コードが存在しない場合、メソッドは aMissingResourceException をトリガーします。例外をキャッチするだけでなく、特定のスクリプト コードが有効かどうかを確認することもできます。
1
$isValidScript = Scripts::exists($scriptCode);

Country Names

The Countries class provides access to the name of all countries according to the ISO 3166-1 alpha-2 list and the ISO 3166-1 alpha-3 list of officially recognized countries and territories:

Country クラスは、ISO 3166-1 alpha-2 リストおよび公式に認められた国と地域の ISO 3166-1 alpha-3 リストに従って、すべての国の名前へのアクセスを提供します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use Symfony\Component\Intl\Countries;

\Locale::setDefault('en');

$countries = Countries::getNames();
// ('alpha2Code' => 'countryName')
// => ['AF' => 'Afghanistan', 'AX' => 'Åland Islands', ...]

$countries = Countries::getAlpha3Names();
// ('alpha3Code' => 'countryName')
// => ['AFG' => 'Afghanistan', 'ALA' => 'Åland Islands', ...]

$country = Countries::getName('GB');
// => 'United Kingdom'

$country = Countries::getAlpha3Name('NOR');
// => 'Norway'

All methods accept the translation locale as the last, optional parameter, which defaults to the current default locale:

すべてのメソッドは、翻訳ロケールを最後のオプション パラメータとして受け入れます。これは、現在のデフォルト ロケールにデフォルト設定されます。
1
2
3
4
5
6
7
8
9
10
11
$countries = Countries::getNames('de');
// => ['AF' => 'Afghanistan', 'EG' => 'Ägypten', ...]

$countries = Countries::getAlpha3Names('de');
// => ['AFG' => 'Afghanistan', 'EGY' => 'Ägypten', ...]

$country = Countries::getName('GB', 'de');
// => 'Vereinigtes Königreich'

$country = Countries::getAlpha3Name('GBR', 'de');
// => 'Vereinigtes Königreich'

If the given country code doesn't exist, the methods trigger a MissingResourceException. In addition to catching the exception, you can also check if a given country code is valid:

指定された国コードが存在しない場合、メソッドは aMissingResourceException をトリガーします。例外をキャッチするだけでなく、特定の国コードが有効かどうかを確認することもできます。
1
$isValidCountry = Countries::exists($alpha2Code);

Or if you have an alpha3 country code you want to check:

または、確認したい alpha3 国コードがある場合:
1
$isValidCountry = Countries::alpha3CodeExists($alpha3Code);

You may convert codes between two-letter alpha2 and three-letter alpha3 codes:

2 文字の alpha2 コードと 3 文字の alpha3 コードの間でコードを変換できます。
1
2
3
$alpha3Code = Countries::getAlpha3Code($alpha2Code);

$alpha2Code = Countries::getAlpha2Code($alpha3Code);

Locales

A locale is the combination of a language, a region and some parameters that define the interface preferences of the user. For example, "Chinese" is the language and zh_Hans_MO is the locale for "Chinese" (language) + "Simplified" (script) + "Macau SAR China" (region). The Locales class provides access to the name of all locales:

ロケールは、言語、地域、およびユーザーのインターフェイス設定を定義するいくつかのパラメーターの組み合わせです。たとえば、"中国語" は言語であり、zh_Hans_MO は "中国語" (言語) + "簡体字" (スクリプト) + "マカオ特別行政区" (地域) のロケールです。 Locales クラスは、すべてのロケールの名前へのアクセスを提供します。
1
2
3
4
5
6
7
8
9
10
use Symfony\Component\Intl\Locales;

\Locale::setDefault('en');

$locales = Locales::getNames();
// ('localeCode' => 'localeName')
// => ['af' => 'Afrikaans', 'af_NA' => 'Afrikaans (Namibia)', ...]

$locale = Locales::getName('zh_Hans_MO');
// => 'Chinese (Simplified, Macau SAR China)'

All methods accept the translation locale as the last, optional parameter, which defaults to the current default locale:

すべてのメソッドは、翻訳ロケールを最後のオプション パラメータとして受け入れます。これは、現在のデフォルト ロケールにデフォルト設定されます。
1
2
3
4
5
$locales = Locales::getNames('de');
// => ['af' => 'Afrikaans', 'af_NA' => 'Afrikaans (Namibia)', ...]

$locale = Locales::getName('zh_Hans_MO', 'de');
// => 'Chinesisch (Vereinfacht, Sonderverwaltungsregion Macau)'

If the given locale code doesn't exist, the methods trigger a MissingResourceException. In addition to catching the exception, you can also check if a given locale code is valid:

指定されたロケール コードが存在しない場合、メソッドは aMissingResourceException をトリガーします。例外をキャッチするだけでなく、特定のロケール コードが有効かどうかを確認することもできます。
1
$isValidLocale = Locales::exists($localeCode);

Currencies

The Currencies class provides access to the name of all currencies as well as some of their information (symbol, fraction digits, etc.):

Currencies クラスは、すべての通貨の名前とその情報 (シンボル、端数など) へのアクセスを提供します。
1
2
3
4
5
6
7
8
9
10
11
12
13
use Symfony\Component\Intl\Currencies;

\Locale::setDefault('en');

$currencies = Currencies::getNames();
// ('currencyCode' => 'currencyName')
// => ['AFN' => 'Afghan Afghani', 'ALL' => 'Albanian Lek', ...]

$currency = Currencies::getName('INR');
// => 'Indian Rupee'

$symbol = Currencies::getSymbol('INR');
// => '₹'

The fraction digits methods return the number of decimal digits to display when formatting numbers with this currency. Depending on the currency, this value can change if the number is used in cash transactions or in other scenarios (e.g. accounting):

小数桁数メソッドは、この通貨で数値をフォーマットするときに表示する 10 進数の桁数を返します。通貨に応じて、数値が現金取引または他のシナリオ (会計など) で使用される場合、この値は変わる可能性があります。
1
2
3
4
5
6
7
// Indian rupee defines the same value for both
$fractionDigits = Currencies::getFractionDigits('INR');         // returns: 2
$cashFractionDigits = Currencies::getCashFractionDigits('INR'); // returns: 2

// Swedish krona defines different values
$fractionDigits = Currencies::getFractionDigits('SEK');         // returns: 2
$cashFractionDigits = Currencies::getCashFractionDigits('SEK'); // returns: 0

Some currencies require to round numbers to the nearest increment of some value (e.g. 5 cents). This increment might be different if numbers are formatted for cash transactions or other scenarios (e.g. accounting):

一部の通貨では、数値を特定の値の最も近い増分 (5 セントなど) に丸める必要があります。この増分は、数値が現金取引またはその他のシナリオ (会計など) 用にフォーマットされている場合は異なる場合があります。
1
2
3
4
5
6
7
8
9
// Indian rupee defines the same value for both
$roundingIncrement = Currencies::getRoundingIncrement('INR');         // returns: 0
$cashRoundingIncrement = Currencies::getCashRoundingIncrement('INR'); // returns: 0

// Canadian dollar defines different values because they have eliminated
// the smaller coins (1-cent and 2-cent) and prices in cash must be rounded to
// 5 cents (e.g. if price is 7.42 you pay 7.40; if price is 7.48 you pay 7.50)
$roundingIncrement = Currencies::getRoundingIncrement('CAD');         // returns: 0
$cashRoundingIncrement = Currencies::getCashRoundingIncrement('CAD'); // returns: 5

All methods (except for getFractionDigits(), getCashFractionDigits(), getRoundingIncrement() and getCashRoundingIncrement()) accept the translation locale as the last, optional parameter, which defaults to the current default locale:

すべてのメソッド (getFractionDigits()、getCashFractionDigits()、getRoundingIncrement()、および getCashRoundingIncrement() を除く) は、翻訳ロケールを最後のオプション パラメータとして受け入れます。このデフォルトは現在のデフォルト ロケールです。
1
2
3
4
5
$currencies = Currencies::getNames('de');
// => ['AFN' => 'Afghanischer Afghani', 'EGP' => 'Ägyptisches Pfund', ...]

$currency = Currencies::getName('INR', 'de');
// => 'Indische Rupie'

If the given currency code doesn't exist, the methods trigger a MissingResourceException. In addition to catching the exception, you can also check if a given currency code is valid:

指定された通貨コードが存在しない場合、メソッドは aMissingResourceException をトリガーします。例外をキャッチするだけでなく、特定の通貨コードが有効かどうかを確認することもできます。
1
$isValidCurrency = Currencies::exists($currencyCode);

Timezones

The Timezones class provides several utilities related to timezones. First, you can get the name and values of all timezones in all languages:

Timezones クラスは、タイムゾーンに関連するいくつかのユーティリティを提供します。まず、すべての言語のすべてのタイムゾーンの名前と値を取得できます。
1
2
3
4
5
6
7
8
9
10
use Symfony\Component\Intl\Timezones;

\Locale::setDefault('en');

$timezones = Timezones::getNames();
// ('timezoneID' => 'timezoneValue')
// => ['America/Eirunepe' => 'Acre Time (Eirunepe)', 'America/Rio_Branco' => 'Acre Time (Rio Branco)', ...]

$timezone = Timezones::getName('Africa/Nairobi');
// => 'East Africa Time (Nairobi)'

All methods accept the translation locale as the last, optional parameter, which defaults to the current default locale:

すべてのメソッドは、翻訳ロケールを最後のオプション パラメータとして受け入れます。これは、現在のデフォルト ロケールにデフォルト設定されます。
1
2
3
4
5
$timezones = Timezones::getNames('de');
// => ['America/Eirunepe' => 'Acre-Zeit (Eirunepe)', 'America/Rio_Branco' => 'Acre-Zeit (Rio Branco)', ...]

$timezone = Timezones::getName('Africa/Nairobi', 'de');
// => 'Ostafrikanische Zeit (Nairobi)'

You can also get all the timezones that exist in a given country. The forCountryCode() method returns one or more timezone IDs, which you can translate into any locale with the getName() method shown earlier:

特定の国に存在するすべてのタイムゾーンを取得することもできます。 forCountryCode() メソッドは 1 つ以上のタイムゾーン ID を返します。これは、前に示した getName() メソッドを使用して任意のロケールに変換できます。
1
2
3
// unlike language codes, country codes are always uppercase (CL = Chile)
$timezones = Timezones::forCountryCode('CL');
// => ['America/Punta_Arenas', 'America/Santiago', 'Pacific/Easter']

The reverse lookup is also possible thanks to the getCountryCode() method, which returns the code of the country where the given timezone ID belongs to:

getCountryCode() メソッドのおかげで、逆引きも可能です。このメソッドは、指定されたタイムゾーン ID が属する国のコードを返します。
1
2
$countryCode = Timezones::getCountryCode('America/Vancouver');
// => $countryCode = 'CA' (CA = Canada)

The UTC/GMT time offsets of all timezones are provided by getRawOffset() (which returns an integer representing the offset in seconds) and getGmtOffset() (which returns a string representation of the offset to display it to users):

すべてのタイムゾーンの UTC/GMT 時間オフセットは、getRawOffset() (秒単位のオフセットを表す整数を返す) と getGmtOffset() (ユーザーに表示するオフセットの文字列表現を返す) によって提供されます。
1
2
3
4
5
6
7
$offset = Timezones::getRawOffset('Etc/UTC');              // $offset = 0
$offset = Timezones::getRawOffset('America/Buenos_Aires'); // $offset = -10800
$offset = Timezones::getRawOffset('Asia/Katmandu');        // $offset = 20700

$offset = Timezones::getGmtOffset('Etc/UTC');              // $offset = 'GMT+00:00'
$offset = Timezones::getGmtOffset('America/Buenos_Aires'); // $offset = 'GMT-03:00'
$offset = Timezones::getGmtOffset('Asia/Katmandu');        // $offset = 'GMT+05:45'

The timezone offset can vary in time because of the daylight saving time (DST) practice. By default these methods use the time() PHP function to get the current timezone offset value, but you can pass a timestamp as their second arguments to get the offset at any given point in time:

タイムゾーン オフセットは、夏時間 (DST) の実施により、時間とともに変化する可能性があります。デフォルトでは、これらのメソッドは time() PHP 関数を使用して現在のタイムゾーン オフセット値を取得しますが、2 番目の引数としてタイムスタンプを渡して、任意の時点でのオフセットを取得できます。
1
2
3
4
5
// In 2019, the DST period in Madrid (Spain) went from March 31 to October 27
$offset = Timezones::getRawOffset('Europe/Madrid', strtotime('March 31, 2019'));   // $offset = 3600
$offset = Timezones::getRawOffset('Europe/Madrid', strtotime('April 1, 2019'));    // $offset = 7200
$offset = Timezones::getGmtOffset('Europe/Madrid', strtotime('October 27, 2019')); // $offset = 'GMT+02:00'
$offset = Timezones::getGmtOffset('Europe/Madrid', strtotime('October 28, 2019')); // $offset = 'GMT+01:00'

The string representation of the GMT offset can vary depending on the locale, so you can pass the locale as the third optional argument:

GMT オフセットの文字列表現はロケールによって異なる場合があるため、3 番目のオプション引数としてロケールを渡すことができます。
1
2
$offset = Timezones::getGmtOffset('Europe/Madrid', strtotime('October 28, 2019'), 'ar'); // $offset = 'غرينتش+01:00'
$offset = Timezones::getGmtOffset('Europe/Madrid', strtotime('October 28, 2019'), 'dz'); // $offset = 'ཇི་ཨེམ་ཏི་+01:00'

If the given timezone ID doesn't exist, the methods trigger a MissingResourceException. In addition to catching the exception, you can also check if a given timezone ID is valid:

指定されたタイムゾーン ID が存在しない場合、メソッドは aMissingResourceException をトリガーします。例外をキャッチするだけでなく、指定されたタイムゾーン ID が有効かどうかを確認することもできます。
1
$isValidTimezone = Timezones::exists($timezoneId);

Emoji Transliteration

6.2

6.2

The Emoji transliteration feature was introduced in Symfony 6.2.

絵文字音訳機能は Symfony 6.2 で導入されました。

The EmojiTransliterator class provides a utility to translate emojis into their textual representation in all languages based on the Unicode CLDR dataset:

EmojiTransliterator クラスは、Unicode CLDR データセットに基づいて、すべての言語で絵文字をテキスト表現に変換するユーティリティを提供します。
1
2
3
4
5
6
7
8
9
10
11
use Symfony\Component\Intl\Transliterator\EmojiTransliterator;

// describe emojis in English
$transliterator = EmojiTransliterator::create('en');
$transliterator->transliterate('Menus with 🍕 or 🍝');
// => 'Menus with pizza or spaghetti'

// describe emojis in Ukrainian
$transliterator = EmojiTransliterator::create('uk');
$transliterator->transliterate('Menus with 🍕 or 🍝');
// => 'Menus with піца or спагеті'

The EmojiTransliterator class also provides two extra catalogues: github and slack that converts any emojis to the corresponding short code in those platforms:

EmojiTransliterator クラスは、2 つの追加のカタログも提供します: githuband slack は、絵文字をそれらのプラットフォームの対応する短いコードに変換します。
1
2
3
4
5
6
7
8
9
10
11
use Symfony\Component\Intl\Transliterator\EmojiTransliterator;

// describe emojis in Slack short code
$transliterator = EmojiTransliterator::create('slack');
$transliterator->transliterate('Menus with 🥗 or 🧆');
// => 'Menus with :green_salad: or :falafel:'

// describe emojis in Github short code
$transliterator = EmojiTransliterator::create('github');
$transliterator->transliterate('Menus with 🥗 or 🧆');
// => 'Menus with :green_salad: or :falafel:'

Tip

ヒント

Combine this emoji transliterator with the Symfony String slugger to improve the slugs of contents that include emojis (e.g. for URLs).

この絵文字トランスリテレータを Symfony String slugger と組み合わせて、絵文字を含むコンテンツ (URL など) のスラッグを改善します。

Learn more