多语言 Web 应用程序 - 如何在 ASP.NET 中检测用户的语言?
我正在构建一个 ASP.NET Web 应用程序,所有字符串都存储在资源文件中。 我想向我的应用程序添加第二种语言,理想情况下,我想自动检测用户的浏览器语言(或 Windows 语言)并默认为该语言,而不是让他们选择英语以外的语言。 目前,我正在手动处理所有资源填充,因此,如果我有一种简单的方法来自动确定要显示的语言,那么从我的角度来看,添加第二个资源文件和语言是微不足道的。
有没有人这样做过,或者您对我如何检索该值有任何想法吗? 由于 ASP.NET 是基于服务器的,因此我似乎无法访问特定的浏览器设置。
解决方案:这就是我最终所做的。 我使用“For Each”来浏览“HttpContext.Current.Request.UserLanguages”并搜索我支持的语言。 实际上,我只是检查左边的两个字符,因为我们还不支持任何方言 - 仅支持英语和西班牙语。 感谢您的帮助!
I'm building an ASP.NET web application, and all of my strings are stored in a resource file. I'd like to add a second language to my application, and ideally, I'd like to auto-detect the user's browser language (or windows language) and default to that, instead of making them choose something besides English. Currently, I'm handling all the resource population manually, so adding a second resource file and language is trivial from my point of view, if I had an easy way to automatically figure out what language to display.
Has anybody done this, or do you have any thoughts about how I might retrieve that value? Since ASP.NET is server-based, I don't seem to have any access to specific browser settings.
RESOLUTION: Here's what I ended up doing. I used a "For Each" to go through "HttpContext.Current.Request.UserLanguages" and search for one I support. I'm actually just checking the left two characters, since we don't support any dialects yet - just English and Spanish. Thanks for all the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 web.config 中尝试此操作:
这将导致 ASP.NET 从请求标头自动检测客户端的区域性。 您还可以通过“页面”属性在每个页面上进行设置。
Try this in the web.config:
This will cause ASP.NET to auto-detect the client's culture from the request header. You can also set this on a per-page basis via the Page attribute.
这篇文章(链接到 archive.org,因为原始链接现已失效) 可能有助于自动检测浏览器的语言设置。
[编辑] 是的。 引用的文章没有使用 ASP.NET。 这篇文章确实如此。
This article (linked to archive.org as original link is now dead) might be helpful with auto detecting the browser's language setting.
[EDIT] Yes. The quoted article does not use ASP.NET. This article does.
ASP.NET 4 中的 Request.UserLanguages 将此解析为字符串数组。
好信息: http://www.w3.org/Protocols/rfc2616/rfc2616- sec14.html
Request.UserLanguages in ASP.NET 4 parses this as a string array.
Good info: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
这是一个很好的问题,因为 ASP.NET 中的本地化被许多开发人员忽视了。
ASP.NET 应该自动获取用户的浏览器设置,并强制将
CultureInfo.CurrentCulture
设置为用户的浏览器语言。 您可以使用Page_OnInit()
中的一行来强制解决该问题,例如:您如何测试这个? 进入浏览器上的语言面板并更改设置。
This is a great question, as localization in ASP.NET is overlooked by many developers.
ASP.NET should automatically pick up on the user's browser settings and force the
CultureInfo.CurrentCulture
to the user's browser language. You can force the issue with a line inPage_OnInit()
like:How can you test this? Enter the languages panel on our browser and change settings.
客户端一般会在 HTTP 请求头中设置 Accept-Language ,并给出量化评分列表首选语言,通常(但不一定)按最喜欢到最不喜欢的顺序排列。 您可以对其进行解析,但正如 Maxam 所指出的, ASP.NET 确实有一种机制代表您这样做。
The client generally sets Accept-Language in the HTTP request header with a quantitatively scored list of preferred language, conventionally (but not necessarily) in order of most favored to least favored. You can parse that, but as Maxam has noted, ASP.NET does have a mechanism for doing that on your behalf.
这篇文章的来源在这里:
如何检测浏览器语言
The source of this article is here:
How to detect browser language