如果我要添加类似-框或页面插件,所以我必须选择语言,它将在其中显示。
但是,如果我有PHP中的多语言站点系统,它只记得国家代码(en,es.),该怎么办?所以我可能需要一个这样的桌子,但是所有的语言都是这样的:
en => en_US
es => es_ES ...
你知道一些吗?
发布于 2015-10-09 15:56:12
最后,我从所有受支持的facebok lang-locales中创建了这个表,然后我使用stripos()
选择了一个表,因为它很少选择错误的代码,所以我将优先放在数组的顶部来重新排序数组。
也许这不是最好的解决办法,但它解决了我的问题。
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';
}
希望它能帮到别人。
发布于 2015-10-09 14:56:50
Facebook正在使用国家代码,但这些代码更加具体,因此它们大多被称为Locale (语言和国家代码)。
您只需从facebook获取XML,其中包含它们所使用的所有地区ids:
<locale>
<englishName>English (UK)</englishName>
<codes>
<code>
<standard>
<name>FB</name>
<representation>en_GB</representation>
</standard>
</code>
</codes>
</locale>
您可以在这里找到整个文件(https://www.facebook.com/translations/FacebookLocales.xml)
但是:这里的问题是,您不能从en
引用en_US
或en_GB
。所以从2个字母到4个字母的映射是行不通的。您需要一个具有语言和国家代码(如en_US
)的表。
大多数现代浏览器都会发送“接受语言”标题,但是要当心--它不是100%可靠的,它只是用户浏览器的主要语言,而不是真正的国家代码。
尝试:var_dump( $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
to get:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
您还可以尝试使用API,即http://www.hostip.info/。
一个例子是:
$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 );
}
https://stackoverflow.com/questions/33040204
复制相似问题