10

我正在尝试获取客户所在的国家/地区,因此我使用 CultureInfo.CurrentCulture。问题是当我的加拿大客户使用我的网站时,他们显示为美国人。

看起来 CultureInfo.CurrentCulture 正在返回我的服务器所在的国家/地区而不是他们的国家/地区。那么如何获取客户所在的国家/地区?

4

3 回答 3

18

您只需在 web.config 文件中将culture属性设置为:auto

<system.web>
    <globalization culture="auto" />
<system.web>

这将自动设置CurrentCulture为客户的文化。

如果您使用本地化资源,您也可以设置uiCulture为。auto

于 2010-09-25T15:12:40.997 回答
2

我相信您需要编写代码来从传入的浏览器请求中读取用户的文化,并从中设置您的 CultureInfo。

这个家伙描述了他们是如何做到的:将当前线程的显示文化设置为来自用户传入的 Http“请求”对象的最合适的文化。

他在那里进行了精彩的讨论,但这基本上是他的做法:

Page_Load中,他们打了这个电话:UIUtilities.setCulture(Request);

这就是所谓的:

/// Set the display culture for the current thread to the most
/// appropriate culture from the user's incoming Http "request" object.
internal static void setCulture(HttpRequest request)
{
    if (request != null)
    {
      if (request.UserLanguages != null)
      {
        if (request.UserLanguages.Length > -1)
        {
          string cultureName = request.UserLanguages[0];
          UIUtilities.setCulture(cultureName);
        }
      }
        // TODO: Set to a (system-wide, or possibly user-specified) default
        // culture if the browser didn't give us any clues.
    }
}

/// Set the display culture for the current thread to a particular named culture.
/// <param name="cultureName">The name of the culture to be set 
/// for the thread</param>
private static void setCulture(string cultureName)
{
    Thread.CurrentThread.CurrentCulture = 
        CultureInfo.CreateSpecificCulture(cultureName);
    Thread.CurrentThread.CurrentUICulture = new
        CultureInfo(cultureName);
}
于 2010-06-28T17:25:07.070 回答
2

就我而言,我的机器最初安装了英语 - 英国。我添加了英语 - 美国语言并将其设置为默认值。我还验证了 US 在注册表中的设置是否正确。可悲的是,System.Threading.Thread.CurrentThread.CurrentCulture仍然显示错误的文化,英国。我发现您需要设置语言选项。下载语言包、手写和语音。

即便如此,文化也不正确。英国会出现在整个机器上,在我安装美国语言包后,开始菜单完全疯了。我放弃并使用英美版本重新安装了操作系统。

于 2016-12-01T00:45:13.713 回答