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 variableHTTP_ACCEPT_LANGUAGE
. if no matching locale can be found, then preference is given toENVIRONMENT
and lastlyFRAMEWORK
.
2.const Zend_Locale::ENVIRONMENT
- PHP publishes the host server's locale via the PHP internal functionsetlocale()
. If no matching locale can be found, then preference is given toFRAMEWORK
and lastlyBROWSER
.
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 toENVIRONMENT
and lastlyBROWSER
.
(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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With