var serveMagicLocale = function (request, response) {
var that = this;
+
if (request.headers['accept-language']) {
fs.readdir('client/assets/locales', function (err, files) {
var available = [],
i = 0,
langs = request.headers['accept-language'].split(','); // Example: en-gb,en;q=0.5
+ // Get a list of the available translations we have
files.forEach(function (file) {
if (file.substr(-5) === '.json') {
available.push(file.slice(0, -5));
}
});
+ // Sanitise the browsers accepted languages and the qualities
for (i = 0; i < langs.length; i++) {
langs[i]= langs[i].split(';q=');
+ langs[i][0] = langs[i][0].toLowerCase();
langs[i][1] = (typeof langs[i][1] === 'string') ? parseFloat(langs[i][1]) : 1.0;
}
+
+ // Sort the accepted languages by quality
langs.sort(function (a, b) {
return b[1] - a[1];
});
+
+ // Serve the first language we have a translation for
for (i = 0; i < langs.length; i++) {
- langs[i][0] = langs[i][0].toLowerCase();
if (langs[i][0] === '*') {
break;
} else if (_.contains(available, langs[i][0])) {
return that.file_server.serveFile('/assets/locales/' + langs[i][0] + '.json', 200, {Vary: 'Accept-Language', 'Content-Language': langs[i][0]}, request, response);
}
}
- serveFallbackLocale.call(this, request, response);
+
+ serveFallbackLocale.call(that, request, response);
});
} else {
- serveFallbackLocale.call(this, request, response);
+ serveFallbackLocale.call(that, request, response);
}
};