// If the 'magic' translation is requested, figure out the best language to use from
// the Accept-Language HTTP header. If nothing is suitible, serve an empty response,
// Kiwi will just use the default en-gb strings baked in to it.
- if (request.url === '/assets/locales/magic.json') {
- return serveMagicLocale.call(this, request, response);
- } else if (request.url.substr(0, 16) === '/assets/locales/') {
- response.setHeader('Content-Language', request.url.substr(16, request.url.indexOf('.') - 16));
+ if (request.url.substr(0, 16) === '/assets/locales/') {
+ if (request.url === '/assets/locales/magic.json') {
+ return serveMagicLocale.call(this, request, response);
+ } else {
+ response.setHeader('Content-Language', request.url.substr(16, request.url.indexOf('.') - 16));
+ }
}
this.file_server.serve(request, response, function (err) {
};
var serveMagicLocale = function (request, response) {
- var langs = [],
- available = [],
- i = 0;
if (request.headers['accept-language']) {
// Example: en-gb,en;q=0.5
langs = request.headers['accept-language'].split(',');
- available = (function () {
- var files = [],
- l = [];
- files = fs.readdirSync('client/assets/locales');
+ fs.readdir('client/assets/locales', function (err, files) {
+ var available = [],
+ i = 0,
+ langs = [];
+
files.forEach(function (file) {
- if (file.slice(-5) === '.json') {
- l.push(file.slice(0, -5));
+ if (file.substr(-5) === '.json') {
+ available.push(file.slice(0, -5));
}
});
- return l;
- })();
- for (i = 0; i < langs.length; i++) {
- langs[i] = langs[i].split(';q=');
- langs[i][1] = (typeof langs[i][1] === 'string') ? parseFloat(langs[i][1]) : 1.0;
- }
- langs.sort(function (a, b) {
- return b[1] - a[1];
- });
- for (i = 0; i < langs.length; i++) {
- if (langs[i][0] === '*') {
- break;
- } else if (_.contains(available, langs[i][0])) {
- return this.file_server.serveFile('/assets/locales/' + langs[i][0] + '.json', 200, {Vary: 'Accept-Language', 'Content-Language': langs[i][0]}, request, response);
+ for (i = 0; i < langs.length; i++) {
+ langs[i]= langs[i].split(';q=');
+ langs[i][1] = (typeof langs[i][1] === 'string') ? parseFloat(langs[i][1]) : 1.0;
}
- }
+ langs.sort(function (a, b) {
+ return b[1] - a[1];
+ });
+ for (i = 0; i < langs.length; i++) {
+ if (langs[i][0] === '*') {
+ break;
+ } else if (_.contains(available, langs[i][0])) {
+ return this.file_server.serveFile('/assets/locales/' + langs[i][0] + '.json', 200, {Vary: 'Accept-Language', 'Content-Language': langs[i][0]}, request, response);
+ }
+ }
+ serveFallbackLocale(response);
+ });
+ } else {
+ serveFallbackLocale(response);
}
+};
+var serveFallbackLocale = function (response) {
response.writeHead(200, {
'Vary': 'Accept-Language',
'Content-Type': 'application/json',