Last active
April 14, 2017 09:53
-
-
Save pingyen/bffc6b22fa56486d222985b4af717d6c to your computer and use it in GitHub Desktop.
Guess Country by Locale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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