A multi-language website with concrete5 and nginx

 

Having a website is fine but in several language it's better. To make a site which open in the right language for the visitor we'll use a bit of concrete5 logic and some nginx rewrite rules. The nginx5 rewrite rules could be made for whatever decent web server.

The idea is having as home page a page which asks the visitor for the language he wants and a subpage for every language we made a translation for. The home page will be used only if language detection failed. Such a page could be seen here.

When a browser wants to get a page it sends to the server the name of the page but also informations about his capabilities. One of this informations is what are the preferred languages. It's send in the accept_language header. We'll use it to write some nginx5 rewrite rules.

if ( $http_accept_language ~ ^(..) ) {
set $lang $1;
}
if ( $lang ~* fr ) {
rewrite ^(/|/index.php)$ /fr/;
}
if ( $lang ~* en ) {
rewrite ^(/|/index.php)$ /en/;
}
if ( $lang ~* es ) {
rewrite ^(/|/index.php)$ /es/;
}

 

Please note that we match on / and /index.php to avoid issues.

Et voilà! only thing to do is to adjust the depth of menus on the left to display only the menu for the current language.

Comments:

Posted by Brian on
I find that using map it makes it a lot shorter and more readable:

map $http_accept_language $sublang {
default en;
~* fr fr;
~* es es;
}

rewrite ^(/|/index.php)$ /$sublang/;
Leave a Reply



(Your email will not be publicly displayed.)

Please type the letters and numbers shown in the image.Captcha CodeClick the image to see another captcha.