Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 429
Pekka Avatar asked Mar 06 '10 21:03

Pekka


3 Answers

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();
like image 123
Phil Rykoff Avatar answered Nov 01 '22 01:11

Phil Rykoff


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

like image 9
Pascal MARTIN Avatar answered Oct 31 '22 23:10

Pascal MARTIN


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

  • http://de3.php.net/manual/en/locale.acceptfromhttp.php or
  • http://de3.php.net/manual/en/function.http-negotiate-language.php
like image 5
Gordon Avatar answered Oct 31 '22 23:10

Gordon