使用货币和语言切换处理区域设置的最佳做法

时间:2021-09-14 11:10:49

I'm working on a multilanguage project with Zend Framework (German and English) where I have a small form to order a product. The user should be able to:

我正在使用Zend Framework(德语和英语)开展一个多语言项目,我有一个小表格来订购产品。用户应该能够:

  • Change his prefered currency for the product prices
  • 更改产品价格的首选货币
  • Change his prefered language for the whole website
  • 改变他对整个网站的首选语言

At the moment I use Zend_Locale::findLocale() to get the user's locale. With my browser settings I get 'de_CH' and that works well. But there are some problems as soon as:

目前我使用Zend_Locale :: findLocale()来获取用户的语言环境。使用我的浏览器设置,我得到'de_CH'并且效果很好。但是有一些问题:

  • The user has configured his browser to just give me the language code ('de' instead of 'de_CH')
  • 用户已将其浏览器配置为只给我语言代码('de'而不是'de_CH')
  • The user prefers a language which is not supported by my website
  • 用户更喜欢我的网站不支持的语言
  • The user is from a country where they got a currency which isn't supported by my product order form
  • 用户来自他们获得我的产品订单不支持的货币的国家/地区

Just the language code The language code is enough to handle Zend_Translate, but Zend_Currency needs more information. I think there is no solution to get the full locale with the country code from every possible user. My question is: how do you handle these situations? Do you check in your bootstrap's _initLocale() if a country code is missing and use a default country code? Are there any Zend Framework methods to do that?

只是语言代码语言代码足以处理Zend_Translate,但Zend_Currency需要更多信息。我认为没有解决方案可以使用来自每个可能用户的国家/地区代码来获取完整的语言环境。我的问题是:你如何处理这些情况?如果缺少国家/地区代码并使用默认国家/地区代码,您是否签入了引导程序的_initLocale()?是否有任何Zend Framework方法可以做到这一点?

An unsupported language If the users language is 'fr', I would like to use a default language because our website doesn't support french. Is there a better possibility than to add a whitelist array in bootstrap? For example a method from Zend_Translate?

不支持的语言如果用户语言是'fr',我想使用默认语言,因为我们的网站不支持法语。是否有更好的可能性,而不是在bootstrap中添加白名单数组?例如Zend_Translate的方法?

An unsupported currency What if the user's language is supported, but not his currency? Should I change the locale in bootstrap or should I make a copy of the locale and change it for Zend_Currency?

不支持的货币如果支持用户的语言但不支持其货币,该怎么办?我应该更改引导程序中的语言环境,还是应该复制语言环境并将其更改为Zend_Currency?

The main problem I think my main problem is that I'm not sure how to handle all these different locale issues. My prefered approach would be to verify the locale set in bootstrap's _initLocale(), so I can be sure that I can support it's language and currency.

主要问题我认为我的主要问题是我不确定如何处理所有这些不同的区域设置问题。我首选的方法是验证bootstrap的_initLocale()中设置的语言环境,所以我可以肯定我可以支持它的语言和货币。

Thank you for your advises.

谢谢你的建议。

Best regards, Nico

最好的问候,尼科

Editing 1 I have made myself some more thoughts about a way to handle these issues. What do you think about this approach?

编辑1我已经让自己更多地考虑了处理这些问题的方法。您如何看待这种方法?

  1. Zend_Locale(): filter all short locales (without a country code) and uses a whitelist with the languages your application supports (just the language code). If there is no matching, use the default given by your application.
  2. Zend_Locale():过滤所有短语言环境(没有国家/地区代码),并使用您的应用程序支持的语言的白名单(只是语言代码)。如果没有匹配,请使用应用程序提供的默认值。
  3. Zend_Translate: start with the language code from the locale. But if the user changes the preferred language, leave the locale as it is (perhaps there is no matching locale based on the new language code and the old country code) and just change the language code in Zend_Translate.
  4. Zend_Translate:从语言环境开始使用语言代码。但是,如果用户更改了首选语言,请将语言环境保留原样(可能没有基于新语言代码和旧国家/地区代码的匹配语言环境),只需更改Zend_Translate中的语言代码即可。
  5. Zend_Currency: Use the locale as the default. If the user changes the preferred currency, just change the currency and not the locale. So the formatting would stay based on the old locale because the change of the currency doesn't mean the user also wants to change his locale.
  6. Zend_Currency:使用语言环境作为默认值。如果用户更改了首选货币,只需更改货币而不是区域设置。因此格式化将保持基于旧的区域设置,因为货币的更改并不意味着用户也想要更改其区域设置。

