[英]CultureInfo.CurrentCulture is giving me the wrong culture
我正在尝试获取客户的国家/地区,因此我使用CultureInfo.CurrentCulture。 问题是,当我的加拿大客户使用我的网站时,他们会显示为美国人。
看起来CultureInfo.CurrentCulture正在返回我服务器的国家而不是他们的国家。 那么我如何获得客户的国家?
您只需要在web.config文件中将culture
属性设置为auto
:
<system.web>
<globalization culture="auto" />
<system.web>
这将自动将CurrentCulture
设置为客户端的文化。
如果您使用的是本地化资源,也可以将uiCulture
设置为auto
。
我相信你需要编写代码来从传入的浏览器请求中读取用户的文化 ,并从中设置CultureInfo。
这个人描述了他们是如何做到的 :将当前线程的显示文化设置为来自用户传入的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);
}
在我的情况下,我的机器最初安装了英语 - 英国。 我添加了英语 - 美国语言并将其设置为默认语言。 我还验证了美国在注册表中设置正确。 可悲的是, System.Threading.Thread.CurrentThread.CurrentCulture
仍然显示错误的文化,英国。 我发现你需要设置语言选项。 下载语言包,手写和语音。
即便如此,文化也是错误的。 英国将出现在整个机器上,在我安装美国语言包后,开始菜单完全疯了。 我放弃并重新安装了使用英美版本的操作系统。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.