我正在尝试使用浏览器语言首选项在 Zend_Locale 和 Zend_Currency 之间创建连接。
浏览器:zh
$locale = new Zend_Locale(Zend_Locale::BROWSER);
Zend_Debug::Dump($locale->getLanguage());
Zend_Debug::Dump($locale->getRegion());
die;
结果:
string(2) "en"
bool(false)
浏览器:en_US
$locale = new Zend_Locale(Zend_Locale::BROWSER);
Zend_Debug::Dump($locale->getLanguage());
Zend_Debug::Dump($locale->getRegion());
die;
结果:
string(2) "en"
string(2) "US"
我要解决这个问题吗?
这是我的插件控制器:
class MyProject_Controller_Plugin_Language extends Zend_Controller_Plugin_Abstract {
public function routeShutdown(Zend_Controller_Request_Abstract $request) {
$locale = new Zend_Locale(Zend_Locale::BROWSER);
$registry = Zend_Registry::getInstance();
// Check if the config file has been created
$isReady = MyProject_Main::isReady();
$module = $request->getModuleName ();
if($module == "default"){ // set the right session namespace per module
$ns = new Zend_Session_Namespace ( 'Default' );
}elseif($module == "admin"){
$ns = new Zend_Session_Namespace ( 'Admin' );
}else{
$ns = new Zend_Session_Namespace ( 'Default' );
}
// check the user request if it is not set, please get the old prefereces
$lang = $request->getParam ( 'lang', $ns->lang );
if(empty($lang)){ // Get the user preference
if(strlen($locale) == 2){ // Check if the Browser locale is formed with 2 chars
$lang = $locale;
}elseif (strlen($locale) > 4){ // Check if the Browser locale is formed with > 4 chars
$lang = substr($locale, 0, 2); // Get the language code from the browser preference
}
}
// Get the translate language or the default language: en
if(file_exists(PUBLIC_PATH . "/languages/$lang/$lang.mo")){
$translate = new Zend_Translate(array('adapter' => "MyProject_Translate_Adapter_Gettext", 'content' => PUBLIC_PATH . "/languages/$lang/$lang.mo", 'locale' => $lang, 'disableNotices' => true));
}else{
$translate = new Zend_Translate(array('adapter' => "MyProject_Translate_Adapter_Gettext", 'locale' => $lang, 'disableNotices' => true));
}
$registry->set('Zend_Translate', $translate);
$registry->set('Zend_Locale', $locale);
if($isReady){
$ns->langid = Languages::get_language_id_by_code($lang);
}else{
$ns->langid = 1;
}
$ns->lang = $lang;
}
}
谢谢