Convert PHP Accept-Language to a more readable format

PHP returns Accept-Languages ​​($ _SERVER ['HTTP_ACCEPT_LANGUAGE']) in the format "en-US", "en", "de-AT". How to convert them to a more readable format?

"en-US" -> "English USA" "de-AT" -> "Γ–sterreichisch" 

Is there a general way / function or do I need to search for a database (what is called this format - is it ISO 639-1 with an optional region ?!)?

+4
source share
2 answers

If you use PHP 5.3.0 or higher and have an internationalization extension, you can use the Locale class or the corresponding procedural function:

 $dispname = Locale::getDisplayName('en-US'); echo $dispname; 

displays

 English (United States) 

(Inlt extension: http://pecl.php.net/package/intl )

+5
source

As you may have already discovered, the codes you use are ISO-3166, the easiest way seems to be to convert from a web service as follows:

 <?php $str = file_get_contents('http://opencountrycodes.appspot.com/xml/'); $xml = new SimpleXMLElement($str); $out = '$countries'." = array(\n"; foreach ($xml->country as $country) { $out .= "'{$country['code']}' => \"{$country['name']}\",\n"; } $out .= ");"; file_put_contents('country_names.php', $out); ?> 

I found this code at http://dragffy.com/blog/posts/creating-a-php-array-of-iso-3166-1-country-codes

+1
source

Source: https://habr.com/ru/post/1401998/


All Articles