简体   繁体   English

CultureInfo.CurrentCulture给了我错误的文化

[英]CultureInfo.CurrentCulture is giving me the wrong culture

I'm trying to get my clients' country, so I use CultureInfo.CurrentCulture. 我正在尝试获取客户的国家/地区,因此我使用CultureInfo.CurrentCulture。 Problem is that when my Canadian customers use my website, they're showing up as American. 问题是,当我的加拿大客户使用我的网站时,他们会显示为美国人。

It looks like CultureInfo.CurrentCulture is returning my server's country instead of their country. 看起来CultureInfo.CurrentCulture正在返回我服务器的国家而不是他们的国家。 So how do I get my clients' country? 那么我如何获得客户的国家?

You just need to set the culture attribute to auto in your web.config file: 您只需要在web.config文件中将culture属性设置为auto

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

This will automatically set the CurrentCulture to the client's culture. 这将自动将CurrentCulture设置为客户端的文化。

You can also set uiCulture to auto if you're using localized resources. 如果您使用的是本地化资源,也可以将uiCulture设置为auto

I believe you need to write code to read the user's culture from the incoming browser request , and set your CultureInfo from that. 我相信你需要编写代码来从传入的浏览器请求中读取用户的文化 ,并从中设置CultureInfo。

This fellow describes how they do it : Set the display culture for the current thread to the most appropriate culture from the user's incoming Http "request" object. 这个人描述了他们是如何做到的 :将当前线程的显示文化设置为来自用户传入的Http“request”对象的最合适的文化。

He has an excellent discussion there, but this is basically how he does it: 他在那里进行了很好的讨论,但这基本上就是他如何做到的:

In Page_Load , they make this call: UIUtilities.setCulture(Request); Page_Load ,他们进行此调用: UIUtilities.setCulture(Request);

Where this is what gets called: 这就是所谓的:

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

In my case my machine originally had English - UK installed. 在我的情况下,我的机器最初安装了英语 - 英国。 I added the English - US language and set it as default. 我添加了英语 - 美国语言并将其设置为默认语言。 I also verified that US was set correctly in the registry. 我还验证了美国在注册表中设置正确。 Sadly, System.Threading.Thread.CurrentThread.CurrentCulture still displayed the wrong culture, UK. 可悲的是, System.Threading.Thread.CurrentThread.CurrentCulture仍然显示错误的文化,英国。 I discovered you need to set the language options. 我发现你需要设置语言选项。 Download the language pack, handwriting and speech. 下载语言包,手写和语音。

Even then the culture was incorrect. 即便如此,文化也是错误的。 The UK would show up all over the machine and after I installed the US language pack the start menu went completely nuts. 英国将出现在整个机器上,在我安装美国语言包后,开始菜单完全疯了。 I gave up and reinstalled the OS using an english-US version. 我放弃并重新安装了使用英美版本的操作系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM