phplocalecountryintl

PHP intl country code 2 chars to country name


I'm trying to convert, using PHP, 2 characters country codes (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 localized country name without using webservices or external resources?

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


Solution

  • 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