forked from tinganho/node-accept-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
312 lines (269 loc) · 8.6 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
* Dependencies
*/
var bcp47 = require('bcp47');
/**
* AcceptLanguage
*/
function AcceptLanguage() {
this.byLowerCaseTag = {};
}
/**
* Language tags
*
* @type {Objects}
* @public
*/
AcceptLanguage.prototype.languageTags_ = {};
/**
* Default language tag
*
* @type {String}
*/
AcceptLanguage.prototype.defaultLanguageTag = null;
/**
* Prune language tags that aren't defined
*
* @param {Array.<languageTag>} languageTags
* @return {Array.<{ value: String, quality: Number }>}
* @private
*/
AcceptLanguage.prototype.prune_ = function(languageTags) {
var this_ = this;
if(Object.keys(this.languageTags_).length > 0) {
languageTags = languageTags
.filter(function(languageTag) {
var language = languageTag.language;
// Filter non-defined language tags.
if(typeof this_.languageTags_[language] === 'undefined') {
return false;
}
return true;
})
.map(function(languageTag) {
var language = languageTag.language;
if(languageTag.region) {
var regionIndex = this_.languageTags_[language].regions.indexOf(languageTag.region);
var hasRegion = true;
if(regionIndex === -1) {
hasRegion = false;
regionIndex = 0;
}
// It should return the first matching region language tag
// only if it doesn't contain ony root language tag.
// So if the define language tags are ['es-419', 'es-US']
// and the Accept-Language string is ['es-ES']. We should
// return 'es-419', because it has the biggest priority.
//
// Whenever it matches only language subtag and not region
// tags and there exist one root language tag. We should
// return the root language tag. E.g. If we have the set
// ['es', 'es-419'] and the Accept-Language string is
// 'es-ES'. Then we should return just ['es'].
//
// Wheneve it matches both language and region subtag it
// should return that matched language tag, regardless if
// there exist any root only languae subtag. E.g. If we
// have the set ['es', 'es-419', 'es-PO'] and the Accept-
// Language header is 'es-419'. Then we should return
// ['es-419'].
if(typeof this_.languageTags_[language].values[regionIndex] !== 'undefined') {
if(hasRegion || !this_.languageTags_[language].onlyLanguageValue) {
return {
value: this_.languageTags_[language].values[regionIndex],
language: language,
region: this_.languageTags_[language].regions[regionIndex] || null,
quality: languageTag.quality
};
}
}
return {
value: this_.languageTags_[language].onlyLanguageValue,
language: language,
region: null,
quality: languageTag.quality
};
}
return languageTag;
});
}
// If no language tags matches the defined set
if(languageTags.length === 0 && this_.defaultLanguageTag) {
return [this_.defaultLanguageTag];
}
return languageTags;
};
/**
* Define languages
*
* @param {Array.<String>} languageTags
* @return {void}
* @throws {TypeError}
* @public
*/
AcceptLanguage.prototype.languages = function(languageTags) {
var this_ = this;
var byLowerCaseTag = {};
// Reset language tags
this.languageTags_ = {};
this.byLowerCaseTag = byLowerCaseTag;
languageTags.forEach(function(languageTagString) {
var languageTag = bcp47.parse(languageTagString);
if(!languageTag) {
throw new TypeError('Your language tag (' + languageTagString + ') are not bcp47 compliant. For more info https://tools.ietf.org/html/bcp47.');
}
var language = languageTag.langtag.language.language;
if(!language) {
throw new TypeError('Your language tag (' + languageTagString + ') is not supported.');
}
var region = languageTag.langtag.region;
byLowerCaseTag[languageTagString.toLowerCase()] = languageTagString;
if(!this_.languageTags_[language]) {
this_.languageTags_[language] = {
values: region ? [languageTagString] : [],
regions: region ? [region] : [],
onlyLanguageValue: null
};
}
else {
if(region) {
this_.languageTags_[language].values.push(languageTagString);
this_.languageTags_[language].regions.push(region);
}
}
if(!region) {
this_.languageTags_[language].onlyLanguageValue = languageTagString;
}
});
var defaultLanguageTag = bcp47.parse(languageTags[0]);
this.defaultLanguageTag = {
value: languageTags[0],
language: defaultLanguageTag.langtag.language.language,
region: defaultLanguageTag.langtag.region,
quality: 1.0
};
};
/**
* Parse accept language string
*
* @param {String} string Accept-Language string
* @return {Array.<{ value: String, quality: Number }>}
* @public
*/
AcceptLanguage.prototype.parse = function(string) {
if(typeof string !== 'string' || string.length === 0) {
return this.defaultLanguageTag ? [this.defaultLanguageTag] : [];
}
var languageTags = string.split(',');
languageTags = languageTags.map(function(languageTagString) {
languageTagString = languageTagString.replace(/\s+/, '');
var components = languageTagString.split(';');
var languageTag = bcp47.parse(components[0]);
if(!languageTag) {
return null;
}
return {
value: components[0],
language: languageTag.langtag.language.language,
region: languageTag.langtag.region,
quality: components[1] ? parseFloat(components[1].split('=')[1]) : 1.0
};
})
// Filter non-defined language tags
.filter(function(languageTag) {
return languageTag;
})
// Sort language tags
.sort(function(a, b) {
return b.quality - a.quality;
});
return this.prune_(languageTags);
};
/**
* Get most suitable language tag
*
* @param {String} string Accept-Language string
* @return {String}
* @public
*/
AcceptLanguage.prototype.get = function(string) {
return this.parse(string)[0].value;
};
/**
* Look up most suitable language tag according to BCP 47 rules, more precisely RFC 4647 "3.4. Lookup"
*
* @param {String} languagePriorityList Accept-Language header string
* @return {String}
* @public
*/
AcceptLanguage.prototype.lookup = function(languagePriorityList) {
if(typeof languagePriorityList !== 'string' || languagePriorityList.length === 0) {
return null;
}
var parsedAndSortedLanguageRanges = languagePriorityList.split(',').map(function(weightedLanguageRange) {
var components = weightedLanguageRange.replace(/\s+/, '').split(';');
return {
languageRange: components[0],
quality: components[1] ? parseFloat(components[1].split('=')[1]) : 1.0
};
})
// sort language ranges
.sort(function(a, b) {
return b.quality - a.quality;
});
// Array findIndex() would be more elegant, but it needs ECMAScript 2015
var i0, n0;
var languageRange; // term "language range" as used in RFC 4647
var prefixes;
var i1, n1;
var match;
for (i0=0, n0=parsedAndSortedLanguageRanges.length; i0<n0; i0++) {
languageRange = parsedAndSortedLanguageRanges[i0].languageRange;
if (match = this.byLowerCaseTag[languageRange.toLowerCase()]) {
return match;
}
prefixes = getPrefixes(languageRange);
for (i1=0, n1=prefixes.length; i1<n1; i1++) {
if (match = this.byLowerCaseTag[prefixes[i1]]) {
return match;
}
}
}
return null;
function getPrefixes(languageRange) {
var languageTag = bcp47.parse(languageRange);
if (!languageTag || !languageTag.langtag.language.language) { // langtag languages only
return [];
}
var langtag = languageTag.langtag;
var prefix = langtag.language.language.toLowerCase(); // the primary language is the shortest prefix
var prefixes = [prefix];
var delimiter = '-';
['extlang', 'script', 'region', 'variant', 'extension', 'privateuse'].forEach(function(property) {
if (property === 'privateuse') {
delimiter = '-x-';
}
if (langtag[property] instanceof Array) {
langtag[property].forEach(check);
} else {
check(langtag[property]);
}
function check(subtag) {
if (subtag) {
prefix += delimiter + subtag.toLowerCase();
delimiter = '-';
prefixes.push(prefix);
}
}
});
return prefixes.reverse();
}
};
/**
* For use as a singleton
*/
module.exports = new AcceptLanguage();
/**
* For use as a non-singleton
*/
module.exports.AcceptLanguage = AcceptLanguage;