在浏览器上检测到无效的语言名称
我得到此代码来检索用户设置的浏览器语言:
string cultureName = string.Empty;
try
{
string[] languages = HttpContext.Current.Request.UserLanguages;
cultureName = languages[0].ToLowerInvariant().Trim();
}
catch
{
cultureName = "en-US";
}
System.Globalization.CultureInfo browserCulture = CultureInfo.CreateSpecificCulture(cultureName);
System.Threading.Thread.CurrentThread.CurrentCulture = browserCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = browserCulture;
我收到以下错误:
不支持文化名称“name”
:从我们的错误日志中,发现该名称是x-ns1xs4m8wixnxg
、x-ns1dfk__jmpnx0
等。我不知道这些语言来自哪里,也不知道它们是什么,但我认为这些语言是从移动设备浏览时检索。名称“name”包含对文化或区域无效的字符
:还发现名称是fr; q=1.0
、en;q=1.0
、en_us
、en;q=0.9
等...我我认为我应该对每个字符进行解析,看看它是否有效
我已经修改了我的代码来处理这些错误:
CultureInfo cultureInfo;
try
{
string[] languages = HttpContext.Current.Request.UserLanguages;
string cultureName = languages[0].ToLowerInvariant().Trim();
cultureInfo = CultureInfo.CreateSpecificCulture(cultureName);
}
catch
{
cultureInfo = CultureInfo.CreateSpecificCulture("en-US");
}
System.Globalization.CultureInfo browserCulture = cultureInfo;
System.Threading.Thread.CurrentThread.CurrentCulture = browserCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = browserCulture;
但我想知道这些语言来自哪里,或者如何复制它们?这是为了验证我的代码是否正常工作。谢谢。
I got this code to retrieve the browser language that was set by the user:
string cultureName = string.Empty;
try
{
string[] languages = HttpContext.Current.Request.UserLanguages;
cultureName = languages[0].ToLowerInvariant().Trim();
}
catch
{
cultureName = "en-US";
}
System.Globalization.CultureInfo browserCulture = CultureInfo.CreateSpecificCulture(cultureName);
System.Threading.Thread.CurrentThread.CurrentCulture = browserCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = browserCulture;
I got these errors:
Culture name 'name' is not supported
: From our error logs, found out that name isx-ns1xs4m8wixnxg
,x-ns1dfk__jmpnx0
etc.. I do not know where this languages come from, or what are they, but I think these languages are retrieved when browsing from mobile.The name 'name' contains characters that are not valid for a Culture or Region
: Also found out that name isfr; q=1.0
,en;q=1.0
,en_us
,en;q=0.9
, etc... I'm thinking that I should parse it per character to see if it is a valid
I have already modified my code to handle these errors:
CultureInfo cultureInfo;
try
{
string[] languages = HttpContext.Current.Request.UserLanguages;
string cultureName = languages[0].ToLowerInvariant().Trim();
cultureInfo = CultureInfo.CreateSpecificCulture(cultureName);
}
catch
{
cultureInfo = CultureInfo.CreateSpecificCulture("en-US");
}
System.Globalization.CultureInfo browserCulture = cultureInfo;
System.Threading.Thread.CurrentThread.CurrentCulture = browserCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = browserCulture;
but I would like to know where these languages came from, or how to replicate them? This is to verify if my code is working properly. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您唯一应该注意的是
UserLanguages
将仅返回相应的 HTTP 标头。标头值可能具有由;
分隔的附加属性。因此,您应该将该值拆分为;
并获取第一位。至于 x-ns1dfk__jmpnx0 我会说忽略它。在这种情况下,回退到默认语言是完全有效的。
编辑:我收回第一部分。根据 this q 参数是语言首选项的值。所以你真正应该做的是用分号分割,然后解析 q 属性的值(如果不存在则假设 1),然后对其进行排序并取第一个。
The only thing you should be aware of that
UserLanguages
will simply return corresponding HTTP header. Header values may have additional attributes separated by;
. So you should split that value on;
and get the first bit.As for
x-ns1dfk__jmpnx0
I would say ignore it. Falling back to your default language in this case is totally valid.edit: I take the first part back. According to this the q parameter is the value of the language preference. So what you really should do is split on semicolon, then parse value of q attribute (assume 1 if not present), then sort on it and take the first one.