Question

In a database we have ISO 3166-1 alpha-2 country codes, e.g. GB for United Kingdom and AR for Argentina. Today we keep a database table with one column for these country codes and one for corresponding English country names.

We work in PHP (5.4.x) which has the intl extenion (wrapper for ICU library) so I thought I'd like to fetch the country names from this source which makes the application more maintainable and easier to internationalize as I easily can fetch country names in German, French or whatever we want in the future.

Locale::getDisplayRegion ( string $locale [, string $in_locale ] ) provides a region name from a locale. E.g.

php > echo Locale::getDisplayRegion('en-US', 'de') . PHP_EOL;
Vereinigte Staaten
php > echo Locale::getDisplayRegion('en-US', 'en') . PHP_EOL;
United States

The problem is that I do not have the language code, only the country code, so I'm not sure how to build up a proper locale.

It seems like I can get the region names by just prefixing with 'en_' for English, e.g

php > echo Locale::getDisplayRegion('en-CH', 'en') . PHP_EOL;
Switzerland
php > echo Locale::getDisplayRegion('en-CN', 'en') . PHP_EOL;
China
php > echo Locale::getDisplayRegion('en-AR', 'en') . PHP_EOL;
Argentina

However, this feels more like a hack and I'm not sure I really can rely on that?

I was thinking if I could somehow compose a locale using the country code (region / territory depending on terminology) and any of the valid languages for the country, but it appears it doesn't work as far as I've found at least:

php > echo Locale::composeLocale( array('region' => 'CN') ) . PHP_EOL;

php >

Any recommendations on how to go about this? I do not want to maintain my own database of country names or simply use an external PHP library for it when there's a built-in extension which seem to be a good source (ICU) already.

Thanks a lot in advance!

Was it helpful?

Solution

use und (unknown language). So 'und_' . 'AR' etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top