Locale::getDisplayRegion
locale_get_display_region
(PHP 5 >= 5.3.0, PECL intl >= 1.0.0)
Locale::getDisplayRegion -- locale_get_display_region — Returns an appropriately localized display name for region of the input locale
Description
Object oriented style
$locale
[, string $in_locale
] )Procedural style
$locale
[, string $in_locale
] )
Returns an appropriately localized display name for region of the input
locale. If is NULL
then the default locale is used.
Parameters
-
locale
-
The locale to return a display region for.
-
in_locale
-
Optional format locale to use to display the region name
Return Values
display name of the region for the $locale in the format appropriate for $in_locale.
Examples
Example #1 locale_get_display_region() example
<?php
echo locale_get_display_region('sl-Latn-IT-nedis', 'en');
echo ";\n";
echo locale_get_display_region('sl-Latn-IT-nedis', 'fr');
echo ";\n";
echo locale_get_display_region('sl-Latn-IT-nedis', 'de');
?>
Example #2 OO example
<?php
echo Locale::getDisplayRegion('sl-Latn-IT-nedis', 'en');
echo ";\n";
echo Locale::getDisplayRegion('sl-Latn-IT-nedis', 'fr');
echo ";\n";
echo Locale::getDisplayRegion('sl-Latn-IT-nedis', 'de');
?>
The above example will output:
Italy; Italie; Italien
See Also
- locale_get_display_name() - Returns an appropriately localized display name for the input locale
- locale_get_display_language() - Returns an appropriately localized display name for language of the inputlocale
- locale_get_display_script() - Returns an appropriately localized display name for script of the input locale
- locale_get_display_variant() - Returns an appropriately localized display name for variants of the input locale
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Поддержка языков и кодировок
- Internationalization Functions
- Функция Locale::acceptFromHttp() - Tries to find out best available locale based on HTTP "Accept-Language" header
- Функция Locale::canonicalize() - Canonicalize the locale string
- Функция Locale::composeLocale() - Returns a correctly ordered and delimited locale ID
- Функция Locale::filterMatches() - Checks if a language tag filter matches with locale
- Функция Locale::getAllVariants() - Gets the variants for the input locale
- Функция Locale::getDefault() - Gets the default locale value from the INTL global 'default_locale'
- Функция Locale::getDisplayLanguage() - Returns an appropriately localized display name for language of the inputlocale
- Функция Locale::getDisplayName() - Returns an appropriately localized display name for the input locale
- Функция Locale::getDisplayRegion() - Returns an appropriately localized display name for region of the input locale
- Функция Locale::getDisplayScript() - Returns an appropriately localized display name for script of the input locale
- Функция Locale::getDisplayVariant() - Returns an appropriately localized display name for variants of the input locale
- Функция Locale::getKeywords() - Gets the keywords for the input locale
- Функция Locale::getPrimaryLanguage() - Gets the primary language for the input locale
- Функция Locale::getRegion() - Gets the region for the input locale
- Функция Locale::getScript() - Gets the script for the input locale
- Функция Locale::lookup() - Searches the language tag list for the best match to the language
- Функция Locale::parseLocale() - Returns a key-value array of locale ID subtag elements.
- Функция Locale::setDefault() - sets the default runtime locale
Коментарии
You don't have to have a fully-formed locale for the first parameter. This makes the function useful for getting the country name from any locale:
<?php
var_dump(Locale::getDisplayRegion('-US', 'fr'));
//Returns
string 'États-Unis' (length=11)
?>
in fact , I would do exactly the opposite : find the country code (iso3166) from the country name. Found on the web, one solution is to use a table of correspondence between the country code and the country name for each language.
There are too many countries and language code for this type of table.
With a PHP function , the convertion can be performed as well:
function countryNameToISO3166($country_name, $language) {
if (strlen($language) != 2) {
//Language must be on 2 caracters
return NULL;
}
//Set uppercase if never
$language = strtoupper($language);
$countrycode_list = array('AF', 'AX', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM', 'AW', 'AU', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BQ', 'BA', 'BW', 'BV', 'BR', 'IO', 'BN', 'BG', 'BF', 'BI', 'KH', 'CM', 'CA', 'CV', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC', 'CO', 'KM', 'CG', 'CD', 'CK', 'CR', 'CI', 'HR', 'CU', 'CW', 'CY', 'CZ', 'DK', 'DJ', 'DM', 'DO', 'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'ET', 'FK', 'FO', 'FJ', 'FI', 'FR', 'GF', 'PF', 'TF', 'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 'GG', 'GN', 'GW', 'GY', 'HT', 'HM', 'VA', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IM', 'IL', 'IT', 'JM', 'JP', 'JE', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA', 'LV', 'LB', 'LS', 'LR', 'LY', 'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX', 'FM', 'MD', 'MC', 'MN', 'ME', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'NC', 'NZ', 'NI', 'NE', 'NG', 'NU', 'NF', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS', 'PA', 'PG', 'PY', 'PE', 'PH', 'PN', 'PL', 'PT', 'PR', 'QA', 'RE', 'RO', 'RU', 'RW', 'BL', 'SH', 'KN', 'LC', 'MF', 'PM', 'VC', 'WS', 'SM', 'ST', 'SA', 'SN', 'RS', 'SC', 'SL', 'SG', 'SX', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS', 'SS', 'ES', 'LK', 'SD', 'SR', 'SJ', 'SZ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL', 'TG', 'TK', 'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE', 'GB', 'US', 'UM', 'UY', 'UZ', 'VU', 'VE', 'VN', 'VG', 'VI', 'WF', 'EH', 'YE', 'ZM', 'ZW');
$ISO3166 = NULL;
//Loop all country codes
foreach ($countrycode_list as $countrycode) {
$locale_cc = Locale::getDisplayRegion('-' . $countrycode, $language);
//Case insensitive
if (strcasecmp($country_name, $locale_cc) == 0) {
$ISO3166 = $countrycode;
break;
}
}
//return NULL if not found or country code
return $ISO3166;
}
Tests
$cc=countryNameToISO3166('Costa Rica','US');
echo '<br>(English) Country code for Costa Rica is '.$cc;
$cc=countryNameToISO3166('Royaume-Uni','FR');
echo '<br>(Français) Country code for Royaume-Uni is '.$cc;
$cc=countryNameToISO3166('Frankreich','DE');
echo '<br>(Deutsch) Country code for Frankreich is '.$cc;
//Wrong language
$cc=countryNameToISO3166('Royaume-Uni','US');
echo '<br>(English) Country code for Royaume-Uni is '.var_dump($cc);
returns
(English) Country code for Costa Rica is CR
(Français) Country code for Royaume-Uni is GB
(Deutsch) Country code for Frankreich is FR
(English) Country code for Royaume-Uni is NULL
Hope it can help someone other
http://www.fobec.com