在浏览器上检测到无效的语言名称

发布于 2024-12-09 00:07:25 字数 1489 浏览 0 评论 0原文

我得到此代码来检索用户设置的浏览器语言:

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;

我收到以下错误:

  1. 不支持文化名称“name”:从我们的错误日志中,发现该名称是x-ns1xs4m8wixnxgx-ns1dfk__jmpnx0 等。我不知道这些语言来自哪里,也不知道它们是什么,但我认为这些语言是从移动设备浏览时检索。

  2. 名称“name”包含对文化或区域无效的字符:还发现名称是fr; q=1.0en;q=1.0en_usen;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:

  1. Culture name 'name' is not supported: From our error logs, found out that name is x-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.

  2. The name 'name' contains characters that are not valid for a Culture or Region: Also found out that name is fr; 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 技术交流群。

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

发布评论

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

评论(1

活雷疯 2024-12-16 00:07:25

您唯一应该注意的是 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.

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