How would I use php-intl to convert country codes to localized country names

流过昼夜 提交于 2019-12-08 08:43:02

问题


Hello I need to retrieve full country names based on a country code and a locale in PHP. Since php-intl is now supposed to be able to do this I want to use that.

nl_NL
nl -> Nederland
uk -> Verenigd Koninkrijk

en_UK
nl -> The Netherlands
uk -> United Kindom

I think I need to use php-intl resource bundles somehow, but am unable to find any useful examples.


回答1:


Exactly. The intl methods can help you with that.

You need to use Locale::getDisplayRegion

Object oriented style:

static string Locale::getDisplayRegion ( string $locale [, string $in_locale ] )

Procedural style:

string locale_get_display_region ( string $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.

So, in your case:

php > echo Locale::getDisplayRegion('nl_NL', 'nl_NL');
Nederland
php > echo Locale::getDisplayRegion('en_GB', 'nl_NL');
Verenigd Koninkrijk
php > echo Locale::getDisplayRegion('nl_NL', 'en_GB');
Netherlands
php > echo Locale::getDisplayRegion('en_GB', 'en_GB');
United Kingdom

Remember that 'GB' is used for the United Kingdom instead of 'UK'



来源:https://stackoverflow.com/questions/12515530/how-would-i-use-php-intl-to-convert-country-codes-to-localized-country-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!