How does the website know what language the user of the browser is using?

Source: Internet
Author: User

We will see a lot of support multi-lingual site, some early site will have a link on the home page, let the user to choose to see the Chinese, or English, now the site is not required, these sites can be based on the language used by users to display different language versions. For example, you are using Simplified Chinese, open the Web page, you will see a simplified Chinese page, you use English, enter the same address, you will see an English page.

How does the server know which language version of the page the user wants to see?

Browser-side

For the Web site, a site to serve a huge amount of users, using the server to remember the user's language is very troublesome, this work or from the browser, if the browser itself to tell the server want to use the language, the server is not easy?

A variety of browsers can set the language you want to use, in IE, choose Settings, Pop-up dialog box, you can set language preferences.

Click on the language to pop up the Language Preferences dialog box.

Set up in the Control Panel.

For Chrome, in advanced settings.

Click Language and input settings.

When the browser makes a request to the server, the browser adds a special request header Accept-language in the request header to illustrate the language type of the browser-side setting, and if you have just noticed, we can set up multiple languages, and we can adjust the order in In the Accept-language header, they appear in the order you set them. Each language is delimited with commas (,).

The accept-language of the various browsers are shown below.

The language of IE

Accept-language en-us,en;q=0.8, zh-hans-cn;q=0.5, zh-hans;q=0.3

Chrome's language

accept-language:zh-cn,zh;q=0.8, en-us;q=0.6, en;q=0.4

The language of FireFox

accept-language:en-us,zh-cn;q=0.8, en;q=0.5, zh;q=0.3

Wait, what's the Q again? There is a detailed description in the HTTP protocol, click here to see the detailed description of the link: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html. In simple terms, a language can carry a "Q" value to indicate how much the user likes the language, which is between 0-1 and the default is 1.

Server-side

In our server-side code, you can directly obtain the language settings that the browser carries in the request, in. NET, HttpRequest represents the request made by the browser, and the attribute userlanguages represents the language passed in Accept-language. has been sorted according to priority, and has been split according to the comma (,). This property is an array of strings.

In your code

string [] languages = request.userlanguages;

For example, if we want to take out the first language, we can get it through request.userlanguages[0].

The acquired language is just a string representing the language from the point of view of the program, and from an object-oriented perspective, we need an object to represent the language, which is the Curtureinfo class, which is defined in the namespace System.Globalization. You can see the definition of it through MSDN.

Typically, we pass a string representing a language through a constructor to CultureInfo.

New CultureInfo (culture);

Chinese questions

If you have just looked carefully, you will find that the language code sent by IE is different from Chrome. You will find that there are actually four ways to say Simplified Chinese:

    • Zh
    • Zh-cn
    • Zh-hans
    • Zh-hans-cn

Where does this come from, with detailed instructions in MSDN?

The CultureInfo class assigns a unique name to each culture according to RFC 4646. The name is a combination of the language associated with the ISO 639 lowercase two-letter culture code and the country/region associated with the ISO 3166 uppercase double word parent culture code.

For example, en means Chinese, Han: Chinese characters, s:simplified simplified, t:tranditional Traditional Chinese characters, and CN denotes China code.

The culture name format is two lowercase characters for the language code >-two uppercase characters for the country area code. For example, "ZH-CN" stands for "Chinese (Simplified Chinese)", "en-US" stands for English (United States). In cases where the two-letter language code is not available, a three-letter code derived from ISO 639-2 will be used.

Note that some culture names also specify the ISO 15924 notation. For example, Chinese is simplified and traditional, and this name can be used in two-character language codes-writing marks-two characters of the National Area code form. For example, "ZH-HANS-CN" stands for "Simplified Chinese (China)". For Windows Vista before the operating system, the culture name that contains the notation uses the Languagecode2-country/regioncode2-scripttag mode, for example "Uz-uz-cyrl" for " Uzbek (Uzbekistan, Cyrillic) ".

A neutral culture can only be specified by a two-letter lowercase language code. For example, FR specifies a neutral culture for "French", and the de specifies a neutral culture of "German".

Zh, then, represents the non-specific region of Chinese, en means the non-specific region of English.

However, it should be noted that CultureInfo does not support ZH-HANS-CN notation, and if you use it, you will receive an exception.

ZH-HANS-CN is an invalid culture identifier.

You can use ZH, ZH-CN, Zh-hans, but you can't use ZH-HANS-CN to build CultureInfo objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.