Accept-Language

We can get from the request header the language type that the browser wants to receive

let http = require('http');
http.createServer(function(req,res){
	res.end(req.headers['accept-language']); }).listen(8080); < < < output useful - CN, useful; Q = 0.9Copy the code

Several languages are separated by, and each language is available; Separator, preceded by the abbreviation of the language and followed by its weight (priority).

LangPackage/language pack

A web site that supports multiple languages generally has multiple language packages stored on its server. When a client makes a request to it, the server looks at the request header to see which language the client is willing to support, and then looks it up in its own language package. The receiving list given by the client contains weights for each language, and the server returns the one the client has and prefers. If the language package stored by the server does not match the list given by the client, the default language package preset by the server will generally be used.

Language pack Examples

let langPack = {
	"zh":{
    	title:'Hello world! '},"en":{
    	title:"hello world!"}}Copy the code

Encapsulation getLang

This method automatically identifies the language type that the client is willing to receive, and then selects the most appropriate one from the multiple language packages stored on the server to return data.

The end result is something like this

getLang(req,'title')
Copy the code

Design ideas

Parse the Accept-language into an array and sort it by weight

The Accept-language first needs to be parsed into individual objects. Each object represents a language and has two properties:

  • LangType: indicates the type of the language

  • Q: The weight of the language helps us filter out which language is preferred by the client

Each object is then placed in an array, sorted by weight from largest to smallest.

Select the language type

Then, the langType of array members is matched with the language package stored in the server successively until the match is successful or the traversal ends. If there is still no match at the end of the traversal, the data will be returned according to the default language type of the server.

Return the requested data

Finally, we choose a language, and we can use the second parameter of the getLang method to decide what data to take from the language package.

The source code

function getLang(req,dataKey){
  let langPack = {
    'zh':{
      data:'Hello world! '},'en':{
      data:'hello world! '}}; //-------------------------------------------------let acceptLanguage = req.headers['accept-language']
    ,langs = acceptLanguage.split(', ')
    ,langType = 'en'; // Parse accept-language into an array and sort by weight langs = langs.map(function(lang){
    let [langType,langQ] = lang.split('; ');
    letq = langQ? parseFloat(langQ.split('=') [1]) : 1; lang = { langType ,q };returnlang; }).sort((a,b)=>b.q-a.q); // Select the language typefor(leti=0; i<langs.length; ++i){let curType = langs[i].langType;
    if(langPack[curType]){
      langType = curType;
      break; }} // Return the requested datareturn langPack[langType][dataKey];
}
Copy the code