Would this approach be acceptable? There is a problem with step 1 if the browsers locale are set like this:

这种方法可以接受吗?如果浏览器语言环境设置如下,则步骤1存在问题:

  • de
  • en_US
  • EN_US

Perhaps the application does support German, but because it is a short locale, the application will use en_US instead. But I don't know which solution would fix this problem, because I can't 'upgrade' a short locale to a full locale.

也许该应用程序支持德语,但由于它是一个简短的语言环境,应用程序将使用en_US。但我不知道哪种解决方案可以解决这个问题,因为我无法将短的语言环境“升级”为完整的语言环境。

3 个解决方案

#1


1  

The problem you highlighted is common and I imagine a small amount of work would be required to set the locale for your user.

您突出显示的问题很常见,我想为您的用户设置区域设置需要少量工作。

de to de_DE requires you to know if they are located in Germany but this can default to de_CH which is your locality, if you do not know the users locality.

de to de_DE要求您知道它们是否位于德国,但如果您不知道用户所在地,则可以默认为de_CH,这是您的所在地。

* The user has configured his browser to just give me the language code ('de' instead of 'de_CH')

Map this in your application. Your mapping can convert this to de_DE for residents in Germany that speak German. Or de_CH for residents in Switzerland that speak German. Or fr_CH for residents of Switzerland that speak French and so on for English speakers it would be en_CH or en_DE. So this solves 2 problems.

在您的应用程序中映射它。您的映射可以将此转换为de_DE,用于德语的德语居民。或者说瑞士居民说德语的de_CH。对于说英语的瑞士居民或者说英语的人来说,fr_CH将是en_CH或en_DE。所以这解决了2个问题。

The users language and location are needed to determine the locale for the user accurately.

需要用户语言和位置来准确地确定用户的区域设置。

If you do not know there location you should default to your own default language and locality.

如果您不知道位置,则应默认使用您自己的默认语言和位置。

Link: Unicode - locality and language mapping

链接:Unicode - 位置和语言映射

* The user prefers a language which is not supported by my website

You should have a default language and currency, if you are in Europe then the choice would be whatever is your default locality and language, if you are Swiss then de_CH makes sense.

你应该有一个默认的语言和货币,如果你在欧洲那么选择将是你的默认地点和语言,如果你是瑞士人,那么de_CH是有道理的。

* The user is from a country where they got a currency which isn't supported by my product order form

In this case you should revert to your default.

在这种情况下,您应该恢复默认值。

Zend has many examples of using Locales on their wiki. The most common place for setting locality in Zend is the bootstrap file.

Zend有很多在他们的wiki上使用Locales的例子。在Zend中设置位置的最常见位置是引导程序文件。

I used this link to reference support for this response on Zend Framework wiki. The code below provides a small example of how the Zend_Locale class throws exceptions if the locale is invalid. You could check for location and language at this point, and use it in combination with the information you receive from the browser to accurately set locale for the user.

我使用此链接在Zend Framework wiki上引用对此响应的支持。下面的代码提供了一个小例子,说明如果语言环境无效,Zend_Locale类如何抛出异常。您可以在此时检查位置和语言,并将其与从浏览器收到的信息结合使用,以准确设置用户的区域设置。

Link: Zend Locale Introduction

链接:Zend Locale简介

  // within the bootstrap file
  try {
      $locale = new Zend_Locale('auto');
  } catch (Zend_Locale_Exception $e) {
      $locale = new Zend_Locale('de');
  }
  // within your model/controller
  $date = new Zend_Date($locale);
  $currency = new Zend_Currency($locale)

#2


0  

Here is an article I wrote about localization that I think you may find useful in answering some of your questions.

这是我写的一篇关于本地化的文章,我认为你在回答一些问题时可能会觉得有用。

