If I'm adding like-box or page-plugin so I have to choose the language in which it will be displayed.
But what if I have multilanguage-site system in PHP which remembers only country codes (en,es...). So I probably need a table like this, but with all languages:
en => en_US
es => es_ES ...
Do you know about some?
Facebook is using country codes, but this codes are a bit more specific so they are mostly called Locale ID (language and country code).
You could just grab the XML from facebook with all locale ids used by them:
<locale>
<englishName>English (UK)</englishName>
<codes>
<code>
<standard>
<name>FB</name>
<representation>en_GB</representation>
</standard>
</code>
</codes>
</locale>
You can find the whole file here ( https://www.facebook.com/translations/FacebookLocales.xml )
BUT: The Problem here is, that you cannot refer from en
to en_US
or en_GB
. So the mapping from 2 to 4 letters won't work. You need to have a table with the language and country code like en_US
.
Most modern browser send an "accept language" header, but beware It's not 100% reliable and it's just the primary language of the users browser and not really the country code.
try: var_dump( $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
to get: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
You can also try to use an API ie http://www.hostip.info/
An example would be:
$theFirstPart = "en";// you said you already got this
$userIP = "12.215.42.19";// from $_SERVER var
$languageCode = "en_US";// your standard
//you should use curl for this
// and it's really slow, so please cache this for at least a day or two ;)
$content = file_get_contents( "http://api.hostip.info/get_json.php?ip=$userIP" );
if( $content ) {
$json = json_decode( $content );
if( $json && isset( $json->country_code ) ) {
// before you assign the value, you should check if it's in the facebook xml
$languageCode = $theFirstPart.'_'.$json->country_code;
}
echo "<pre>";
var_dump( $languageCode );
}
Finally I created this table from all supported facebok lang-locales then I picked one using stripos()
and because it few-times picked up wrong code, I re-ordered the array by putting prefered to top of the array.
Maybe it's not the best solution, but it solved my problem.
function fb_lang($lang_code){
$fb_locales = [
'es_ES', 'en_US', 'fr_FR', 'tr_TR', 'sv_SE', // prefered codes are moved to line
'af_ZA', 'sq_AL', 'ar_AR', 'hy_AM', 'ay_BO', 'az_AZ', 'eu_ES', 'be_BY', 'bn_IN', 'bs_BA', 'bg_BG', 'ca_ES', 'ck_US',
'hr_HR', 'cs_CZ', 'da_DK', 'nl_NL', 'nl_BE', 'en_PI', 'en_GB', 'en_UD', 'eo_EO', 'et_EE', 'fo_FO', 'tl_PH', 'fi_FI',
'fb_FI', 'fr_CA', 'gl_ES', 'ka_GE', 'de_DE', 'el_GR', 'gn_PY', 'gu_IN', 'he_IL', 'hi_IN', 'hu_HU', 'is_IS', 'id_ID',
'ga_IE', 'it_IT', 'ja_JP', 'jv_ID', 'kn_IN', 'kk_KZ', 'km_KH', 'tl_ST', 'ko_KR', 'ku_TR', 'la_VA', 'lv_LV', 'fb_LT', 'li_NL',
'lt_LT', 'mk_MK', 'mg_MG', 'ms_MY', 'ml_IN', 'mt_MT', 'mr_IN', 'mn_MN', 'ne_NP', 'se_NO', 'nb_NO', 'nn_NO', 'ps_AF', 'fa_IR',
'pl_PL', 'pt_BR', 'pt_PT', 'pa_IN', 'qu_PE', 'ro_RO', 'rm_CH', 'ru_RU', 'sa_IN', 'sr_RS', 'zh_CN', 'sk_SK', 'sl_SI', 'so_SO',
'es_LA', 'es_CL', 'es_CO', 'es_MX', 'es_VE', 'sw_KE', 'sy_SY', 'tg_TJ', 'ta_IN', 'tt_RU', 'te_IN', 'th_TH',
'zh_HK', 'zh_TW', 'uk_UA', 'ur_PK', 'uz_UZ', 'vi_VN', 'cy_GB', 'xh_ZA', 'yi_DE', 'zu_ZA'
];
foreach($fb_locales as $fbl){
if(stripos($fbl,$lang_code)!==false){
return $fbl;
}
}
trigger_error('Fb_lang() couldn\'t find equvalent for language code "'.$lang_code.'"');
return 'en_US';
}
Hope it will help someone else.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.