我如何(在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