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

别说谁变了你拦得住时间么 提交于 2019-12-23 08:06:19

问题


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"

回答1:


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.



来源:https://stackoverflow.com/questions/5788824/how-to-use-localeacceptfromhttp-without-a-filter-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!