Accept Language Module

Description

nginx_accept_language_module - Parses the Accept-Language field in HTTP headers and chooses the most suitable locale for the user from the list of locales supported at your website.

Available on github at giom/nginx_accept_language_module

Note

This module is not distributed with the NGINX source. See the installation instructions.

Example Configuration

set_from_accept_language $lang en ja pl;

where $lang is the variable in which to store the locale and en ja pl are the locales supported by the website.

If none of the locales from accept_language is available on your website, the variable (here, $lang) is set to the first locale in the list (here, en).

Note

It currently assumes that the accept-language is sorted by quality values (from my tests it’s the case for Safari, Firefox, Opera and IE) and discards q (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). In the situation where I’m using the module, this assumption works… but buyer beware :-)

Installation

  1. Download the module source from GitHub
  2. Unpack, and then compile NGINX with:
$ ./configure --add-module=path/to/nginx_accept_language_module

Why Did I Create It?

I’m using page caching with merb on a multi-lingual website and I needed a way to serve the correct language page from the cache.

Bugs

Send Bugs to Guillaume Maury (dev@gom-jabbar.org)

Credits

Thanks to Evan Miller for his guide on writing NGINX modules.

Alternative

You can manage $language_suffix by this setting when you cannot add this module into your system.

# accept-language: en,en-US;q=0.8,ja;q=0.6
set $first_language $http_accept_language;
if ($http_accept_language ~* '^(.+?),') {
    set $first_language $1;
}

set $language_suffix 'en';
if ($first_language ~* 'ja') {
    set $language_suffix 'ja';
}