开发者

What's the best way to pull user defined locales from Zend Framework using the full locale preference list?

开发者 https://www.devze.com 2023-01-23 22:37 出处:网络
Zend Framework has a great component called Zend_Locale. There\'s all kind of nitty gritty details in properly parsing out language and locale, partly because it\'s been hacked into HTTP Accept-Langua

Zend Framework has a great component called Zend_Locale. There's all kind of nitty gritty details in properly parsing out language and locale, partly because it's been hacked into HTTP Accept-Language header.

In the Zend Framework locale docs, it says:

Note: Be aware that there exist not only locales with 2 characters as most people think. Also there are languages and regions which are not only abbreviated with 2 characters. Therefor you should NOT strip the region and language yourself, but use Zend_Locale when you want to strip language or region from a locale string. Otherwise you could have unexpected behaviour within your code when you do this yourself.

Perfect. Except I've been testing out various language translations. Assuming you browse with Firefox, change your settings and add some language_开发者_运维问答region profiles...change the default order. You'll see that Zend only pulls the default. The docs even say so:

If a user provides more than one locale within his browser, Zend_Locale will use the first found locale. If the user does not provide a locale or the script is being called from the command line the automatic locale 'environment' will automatically be used and returned.

This is unfortunate. If you have 5 languages defined, you want to go through all the locales the user defined in the browser and use a language that matches...

So my question is, what approach would you take to parse the full list and pull out any matches?

UPDATE There's a Zend_Locale::getBrowser() method which returns the full list. Great! Not sure why Zend_Translate only seems to pull the first one then. I'll come back after checking how that works... will probably need to build the array of langs you have translations for and compare it to the results of getBrowser() and pull the first match.

UPDATE2 I did implement a solution, but it's still kind of hacky, because Zend_Translate and Zend_Locale are not small little classes. Internationalization is a large topic, obviously. There's a fair bit to research.

But in essence it boils down to checking each language for matches against the browser preference list.


I got the approach form http://www.zf-beginners-guide.com/ book

protected function _initLocale()
{
// try to auto-detect locale
// if fail, set locale manually
try {
$locale = new Zend_Locale('browser');
} catch (Zend_Locale_Exception $e) {
$locale = new Zend_Locale('en_GB');
}
// register locale
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Locale', $locale);
}

but what i am afraid of and he mention about locale upgrading :

CAUTION It is important to note that Zend_Locale will not throw an exception if the locale identifier detected contains only the language code and not the region code. For example, some client and/or server environments may simply return the locale en or fr. This situation can sometimes create a problem when Zend_Locale is used with other locale-aware classes that expect a complete locale identifier, such as Zend_Currency. There is no real solution to this problem, apart from manually setting the locale based on available facts and a “best guess” estimate. A proposed new feature in Zend_Locale will, in the future, allow “locale upgrading,” wherein partially qualified locale identifiers (for example, en) will be automatically converted to fully qualified locale identifiers (for example, en_US).

for example in firefox if you choose only the language code [en] only , and that code above will accept it as locale but the other locale aware classes will throw an exception

after some testing i had found very high percentage of people don't change the browser default language which would make you happy :)

0

精彩评论

暂无评论...
验证码 换一张
取 消