我开发了自己庞大的WP插件并将其本地化为3种语言。从wp-admin->设置切换语言时效果很好,但是当我按照这样的代码切换语言时,它不会切换语言:
add_filter( 'locale', 'wpse_52419_change_language' );
function wpse_52419_change_language( $locale ){
return 'fr_FR';
}
然而,该网站的其余部分正在根据此代码进行切换。
可能出现什么问题?
答案 0 :(得分:0)
好吧,我的错是简单的:我过早地包含了load_plugin_textdomain命令。
就像一张纸条,也许它会对某人有所帮助,我现在做了下面的工作就像一个魅力:
add_filter( 'locale', 'slople_change_language' );
function slople_change_language( $locale )
{
$lang = 'en';
if(isset($_GET['lang'])) {
//get parameter is the most important factor
$lang = $_GET['lang'];
}else if(isset($_COOKIE['lang'])){
//cookie is the 2nd most important factor
$lang = $_COOKIE['lang'];
}else{
//try to make an educated guess about what the user wants to see
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}
//store current language in cookie
setcookie('lang', $lang, time() + (10 * 365 * 24 * 60 * 60), "/");
$langIsoCode = $lang.'_'.strtoupper($lang);
return $langIsoCode;
}