Skip to content

Instantly share code, notes, and snippets.

@pingyen pingyen/guessCountry.php

Last active Apr 14, 2017
Embed
What would you like to do?
Guess Country by Locale
<?php
function guess_country_by_locale ($locale) {
$tokens = explode('-', strtolower($locale));
if (isset($tokens[1]) === true) {
$country = $tokens[1];
if ($country !== '') {
switch ($country) {
case 'hans':
return 'CN';
case 'hant':
return 'TW';
case '419':
return 'MX';
default:
return strtoupper($country);
}
}
}
$lang = $tokens[0];
if (strlen($lang) !== 2 ||
ctype_alpha($lang) === false ||
$lang === 'eo') {
return false;
}
$map = array(
'aa' => 'ET',
'ab' => 'GE',
'am' => 'ET',
'ar' => 'SA',
'as' => 'IN',
'av' => 'RU',
'be' => 'BY',
'bn' => 'BD',
'br' => 'FR',
'bs' => 'BA',
'ca' => 'ES',
'ce' => 'RU',
'cs' => 'CZ',
'cy' => 'GB',
'da' => 'DK',
'el' => 'GR',
'en' => 'US',
'et' => 'EE',
'eu' => 'ES',
'fa' => 'IR',
'ga' => 'IE',
'gl' => 'ES',
'gn' => 'PY',
'gu' => 'IN',
'he' => 'IL',
'hi' => 'IN',
'hy' => 'AM',
'in' => 'ID',
'iw' => 'IL',
'ja' => 'JP',
'jv' => 'ID',
'ka' => 'GE',
'kk' => 'KZ',
'km' => 'KH',
'kn' => 'IN',
'ko' => 'KR',
'kw' => 'GB',
'ky' => 'KG',
'la' => 'IT',
'lb' => 'LU',
'lo' => 'LA',
'ml' => 'IN',
'mr' => 'IN',
'ms' => 'MY',
'my' => 'MM',
'nb' => 'NO',
'ne' => 'NP',
'nn' => 'NO',
'ny' => 'MW',
'om' => 'ET',
'or' => 'IN',
'os' => 'RU',
'pa' => 'IN',
'ps' => 'AF',
'sa' => 'IN',
'sd' => 'PK',
'si' => 'LK',
'sl' => 'SI',
'sm' => 'WS',
'sn' => 'ZW',
'sq' => 'AL',
'sr' => 'RS',
'ss' => 'SZ',
'su' => 'ID',
'sv' => 'SE',
'sw' => 'KE',
'ta' => 'IN',
'te' => 'IN',
'tg' => 'TJ',
'ti' => 'ET',
'tl' => 'PH',
'tk' => 'TM',
'ts' => 'ZA',
'tt' => 'RU',
'ug' => 'CN',
'uk' => 'UA',
'ur' => 'PK',
'vi' => 'VN',
'za' => 'CN',
'zh' => 'CN'
);
return isset($map[$lang]) === true ?
$map[$lang] :
strtoupper($lang);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.