Today I needed to perform some small task regarding country detection by Accepted-Language header in PHP. It quickly turned out that there’s no (or I cannot find) the reliable source for such mapping. Finally I’ve found it here.
I parsed it to the php format, that can be used in the code:
Here is the parser source:
#!/usr/bin/php
<?
$handle = fopen('langcodes.txt', 'r');
$codes = array();
while (($code = fgetcsv($handle, 0, "t"))!==FALSE)
$codes[] = $code;
fclose($handle);
$langs = array();
echo "$countries = array(n";
foreach($codes as $code) {
$c = strtolower($code[0]);
echo " '".$c."'=>array(n";
echo " 'iso3'=>'".strtolower($code[1])."',n";
echo " 'name'=>'".$code[4]."',n";
echo " 'capital'=>'".$code[5]."',n";
echo " 'currencyCode'=>'".strtolower($code[10])."',n";
echo " 'currencyName'=>'".$code[11]."',n";
echo " ),n";
// parse language
$lang = explode(",", strtolower($code[15]));
foreach($lang as $l) {
if (!isset($langs[$l]))
$langs[$l] = array();
$langs[$l][] = $c;
}
}
echo ");n";
ksort($langs);
echo "$languages = array(n";
foreach($langs as $langKey=>$lang) {
echo " '".$langKey."'=>array(n ";
foreach($lang as $l) {
echo "'".$l."',";
}
echo "n ),n";
}
echo ");n";
this is a copy from my old blogspot blog