http://phpadvent.org/2010/localization-by-anthony-gentile

http://phpadvent.org/2010/localization-by-anthony-gentile

#3


0  

In my case, i would like to ask user to choose their language and currency when they first visit my site. in the language choose interface i was take the language code from user's browser as a reference of default language. and also i provide quick language switch button in my site.

就我而言,我想让用户在第一次访问我的网站时选择他们的语言和货币。在语言选择界面我是从用户的浏览器中获取语言代码作为默认语言的参考。我还在我的网站上提供快速语言切换按钮。

#1


1  

The problem you highlighted is common and I imagine a small amount of work would be required to set the locale for your user.

您突出显示的问题很常见,我想为您的用户设置区域设置需要少量工作。

de to de_DE requires you to know if they are located in Germany but this can default to de_CH which is your locality, if you do not know the users locality.

de to de_DE要求您知道它们是否位于德国,但如果您不知道用户所在地,则可以默认为de_CH,这是您的所在地。

* The user has configured his browser to just give me the language code ('de' instead of 'de_CH')

Map this in your application. Your mapping can convert this to de_DE for residents in Germany that speak German. Or de_CH for residents in Switzerland that speak German. Or fr_CH for residents of Switzerland that speak French and so on for English speakers it would be en_CH or en_DE. So this solves 2 problems.

在您的应用程序中映射它。您的映射可以将此转换为de_DE,用于德语的德语居民。或者说瑞士居民说德语的de_CH。对于说英语的瑞士居民或者说英语的人来说,fr_CH将是en_CH或en_DE。所以这解决了2个问题。

The users language and location are needed to determine the locale for the user accurately.

需要用户语言和位置来准确地确定用户的区域设置。

If you do not know there location you should default to your own default language and locality.

如果您不知道位置,则应默认使用您自己的默认语言和位置。

Link: Unicode - locality and language mapping

链接:Unicode - 位置和语言映射

* The user prefers a language which is not supported by my website

You should have a default language and currency, if you are in Europe then the choice would be whatever is your default locality and language, if you are Swiss then de_CH makes sense.

你应该有一个默认的语言和货币,如果你在欧洲那么选择将是你的默认地点和语言,如果你是瑞士人,那么de_CH是有道理的。

* The user is from a country where they got a currency which isn't supported by my product order form

In this case you should revert to your default.

在这种情况下,您应该恢复默认值。

Zend has many examples of using Locales on their wiki. The most common place for setting locality in Zend is the bootstrap file.

Zend有很多在他们的wiki上使用Locales的例子。在Zend中设置位置的最常见位置是引导程序文件。

I used this link to reference support for this response on Zend Framework wiki. The code below provides a small example of how the Zend_Locale class throws exceptions if the locale is invalid. You could check for location and language at this point, and use it in combination with the information you receive from the browser to accurately set locale for the user.

我使用此链接在Zend Framework wiki上引用对此响应的支持。下面的代码提供了一个小例子,说明如果语言环境无效,Zend_Locale类如何抛出异常。您可以在此时检查位置和语言,并将其与从浏览器收到的信息结合使用,以准确设置用户的区域设置。

Link: Zend Locale Introduction

链接:Zend Locale简介

  // within the bootstrap file
  try {
      $locale = new Zend_Locale('auto');
  } catch (Zend_Locale_Exception $e) {
      $locale = new Zend_Locale('de');
  }
  // within your model/controller
  $date = new Zend_Date($locale);
  $currency = new Zend_Currency($locale)

#2


0  

Here is an article I wrote about localization that I think you may find useful in answering some of your questions.

这是我写的一篇关于本地化的文章,我认为你在回答一些问题时可能会觉得有用。

http://phpadvent.org/2010/localization-by-anthony-gentile

http://phpadvent.org/2010/localization-by-anthony-gentile

#3


0  

In my case, i would like to ask user to choose their language and currency when they first visit my site. in the language choose interface i was take the language code from user's browser as a reference of default language. and also i provide quick language switch button in my site.

就我而言,我想让用户在第一次访问我的网站时选择他们的语言和货币。在语言选择界面我是从用户的浏览器中获取语言代码作为默认语言的参考。我还在我的网站上提供快速语言切换按钮。