Zend Locale& Zend_Currency:地区代码 [英] Zend Locale & Zend_Currency: Region code

查看:100
本文介绍了Zend Locale& Zend_Currency:地区代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用浏览器语言首选项在Zend_Locale和Zend_Currency之间创建连接.

I am trying to create a connection between the Zend_Locale and the Zend_Currency using the browser language preferences.

浏览器:en

$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"

我该如何解决这个问题?

Ho have I to solve this problem?

这是我的插件控制器:

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;
    }
}

谢谢

推荐答案

很显然,客户的语言首选项是一个很好的第一步,但是在所有情况下都无法为您提供帮助.即使他们返回值,我也不会用它来确定您所描述的适当货币-用户可以将其首选语言/地区设置为外来设置.例如外籍人士和语言学习者.尽管这可能是一个极端情况,但不要相信它可以检测货币.

Obviously, the client's language preferences are a good first step, but won't help you in all cases. Even if they return a value, I would not use it to determine the appropriate currency as you described - users may set their preferred language/region to a foreign setting. Examples are expats and language learners. Though this may be an edge case, don't trust it to detect currency.

最可靠的解决方案是使用地理位置服务,该服务还会返回找到的位置的币种,例如 http://www.geoplugin.com/webservices/php .

The most robust solution would be to use a geolocation service which also returns the currency for the location found, e.g. http://www.geoplugin.com/webservices/php.

两种方法的组合也可能是一个很好的解决方案.如果它们返回冲突的值,则为用户提供了从两种找到的货币(或任何其他货币)中进行选择的可能性.

The combination of both methods may also be a good solution. If they return conflicting values, offer the user a possibility to choose from the two found currencies (or from any other).

这篇关于Zend Locale& Zend_Currency:地区代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