CultureInfo.CurrentCulture 给了我错误的文化
我正在尝试获取客户的国家/地区,因此我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需在 web.config 文件中将
culture
属性设置为auto
:这会自动将
CurrentCulture
设置为客户端的文化。如果您使用本地化资源,还可以将
uiCulture
设置为auto
。You just need to set the
culture
attribute toauto
in your web.config file:This will automatically set the
CurrentCulture
to the client's culture.You can also set
uiCulture
toauto
if you're using localized resources.我相信您需要编写代码来从传入的浏览器请求中读取用户的文化,并从中设置您的 CultureInfo。
此人描述了他们是如何做到的:将当前线程的显示区域性设置为来自用户传入的 Http“请求”对象的最合适的区域性。
他在那里进行了精彩的讨论,但这基本上就是他的做法:
在
Page_Load
中,他们进行了这样的调用:UIUtilities.setCulture(Request);
这是得到的结果称为:
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:
就我而言,我的机器最初安装了英语 - 英国。我添加了英语 - 美国语言并将其设置为默认值。我还验证了美国在注册表中的设置是否正确。遗憾的是,
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.