Original: www.w3.org/Internation…

The premise

Language + Region: language of a specific region (for example, en-us, zh-hk)

Vanilla Language: parent language (e.g., en, zh)

For example, the vanilla language of En-us/EN-GB/EN-CA was en; Zh-hk [Chinese-Hong Kong] / zh-tw [Chinese-Taiwan] / zh-cn [Chinese-simplified] Vanilla language is zh.

The problem

How to specify the language of the content returned by the server through the browser language Settings?

Briefly answer

Most desktop browsers have Settings to view and modify language preferences, which tell the server what language users prefer to display pages and resources in (note the distinction between the browser interface language). However, for browsers on mobile devices, the language preferences are more determined by the operating system’s language Settings, or in some cases by the system language in which the browser was downloaded.

Browsers that allow users to change language preferences also typically allow users to specify multiple language preferences, so that subsequent language options are used in order if the first choice of language cannot be satisfied.

If your browser is set up to request a location-specific language, such as zh-hk (Chinese used in Hong Kong), make sure it is followed by specifying a vanilla language, such as zh.

You can use Internationalization Checker to see the language preference information that the browser carries with it when sending a request.

In most cases, the browser’s initial setup will do what you need. For example, if you have a Japanese version of your browser, the browser will often assume that you prefer to display pages in Japanese and send requests with Japanese preference information. The following details how to modify language preferences.

details

Technical background

When a browser pulls a document from a Web server, it sends a request, an HTTP request, to the server where the information is stored. In this request, the browser sends its language preferences.

Browsers request Language preferences via accept-language, and if the requested page has multiple Language versions on the server and the preferred Language version exists, they go through an HTTP content negotiation process to find and return the preferred version. If there is only one language version on the server, that version is returned. If none of the requested language versions exists, the default language version set by the server is returned.

Most major browsers allow modification of the language preferences for HTTP headers, which should conform to the BCP 47 specification (Tags for the Identification of Languages), usually two or three letter language codes (for example, FR for French), This is followed by an optional subcode representing the country or region (for example, FR-CA for The French language used in Canada; Es-419 stands for Spanish spoken in Latin America). For more language tags, see Language Tags.

When the browser’s language preferences are set to multiple languages, the browser sets a sequential weight to help the server understand your preferred language. Here is an example of an Accept-language with multiple Language values: DA is preferred, en-GB is returned if it is not available, and en is returned if it is not available (for example, en-us, en-ca, en-de, and so on).

Accept-Language: da,en-gb; q=0.8, en; q=0.7
Copy the code

Specify optional languages

You can usually specify several optional languages, in order. French/Switzerland [FR-ch] is preferred, and if not available, French [FR], German [de], English [en] will be requested.

To ensure that the server returns your preferred language version, you should configure the browser in order of Language + Region, Vanilla Language (for example, FR-ch -> FR). This ensures that the server looks for the FR-CH version and, if not available, any French version, then German. Because the preference is Swiss French, in case the preference is not available, of course French is a better choice than German because it is at least French.

Chrome automatically adds an arbitrary version of the language marker for the specified region or country (for example, zh for zh-cn), even if it is not listed in the language preferences Settings. Most browsers don’t do this, so it’s best to check the current accept-language value to see if any version is automatically added. If not, manually add any version to the Language preferences.

Check the Accept – Language headers

You may want to look at the current accept-language value before modifying the Language preferences. Run Internationalization Checker, enter any URL, and see Request Headers -> accept-language. See the demo.

Modify browser language preferences

Here we list examples of how to change the language preferences for the latest versions of many popular browsers on Windows or OS X desktop platforms. We used the latest versions of these browsers for this article.

For most mobile devices, language preferences are determined by the system’s first language and cannot be set separately.

Edge

  • On the Settings page open Regions & Language and select Option to add a language+ Region/Vanilla Language.

  • Once selected, you can reorder or remove a language from the language list.

  • Restart the Edge.

