Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

c# - Detecting browser display language

By using the following code

string[] languages = HttpContext.Current.Request.UserLanguages;
string chosenLanguage = languages[0];

if I have installed 3 languages (ex. "da (danish)", "sv (swedish)" and "en (english)"), then the languages array looks like this:

[0]: "en-US"
[1]: "en;q=0.8"
[2]: "da;q=0.6"
[3]: "sv;q=0.4"

Even if I change the display language to "Danish" instead of "English" then the array doesn't change any of the values. As far as I can read from what other people have written about this subject, the [0] value should be the chosen language of the browser, but it is still "en-US".

Is there some other way to register the language of the browser or am I doing something wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Setting the UICulture and Culture on the page directive worked for me:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" UICulture="auto" Culture="auto" %>

I then set my chrome language to French and made sure to re-order the languages - chrome will take the topmost language as default for the browser.

I then used:

Response.Write(System.Threading.Thread.CurrentThread.CurrentUICulture);

Which correctly gave me fr

You can set the culture on the page level or globally, see here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...