使用完整的区域设置首选项列表从 Zend Framework 中提取用户定义的区域设置的最佳方法是什么?
Zend Framework 有一个很棒的组件,称为 Zend_Locale。在正确解析语言和区域设置方面存在各种细节,部分原因是它已被黑客入侵 HTTP 接受语言标头。
在 Zend Framework 语言环境文档 中,它说:
注意:请注意,不存在 仅包含最多 2 个字符的区域设置 人们认为。还有语言 和地区不仅 缩写为2个字符。 因此你不应该剥去 自己的地区和语言,但使用 Zend_Locale 当你想剥离时 语言环境中的语言或区域 细绳。否则你可以有 代码中的意外行为 当您自己执行此操作时。
完美的。除了我一直在测试各种语言翻译。假设您使用 Firefox 浏览,更改您的设置并添加一些 language_region 配置文件...更改默认顺序。您将看到 Zend 仅获取默认值。文档甚至这样说:
如果用户提供了多个 浏览器中的语言环境 Zend_Locale 将使用第一个找到的语言环境。如果 用户未提供区域设置或 该脚本是从 命令行自动区域设置 “环境”将自动成为 使用并退回。
这是不幸的。如果您定义了 5 种语言,您希望遍历用户在浏览器中定义的所有区域设置并使用匹配的语言...
所以我的问题是,您将采取什么方法来解析完整列表并提取任何匹配项?
更新 有一个 Zend_Locale::getBrowser() 方法返回完整列表。伟大的!不知道为什么 Zend_Translate 似乎只拉第一个。我会在检查其工作原理后回来...可能需要构建您有翻译的语言数组,并将其与 getBrowser() 的结果进行比较并拉出第一个匹配项。
更新2 我确实实现了一个解决方案,但它仍然有点hacky,因为 Zend_Translate 和 Zend_Locale 不是小类。显然,国际化是一个很大的话题。有相当多的东西需要研究。
但本质上,它归结为检查每种语言是否与浏览器首选项列表相匹配。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我得到了http://www.zf-beginners-guide.com/一本书的方法
但我担心的是,他提到了语言环境升级:
例如,在 Firefox 中,如果您仅选择语言代码 [en] ,并且上面的代码将接受它作为区域设置,但其他区域设置感知类
在经过一些测试后会抛出异常,我发现很高比例的人不会改变浏览器默认语言
这会让你高兴:)
I got the approach form http://www.zf-beginners-guide.com/ book
but what i am afraid of and he mention about locale upgrading :
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 :)