PHP intl country code 2 chars to country name

Surt picture Surt · Jul 24, 2013 · Viewed 55.2k times · Source

I'm triying to convert, using PHP, 2 characters country code (ISO 3166-1) to country names, or intl country codes, so I can translate the country name.

Using Locale::getDisplayRegion needs locale codes for languages (nl_NL) and all I have is the 2 characters country code. I don't want to rely on Webservices, but I can think on the geoip extension (I can't find a solution using it anyway)

Is there any way to get the 2 chars country code and convert it to localiced country name in PHP without using webservices or external resources?

(I was using zend_locale some time ago, but zf2 does not have that functionality)

Answer

fromvega picture fromvega · Oct 10, 2014

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

http://php.net/manual/en/locale.getdisplayregion.php#115523