Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the language source header customizable #390

Merged
merged 1 commit into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 5 additions & 1 deletion i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];

Expand Down