From f539c23557b6a97f621c9b61448e0f09a11e2c26 Mon Sep 17 00:00:00 2001 From: Daniel Wardin Date: Wed, 6 Feb 2019 16:48:48 +0000 Subject: [PATCH] Make the language source header customizable --- README.md | 3 +++ i18n.js | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d0a29666..7be87021 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,9 @@ i18n.configure({ // sets a custom cookie name to parse locale settings from - defaults to NULL cookie: 'yourcookiename', + + // sets a custom header name to read the language preference from - accept-language header by default + header: 'accept-language', // query parameter to switch locale (ie. /home?lang=ch) - defaults to NULL queryParameter: 'lang', diff --git a/i18n.js b/i18n.js index 0870cac9..81f403a5 100644 --- a/i18n.js +++ b/i18n.js @@ -45,6 +45,7 @@ module.exports = (function() { pathsep = path.sep, // ---> means win support will be available in node 0.8.x and above autoReload, cookiename, + languageHeaderName, defaultLocale, directory, directoryPermissions, @@ -101,6 +102,9 @@ module.exports = (function() { // sets a custom cookie name to parse locale settings from cookiename = (typeof opt.cookie === 'string') ? opt.cookie : null; + + // set the custom header name to extract the language locale + languageHeaderName = (typeof opt.header === 'string') ? opt.header : 'accept-language'; // query-string parameter to be watched - @todo: add test & doc queryParameter = (typeof opt.queryParameter === 'string') ? opt.queryParameter : null; @@ -647,7 +651,7 @@ module.exports = (function() { var guessLanguage = function(request) { if (typeof request === 'object') { - var languageHeader = request.headers? request.headers['accept-language'] : undefined, + var languageHeader = request.headers? request.headers[languageHeaderName] : undefined, languages = [], regions = [];