How to use Locale :: acceptFromHttp without a filter list?

locale_accept_from_http is the base shell of the ICU API uloc_acceptLanguageFromHTTP , but the PHP / PECL implementation seems fundamentally wrong that it uses systems of the whole set of locales instead of taking the list as a parameter?

For example, the user has HTTP_ACCEPT_LANGUAGE = zh-HK;q=0.2, fr , that is, the user reads traditional Chinese or French, preferring the latter. You have, for example, a news site that offers articles in traditional Chinese and simplified Chinese. Using the Locale::acceptFromHttp will only return fr .

 <?php var_dump (Locale::acceptFromHttp ("zh-HK;q=0.2,fr")); ?> 

Outputs:

 string(2) "fr" 
+7
source share
1 answer

That's right, PHP wraps ICU uloc_acceptLanguageFromHTTP without being able to pass your list of locales. In general, the intl extension is relatively new (PHP 5.3+), and I really found a couple of errors that were quickly fixed in the next version.

What can you do:

  • Submit a bug / feature request. There is a similar error already reported .

  • The Accept-Language format is actually not that complicated, I'm sure you could write your own parser within 20 lines of code. See this article for an example.

+6
source

All Articles