If you manually add a language+region language, Edge automatically adds a Vanilla Language after it, but does not show it in the language list. For example, if you add en-us manually, Edge implicitly adds an en after it, but you’ll only see en-us in the list of languages, not en. At this point, if you add another en-GB manually, the final list of languages is en-us -> en-GB -> en, and you only see en-us -> en-GB in the list of languages.

Chrome

  • Open the browser Settings, scroll to the bottom, click “Advanced” -> “Language” -> “Add Language”, and select Language + Region/Vanilla Language from the language list.
  • Adjust the order of the language preference list to your preference.
  • No need to restart Chrome.

If you add a language+region language, Chrome automatically adds a Vanilla Language after it. For example, if you add en-us manually, Chrome implicitly adds an en after it. If you add en-GB manually, the final language list is en-us -> en- > en-GB, and you need to manually place en-GB before en.

Firefox

  • Open the browser, choose General > Language > Add Language, and select Language + Region/Vanilla Language from the language list.
  • Adjust the order of the language preference list to your preference.

If you add a language+region language, Firefox will only add it, not automatically add its Vanilla language, you need to manually add its Vanilla language after it. For example, if you add en-us manually, Firefox does not automatically add en, you need to add en manually. If you add another en-GB, the final language list is en-us -> en- > en-GB, and you need to manually place en-GB before EN.

Safari

  • Safari’s language preferences are determined by OS X system language. To change, go to System Preferences -> [Language and Region] -> [Preferred Language], but note that it will change the user interface language, not just the browser language preferences, make sure you know how to switch back to your normal language!
  • You can add multiple system languages, but in Safari, the accept-language value is only for the highest-priority Language. If you add the following system Language: zh-CN -> EN-us -> EN-GB, the accept-language value is only zh-CN.

other

  • Why language+ Region and Vanilla Language in pairs?

    If your added language is Language + Region (for example, FR-CH), you should follow it with a corresponding Vanilla Language (for example, FR).

    This is because if the server is following the HTTP/1.1 specification and has a FR version but no FR-CH version, when accept-language is FR-CH, the server cannot return the FR version even if it cannot provide the FR-CH version.

    In practice, the Apache server still returns the FR version document when it encounters the above situation, because it implicitly adds an FR language to the language preference list. However, the implicitly added FR weights are low, and if your language preference list contains only one language, the Apache server will correctly return the Vanilla Language version; If you specify more than one language, you are likely to encounter problems. Apache Documentation explains it like this:

    Apache implicitly adds Vanilla language to the list of language preferences on the client side, but with very little weight. If accept-language is "en-GB; Q = 0.9, fr. Q =0.8", the server has en version and FR version, so when en-GB is not available, the server will return FR version even though en is implicitly added, because the weight is lower than FR.Copy the code

    Let’s look at an example, assuming that the Apache server has all but the French/Switzerland [FR-CH] version. Based on the list of preferences in the right figure, the Apache server always returns the FR version because French[FR] has the second highest weight. If we remove fr, as shown in the figure on the left, the Apache server will return the German[de] version because it has a lower weight than the French[FR] version, although it will implicitly add French[FR].

  • Content negotiation

    • Here’s a page with multiple language versions: The CSS2 Press Release has Dutch, English, German and Japanese versions. Set The browser language preference to The above available language. After each setting, you can click this link to check whether The page language is updated.
  • OS language VS Browser language VS browser interface language

    • OS Language Settings: Set the language in the Language and Region option. MAC OSX can be set in System Preferences -> Language and Region. Windows can be set in the Control Panel.

    • Browser Language Settings: Accept-language value, which can be obtained from navigator.languages.

    • Browser interface language: Language of the browser interface (such as the menu bar or address bar).

Links to other articles

TypeScript trampling tour

[例 句] Build and publish a TypeScript NPM package step by step

NPM install package-lock.json update policy

Browser language preferences

Date.prototype.toLocaleString()