使用完整的区域设置首选项列表从 Zend Framework 中提取用户定义的区域设置的最佳方法是什么?

发布于 2024-10-01 00:56:41 字数 1208 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃酥萝莉 2024-10-08 00:56:41

我得到了http://www.zf-beginners-guide.com/一本书的方法

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);
}

但我担心的是,他提到了语言环境升级:

警告 值得注意的是
Zend_Locale 不会抛出
如果区域设置标识符出现异常
检测到仅包含该语言
代码而不是区域代码。为了
例如,某些客户端和/或服务器
环境可能只是返回
区域设置 en 或 fr。这种情况可以
有时会产生问题
Zend_Locale 与其他一起使用
区域设置感知类,期望
完整的区域设置标识符,例如
Zend_Currency。没有真正的
解决这个问题除了
根据手动设置区域设置
现有事实和“最佳猜测”
估计。 提议的新特征
Zend_Locale 将来将允许
“区域设置升级”,其中部分
合格的区域设置标识符(用于
例如,en) 将自动
转换为完全限定的语言环境
标识符(例如 en_US)。

例如,在 Firefox 中,如果您仅选择语言代码 [en] ,并且上面的代码将接受它作为区域设置,但其他区域设置感知类

在经过一些测试后会抛出异常,我发现很高比例的人不会改变浏览器默认语言
这会让你高兴:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文