When we open a lot of websites, you may find that it automatically judges and jumps to different language interfaces according to the language you use. For example: Google is you in different languages, then enter the Google homepage can automatically turn to the language you use. In this era of network popularization, the main way of foreign trade must be the network. Maybe your corporate website has both Chinese and English versions (if your company has a foreign trade business). So how do their sites implement this function? Here we look at the header,http_accept_language of the Web server is the language that the server receives, generally the language of the server. In the Web site can be seen, Firefox in the installation of multiple language packs, will send multiple language information, but the first choice is definitely in the number one.
Simplified and traditional judgments
This function has been open for a long time, but it was not until yesterday that Atom.xml was omitted ...
I want to provide simplified or traditional files based on the HTTP header accept-language decision. In Chrome, chrome://settings/languages can set the preference language, and the browser will set the Accept-language header accordingly. A better approach is to parse the field, get Qvalue, and choose the most appropriate language according to the priority. But only for the support of simple traditional, I would like to use the method of trickery: ignore priority, as long as Accept-language appeared in the Zh-hant, ZH-TW, ZH-HK, and so on, return to the traditional, or return to simplified.
Map $http _accept_language $lang {
default zhs;
~zh-hant Zht;
~ZH-TW Zht;
~ZH-HK zht;
}
I use Hexo to generate the website, the source file is written in traditional Chinese. For the 2015-10-06-nginx-accept-language-zhs-zht.html generated by Hexo generate, a simplified version is obtained by OPENCC conversion: 2015-10-06- Nginx-accept-language-zhs-zht.html.zhs.html. Depending on the situation, some other files, such as Atom.xml, and Popular.json that provide "read most articles" feature are also required to be converted.
# zsh
cd ~/maskray.me/public
opencc-c t2s.json-i Atom.xml-o atom.xml.zhs.xml for
i in **/*.html 20*; Select a simple traditional support file
c=${#${(s/.html/%) i}//[^%]/} # COMPUTE substring '. html ' occurrences
if (($c <= 1)); Then # occurs once for the original file and needs to be converted to simplified C7/>opencc-c t2s.json-i $i-o $i. zhs.html
fi done
To specify a route in the Nginx configuration file that requires simple traditional support:
Location ~ ^/blog/20?? {
try_files $uri. $lang. html $uri =404;
Add_header Vary accept-language;
}
Location ~/atom.xml {
try_files $uri. $lang. xml $uri =404;
Add_header Vary accept-language;
}
Location ~ \.json$ {
try_files $uri. $lang. JSON $uri =404;
Add_header Vary accept-language;
}
# Other routes that require simple traditional support
forward to a different page according to the Accept-language in the HTTP request header:
directly on the code
if ($http _accept_language ~* ^zh) {
set $lang '/index_cn.jsp ';
}
if ($http _accept_language!~* ^zh) {
set $lang '/index_en.jsp ';
}
Location =/{
proxy_set_header Host $host;
Proxy_set_header x-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $remote _addr;
Proxy_pass Http://localhost:8080$lang;
}
Test:
Http://www.findmaven.net is a Findjar and Findmaven search engine.
Browser settings (English)
Return
Browser settings (Chinese)
Return