The Intl Component

The Intl Component

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

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.

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.

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.

Accessing ICU Data

This component provides the following ICU data:

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 list:

  1. use Symfony\Component\Intl\Languages;
  2. \Locale::setDefault('en');
  3. $languages = Languages::getNames();
  4. // ('languageCode' => 'languageName')
  5. // => ['ab' => 'Abkhazian', 'ace' => 'Achinese', ...]
  6. $languages = Languages::getAlpha3Names();
  7. // ('languageCode' => 'languageName')
  8. // => ['abk' => 'Abkhazian', 'ace' => 'Achinese', ...]
  9. $language = Languages::getName('fr');
  10. // => 'French'
  11. $language = Languages::getAlpha3Name('fra');
  12. // => 'French'

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

  1. $languages = Languages::getNames('de');
  2. // => ['ab' => 'Abchasisch', 'ace' => 'Aceh', ...]
  3. $languages = Languages::getAlpha3Names('de');
  4. // => ['abk' => 'Abchasisch', 'ace' => 'Aceh', ...]
  5. $language = Languages::getName('fr', 'de');
  6. // => 'Französisch'
  7. $language = Languages::getAlpha3Name('fra', 'de');
  8. // => 'Französisch'

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

  1. $isValidLanguage = Languages::exists($languageCode);

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

  1. $isValidLanguage = Languages::alpha3CodeExists($alpha3Code);

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

  1. $alpha3Code = Languages::getAlpha3Code($alpha2Code);
  2. $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):

  1. use Symfony\Component\Intl\Scripts;
  2. \Locale::setDefault('en');
  3. $scripts = Scripts::getNames();
  4. // ('scriptCode' => 'scriptName')
  5. // => ['Adlm' => 'Adlam', 'Afak' => 'Afaka', ...]
  6. $script = Scripts::getName('Hans');
  7. // => 'Simplified'

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

  1. $scripts = Scripts::getNames('de');
  2. // => ['Adlm' => 'Adlam', 'Afak' => 'Afaka', ...]
  3. $script = Scripts::getName('Hans', 'de');
  4. // => 'Vereinfacht'

If the given script code doesn’t exist, the methods trigger a Symfony\Component\Intl\Exception\MissingResourceException. In addition to catching the exception, you can also check if a given script code is valid:

  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:

  1. use Symfony\Component\Intl\Countries;
  2. \Locale::setDefault('en');
  3. $countries = Countries::getNames();
  4. // ('alpha2Code' => 'countryName')
  5. // => ['AF' => 'Afghanistan', 'AX' => 'Åland Islands', ...]
  6. $countries = Countries::getAlpha3Names();
  7. // ('alpha3Code' => 'countryName')
  8. // => ['AFG' => 'Afghanistan', 'ALA' => 'Åland Islands', ...]
  9. $country = Countries::getName('GB');
  10. // => 'United Kingdom'
  11. $country = Countries::getAlpha3Name('NOR');
  12. // => 'Norway'

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

  1. $countries = Countries::getNames('de');
  2. // => ['AF' => 'Afghanistan', 'EG' => 'Ägypten', ...]
  3. $countries = Countries::getAlpha3Names('de');
  4. // => ['AFG' => 'Afghanistan', 'EGY' => 'Ägypten', ...]
  5. $country = Countries::getName('GB', 'de');
  6. // => 'Vereinigtes Königreich'
  7. $country = Countries::getAlpha3Name('GBR', 'de');
  8. // => 'Vereinigtes Königreich'

If the given country code doesn’t exist, the methods trigger a Symfony\Component\Intl\Exception\MissingResourceException. In addition to catching the exception, you can also check if a given country code is valid:

  1. $isValidCountry = Countries::exists($alpha2Code);

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

  1. $isValidCountry = Countries::alpha3CodeExists($alpha3Code);

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

  1. $alpha3Code = Countries::getAlpha3Code($alpha2Code);
  2. $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:

  1. use Symfony\Component\Intl\Locales;
  2. \Locale::setDefault('en');
  3. $locales = Locales::getNames();
  4. // ('localeCode' => 'localeName')
  5. // => ['af' => 'Afrikaans', 'af_NA' => 'Afrikaans (Namibia)', ...]
  6. $locale = Locales::getName('zh_Hans_MO');
  7. // => 'Chinese (Simplified, Macau SAR China)'

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

  1. $locales = Locales::getNames('de');
  2. // => ['af' => 'Afrikaans', 'af_NA' => 'Afrikaans (Namibia)', ...]
  3. $locale = Locales::getName('zh_Hans_MO', 'de');
  4. // => 'Chinesisch (Vereinfacht, Sonderverwaltungsregion Macau)'

