简体   繁体   中英

Determine browser language(s) in Zend Framework?

I am dissolving a rather big old library with various PHP helper functions that have amassed over time. I'm looking for Zend Framework based replacements for as many of these functions as possible.

My first candidate is a function that returns the users's most preferred browser language from the huge list that can be http_accept_language .

Does ZF have a function for that?

I realize Zend_Translate is able to somehow detect the browser language, but I am not seeing a publicly available function to actually get the language string.

here you go:

$locale = new Zend_Locale();

// if locale is 'de_AT' then 'de' will be returned as language
print $locale->getLanguage();

// if locale is 'de_AT' then 'AT' will be returned as region
print $locale->getRegion();

Zend_Locale should be able to help, about that.

See the examples and explanations on the page Using Zend_Locale (quoting) :

For most situations, new Zend_Locale() will automatically select the correct locale, with preference given to information provided by the user's web browser.


And there are a couple more details later on that page (quoting) :

The search algorithm used by Zend_Locale for automatic selection of a locale uses three sources of information:

1. const Zend_Locale::BROWSER - The user's Web browser provides information with each request, which is published by PHP in the global variable HTTP_ACCEPT_LANGUAGE . if no matching locale can be found, then preference is given to ENVIRONMENT and lastly FRAMEWORK .

2. const Zend_Locale::ENVIRONMENT - PHP publishes the host server's locale via the PHP internal function setlocale() . If no matching locale can be found, then preference is given to FRAMEWORK and lastly BROWSER .

3. const Zend_Locale::FRAMEWORK - When Zend Framework has a standardized way of specifying component defaults (planned, but not yet available) , then using this constant during instantiation will give preference to choosing a locale based on these defaults. If no matching locale can be found, then preference is given to ENVIRONMENT and lastly BROWSER .

(Advice : go read that page -- I will not copy-paste everything there is to read ^^ )


Edit : And here's the portion of code that illustrates my comment :

$locale = new Zend_Locale();
var_dump($locale->getLanguage());
var_dump($locale->getRegion());
die;

Gives me :

string(2) "fr" 
bool(false) 

Well, my browser is asking for french, without specifying a region ^^

I've upvoted both answers. As an alternative to getting the language with Zend_Locale consider

Is there a possibility to force Zend_Locale() to get the first full locale (language code and region code, like 'en_US') instead of just the language code ('en')? If not, it's a problem to handle currencies because they need a full locale.

How do you handle this issue? I haven't found any acceptable solution for that problem yet and for me it seems like this should be a task for Zend_Locale , in _prepareLocale($locale, $strict = false) for example.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM