Express middleware for localising your URLs based on user's location and language preferences.
Easily redirect unlocalised URLs to localised ones, for example:
www.site.com/path
becomes
www.site.com/gb/en/path
Install this modue with the following command:
npm install --save localise-url
This module is used as middleware for Express. It requires the express cookie-parser middleware to be executed first for each request.
The module checks whether the requested URL is localised (based on the options you provide it), and just moves on to the next middleware if it is. If it isn't, it results in a 302 redirect to a localised URL. This allows you to combine multiple types of path into one block of code:
var localiseUrl = require('localise-url')(options);
app.get(['/path', '/:country/:lang/path'],
localiseUrl,
//more middleware
);
Alternatively, they can be separated:
var localiseUrl = require('localise-url')(options);
app.get('/path',
localiseUrl);
app.get('/:country/:lang/path', function() {
// process request
});
Type: String
Default: '/'
The country and language parts of the path will be joined using this string.
Type: Boolean
Default: false
Reverse the country and language parts of the URL, so that language comes first.
Type: Boolean
Default: false
Makes the country part of the URL uppercase when generated. This does not force the module to match an uppercase country in the URL when seeing if the URL is already localised.
Type: Boolean
Default: false
Makes the country part of the URL uppercase when generated. This does not force the module to match an uppercase language in the URL when seeing if the URL is already localised.
Type: String
Default: country_iso
The name of the cookie to look for when determining country.
Type: String
Default: lang_iso
The name of the cookie to look for when determining language.
Type: Number
Default: 302
The HTTP code to return with the redirect. This must be a number between 300 and 308. Anything else will cause the module to default to a 302 redirect.
The user's country is determined using the following rules:
- The value of the 'country_iso' cookie, if it exists
- The value of the 'GEO' HTTP header, which should take the form "COUNTRIES:US", for example.
- Defaults to 'gb'
The user's language is determined using the following rules:
- The value of the 'lang_iso' cookie, if it exists
- First supported language found in Accept-Language HTTP header, if present. Supported languages are
['en', 'de', 'fr', 'zh']
- Default language for the user's country (as determined above).
- Default to 'en'
Copyright (c) 2015 Matthew Green. See LICENSE for details.