CultureInfo.CurrentCulture 给了我错误的文化

发布于 2024-09-07 04:19:14 字数 168 浏览 7 评论 0原文

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

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

I'm trying to get my clients' country, so I use 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. So how do I get my clients' country?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

破晓 2024-09-14 04:19:14

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

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

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

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

You just need to set the culture attribute to auto in your web.config file:

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

This will automatically set the CurrentCulture to the client's culture.

You can also set uiCulture to auto if you're using localized resources.

寒冷纷飞旳雪 2024-09-14 04:19:14

我相信您需要编写代码来从传入的浏览器请求中读取用户的文化,并从中设置您的 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);
}

I believe you need to write code to read the user's culture from the incoming browser request, and set your CultureInfo from that.

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.

He has an excellent discussion there, but this is basically how he does it:

In Page_Load, they make this call: 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);
}
马蹄踏│碎落叶 2024-09-14 04:19:14

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

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

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. 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.

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