我如何(在ASP.NET MVC中)获取当前访问者的CultureInfo(基于他/她的浏览器语言)?
我不知道从哪里开始。我试着查看浏览器发送的“Accept-Languages”标题。但这是最好的做法吗?
解决方法
Request.UserLanguages是您要寻找的财产。请记住,此数组可能包含由请求标头设置的任意(甚至非常规)语言。
UPDATE
例:
// Get browser languages. var userLanguages = Request.UserLanguages; CultureInfo ci; if (userLanguages.Count() > 0) { try { ci = new CultureInfo(userLanguages[0]); } catch(CultureNotFoundException) { ci = CultureInfo.InvariantCulture; } } else { ci = CultureInfo.InvariantCulture; } // Here CultureInfo should already be set to either user's prefereable language // or to InvariantCulture if user transmitted invalid culture ID
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。