From 28cde487de4db28acd0152af999d317f5036f5ee Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Sun, 7 Jul 2013 23:37:30 +0100 Subject: [PATCH] Use async readdir in locales directory, reorder checking on locale files --- server/httphandler.js | 60 +++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/server/httphandler.js b/server/httphandler.js index e58a9de..16320f2 100644 --- a/server/httphandler.js +++ b/server/httphandler.js @@ -38,10 +38,12 @@ HttpHandler.prototype.serve = function (request, response) { // 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) { @@ -53,40 +55,42 @@ HttpHandler.prototype.serve = function (request, response) { }; 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', -- 2.25.1