Formatting + comments in httphandler.js
authorDarren <darren@darrenwhitlen.com>
Wed, 24 Jul 2013 19:52:59 +0000 (20:52 +0100)
committerDarren <darren@darrenwhitlen.com>
Wed, 24 Jul 2013 19:52:59 +0000 (20:52 +0100)
server/httphandler.js

index 6b18bb8dee57f0569d5946e60046e208d3d43ee0..045dd2f806b049fd54707cb2cae4d00547c45d36 100644 (file)
@@ -55,37 +55,45 @@ HttpHandler.prototype.serve = function (request, response) {
 
 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);
     }
 };