CultureInfo.CurrentCulture给了我错误的文化

时间:2010-06-28 17:09:44

标签: c# cultureinfo currentculture

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

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

3 个答案:

答案 0 :(得分:18)

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

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

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

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

答案 1 :(得分:2)

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

This fellow describes how they do it:将当前线程的显示文化设置为用户传入的Http“request”对象中最合适的文化。

他在那里进行了很好的讨论,但这基本上就是他如何做到的:

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

答案 2 :(得分:2)

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

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

相关问题