Nginx a way to configure page jumps based on different browser languages _nginx

Source: Internet
Author: User

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

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.