-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
96 lines (86 loc) · 3.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var I18n = require('i18n');
var Boom = require('@hapi/boom');
var Hoek = require('@hapi/hoek');
var acceptLanguageParser = require('accept-language-parser');
var _ = require('lodash');
var pkg = require('./package.json');
exports.extractDefaultLocale = function(allLocales){
if (!allLocales) {
throw new Error('No locales defined!');
}
if (allLocales.length === 0) {
throw new Error('Locales array is empty!');
}
return allLocales[0];
};
function detectLocaleFromAcceptedLanguages(acceptedLanguages, localesSupported) {
var acceptedLanguageCodes = acceptLanguageParser.parse(acceptedLanguages);
var matchedLanguageFound = acceptedLanguageCodes.find(function (languageCode) {
return localesSupported.includes(languageCode.code);
});
if (matchedLanguageFound) {
var matchedLanguageCode = matchedLanguageFound.code;
if (matchedLanguageFound.region && localesSupported.includes(matchedLanguageFound.code + '-' + matchedLanguageFound.region)) {
return matchedLanguageFound.code + '-' + matchedLanguageFound.region;
}
return matchedLanguageFound.code;
}
}
exports.plugin = {
name: pkg.name,
version: pkg.version,
pkg: pkg ,
register: function(server, options){
var pluginOptions = options ? options : {};
if (pluginOptions.languageHeaderField) {
pluginOptions.languageHeaderField = pluginOptions.languageHeaderField.toLowerCase();
}
I18n.configure(pluginOptions);
var defaultLocale = pluginOptions.defaultLocale || exports.extractDefaultLocale(pluginOptions.locales);
if (!pluginOptions.locales) {
throw Error('No locales defined!');
}
server.ext('onRequest', function (request, h) {
request.i18n = {};
I18n.init(request, request.i18n);
request.i18n.setLocale(defaultLocale);
return h.continue;
});
server.ext('onPreAuth', function (request, h) {
if (request.params && request.params.languageCode) {
if (_.includes(pluginOptions.locales, request.params.languageCode) == false) {
throw Boom.notFound('No localization available for ' + request.params.languageCode);
}
request.i18n.setLocale(request.params.languageCode);
} else if (pluginOptions.queryParameter && request.query && request.query[pluginOptions.queryParameter]) {
if (_.includes(pluginOptions.locales, request.query[pluginOptions.queryParameter]) == false) {
throw Boom.notFound('No localization available for ' + request.query[pluginOptions.queryParameter]);
}
request.i18n.setLocale(request.query[pluginOptions.queryParameter]);
} else if (pluginOptions.languageHeaderField && request.headers[pluginOptions.languageHeaderField]) {
var matchedLanguageCode = detectLocaleFromAcceptedLanguages(
request.headers[pluginOptions.languageHeaderField],
pluginOptions.locales
);
if (matchedLanguageCode) {
request.i18n.setLocale(matchedLanguageCode);
}
}
return h.continue;
});
server.ext('onPreResponse', function (request, h) {
if (!request.i18n || !request.response) {
return h.continue;
}
var response = request.response;
if (Boom.isBoom(response)) {
return h.continue;
}
if (response.variety === 'view') {
response.source.context = Hoek.merge(response.source.context || {}, request.i18n);
response.source.context.languageCode = request.i18n.getLocale();
}
return h.continue;
});
}
};