Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

locale_accept_from_http is a basic wrapper around ICU's API uloc_acceptLanguageFromHTTP but the PHP/PECL implementation seems fundamentally flawed that it uses the systems entire set of locales instead of taking a list as a parameter?

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

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

Outputs:

string(2) "fr"
like image 496
Steve-o Avatar asked Apr 26 '11 10:04

Steve-o


1 Answers

Correct, PHP wraps ICU's uloc_acceptLanguageFromHTTP without the ability to pass your locale list. Overall, intl extension is relatively new (PHP 5.3+) and I did find a couple of bugs myself which were quickly fixed within the next release.

What you could do is:

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

  • Accept-Language format is really not that complex, I bet you could write your own parser within 20 lines of code. See this article for an example.

like image 75
Sim Avatar answered Nov 24 '22 15:11

Sim