If the given locale code doesn’t exist, the methods trigger a Symfony\Component\Intl\Exception\MissingResourceException. In addition to catching the exception, you can also check if a given locale code is valid:

  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.):

  1. use Symfony\Component\Intl\Currencies;
  2. \Locale::setDefault('en');
  3. $currencies = Currencies::getNames();
  4. // ('currencyCode' => 'currencyName')
  5. // => ['AFN' => 'Afghan Afghani', 'ALL' => 'Albanian Lek', ...]
  6. $currency = Currencies::getName('INR');
  7. // => 'Indian Rupee'
  8. $symbol = Currencies::getSymbol('INR');
  9. // => '₹'

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):

  1. // Indian rupee defines the same value for both
  2. $fractionDigits = Currencies::getFractionDigits('INR'); // returns: 2
  3. $cashFractionDigits = Currencies::getCashFractionDigits('INR'); // returns: 2
  4. // Swedish krona defines different values
  5. $fractionDigits = Currencies::getFractionDigits('SEK'); // returns: 2
  6. $cashFractionDigits = Currencies::getCashFractionDigits('SEK'); // returns: 0

New in version 5.3: The `getCashFractionDigits() method was introduced in Symfony 5.3.

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):

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

New in version 5.3: The `getCashRoundingIncrement() method was introduced in Symfony 5.3.

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

  1. $currencies = Currencies::getNames('de');
  2. // => ['AFN' => 'Afghanischer Afghani', 'EGP' => 'Ägyptisches Pfund', ...]
  3. $currency = Currencies::getName('INR', 'de');
  4. // => 'Indische Rupie'

If the given currency code doesn’t exist, the methods trigger a Symfony\Component\Intl\Exception\MissingResourceException. In addition to catching the exception, you can also check if a given currency code is valid:

  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:

  1. use Symfony\Component\Intl\Timezones;
  2. \Locale::setDefault('en');
  3. $timezones = Timezones::getNames();
  4. // ('timezoneID' => 'timezoneValue')
  5. // => ['America/Eirunepe' => 'Acre Time (Eirunepe)', 'America/Rio_Branco' => 'Acre Time (Rio Branco)', ...]
  6. $timezone = Timezones::getName('Africa/Nairobi');
  7. // => 'East Africa Time (Nairobi)'

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

  1. $timezones = Timezones::getNames('de');
  2. // => ['America/Eirunepe' => 'Acre-Zeit (Eirunepe)', 'America/Rio_Branco' => 'Acre-Zeit (Rio Branco)', ...]
  3. $timezone = Timezones::getName('Africa/Nairobi', 'de');
  4. // => '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 thegetName() method shown earlier:

  1. // unlike language codes, country codes are always uppercase (CL = Chile)
  2. $timezones = Timezones::forCountryCode('CL');
  3. // => ['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:

  1. $countryCode = Timezones::getCountryCode('America/Vancouver');
  2. // => $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) andgetGmtOffset() (which returns a string representation of the offset to display it to users):

  1. $offset = Timezones::getRawOffset('Etc/UTC'); // $offset = 0
  2. $offset = Timezones::getRawOffset('America/Buenos_Aires'); // $offset = -10800
  3. $offset = Timezones::getRawOffset('Asia/Katmandu'); // $offset = 20700
  4. $offset = Timezones::getGmtOffset('Etc/UTC'); // $offset = 'GMT+00:00'
  5. $offset = Timezones::getGmtOffset('America/Buenos_Aires'); // $offset = 'GMT-03:00'
  6. $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:

  1. // In 2019, the DST period in Madrid (Spain) went from March 31 to October 27
  2. $offset = Timezones::getRawOffset('Europe/Madrid', strtotime('March 31, 2019')); // $offset = 3600
  3. $offset = Timezones::getRawOffset('Europe/Madrid', strtotime('April 1, 2019')); // $offset = 7200
  4. $offset = Timezones::getGmtOffset('Europe/Madrid', strtotime('October 27, 2019')); // $offset = 'GMT+02:00'
  5. $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:

  1. $offset = Timezones::getGmtOffset('Europe/Madrid', strtotime('October 28, 2019'), 'ar'); // $offset = 'غرينتش+01:00'
  2. $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 Symfony\Component\Intl\Exception\MissingResourceException. In addition to catching the exception, you can also check if a given timezone ID is valid:

  1. $isValidTimezone = Timezones::exists($timezoneId);

Learn more

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.