From af078ff6df2f2e0034733fb0766c2dc8f2edb867 Mon Sep 17 00:00:00 2001
From: Austin King <shout@ozten.com>
Date: Thu, 1 Mar 2012 14:00:17 -0800
Subject: [PATCH] Enhacing best language match to also match a language
 regardless of locale. Fixes Issue#1128

---
 lib/i18n.js         |  5 ++++-
 tests/i18n-tests.js | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/lib/i18n.js b/lib/i18n.js
index 8ca4d337d..18504bcfb 100644
--- a/lib/i18n.js
+++ b/lib/i18n.js
@@ -177,12 +177,15 @@ exports.parseAcceptLanguage = parseAcceptLanguage = function (header) {
  // supported languages, returns the best match or a default language.
  //
  // languages must be a sorted list, the first match is returned.
-function bestLanguage(languages, supported_languages, defaultLanguage) {
+exports.bestLanguage = bestLanguage = function(languages, supported_languages, defaultLanguage) {
   var lower = supported_languages.map(function (l) { return l.toLowerCase(); });
   for(var i=0; i < languages.length; i++) {
     var lq = languages[i];
     if (lower.indexOf(lq.lang.toLowerCase()) !== -1) {
       return lq.lang;
+    // Issue#1128 match locale, even if region isn't supported
+    } else if (lower.indexOf(lq.lang.slice(0, 2).toLowerCase()) !== -1) {
+      return lq.lang.slice(0, 2);
     }
   }
   return defaultLanguage;
diff --git a/tests/i18n-tests.js b/tests/i18n-tests.js
index 66c5e4bf5..4add4f433 100755
--- a/tests/i18n-tests.js
+++ b/tests/i18n-tests.js
@@ -54,6 +54,48 @@ suite.addBatch({
   }
 });
 
+suite.addBatch({
+  "We find exact language match": {
+    topic: function () {
+      var accept = 'pa,sv;q=0.8,fi;q=0.7,it-ch;q=0.5,en-us;q=0.3,en;q=0.2';
+      var supported = ['af', 'en-US', 'pa'];
+      var def = 'en-US';
+      return i18n.bestLanguage(
+          parseAcceptLanguage(accept),
+          supported, def);
+    },
+    "For Punjabi": function (err, locale) {
+      assert.equal(locale, "pa");
+    }
+  },
+  "Issue#1128 We find best locale even if region doesn't match": {
+    topic: function () {
+      var accept = 'pa-it,sv;q=0.8,fi;q=0.7,it-ch;q=0.5,en-us;q=0.3,en;q=0.2';
+      var supported = ['af', 'en-US', 'pa'];
+      var def = 'en-US';
+      return i18n.bestLanguage(
+          parseAcceptLanguage(accept),
+          supported, def);
+    },
+    "For Punjabi (India) serve Punjabi": function (err, locale) {
+      assert.equal(locale, "pa");
+    }
+  },
+  "We don't extend into a region, unless we have an exact match": {
+    topic: function () {
+      var accept = 'pa,sv;q=0.8,fi;q=0.7,it-ch;q=0.5,en-us;q=0.3,en;q=0.2';
+      var supported = ['af', 'en-US', 'pa-IT'];
+      var def = 'en-US';
+      return i18n.bestLanguage(
+          parseAcceptLanguage(accept),
+          supported, def);
+    },
+    "Don't choose Punjabi (India)": function (err, locale) {
+      assert.equal(locale, "en-us");
+    }
+  }
+});
+
 // run or export the suite.
 if (process.argv[1] === __filename) suite.run();
 else suite.export(module);
-- 
GitLab