Pekka
Pekka

Reputation: 449703

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.

Upvotes: 12

Views: 4927

Answers (4)

Nico
Nico

Reputation: 170

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.

Upvotes: 1

Gordon
Gordon

Reputation: 317147

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

Upvotes: 5

Pascal MARTIN
Pascal MARTIN

Reputation: 401152

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 ^^

Upvotes: 9

Phil Rykoff
Phil Rykoff

Reputation: 12097

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();

Upvotes: 11

Related Questions