From 2f0b09a4947d1ba0315a2aa30ed437f41ddb7615 Mon Sep 17 00:00:00 2001
From: Austin King <shout@ozten.com>
Date: Fri, 3 Feb 2012 10:44:17 -0800
Subject: [PATCH] Adding debug_lang, i18n_json_dir config options. Bailing if
 json is missing. Fixes Issue#1045.

---
 bin/browserid        |  1 +
 config/dev.json      |  1 +
 lib/configuration.js |  1 +
 lib/i18n.js          | 38 +++++++++++++++++++++++++-------------
 4 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/bin/browserid b/bin/browserid
index 936647854..cf199e123 100755
--- a/bin/browserid
+++ b/bin/browserid
@@ -65,6 +65,7 @@ app.use(express.logger({
 app.use(i18n.abide({
   supported_languages: config.get('supported_languages'),
   default_lang: config.get('default_lang'),
+  debug_lang: config.get('debug_lang'),
   locale_directory: config.get('locale_directory')
 }));
 
diff --git a/config/dev.json b/config/dev.json
index 3ef84b7ca..943b15bf3 100644
--- a/config/dev.json
+++ b/config/dev.json
@@ -23,6 +23,7 @@
   "disable_primary_support": false,
   "enable_code_version": false,
   "default_lang": "en-US",
+  "debug_lang": "it-CH",
   "locale_directory": "/home/browserid/code/locale",
   "supported_languages": [
     "af", "ca", "cs", "db-LB", "de", "en-US", "eo", "es", "es-MX", "eu", "fr", 
diff --git a/lib/configuration.js b/lib/configuration.js
index 81cf11b78..c7c50d645 100644
--- a/lib/configuration.js
+++ b/lib/configuration.js
@@ -137,6 +137,7 @@ var conf = module.exports = convict({
     host: 'string?'
   },
   default_lang: 'string = "en-US"',
+  debug_lang: 'string = "it-CH"',
   supported_languages: 'array { string }* = [ "en-US" ]',
   locale_directory: 'string = "locale"',
   express_log_format: 'string [ "default", "dev" ] = "default"',
diff --git a/lib/i18n.js b/lib/i18n.js
index 314049da3..8ca4d337d 100644
--- a/lib/i18n.js
+++ b/lib/i18n.js
@@ -44,29 +44,41 @@ exports.abide = function (options) {
   if (! options.default_lang)        options.default_lang = 'en-US';
   if (! options.debug_lang)          options.debug_lang = 'it-CH';
   if (! options.locale_directory)    options.locale_directory = 'locale';
+  if (! options.i18n_json_dir)       options.i18n_json_dir = 'resources/static/i18n/';
 
   var mo_file_path = function (locale) {
-    return path.resolve(
+        return path.resolve(
              path.join(__dirname, '..'),
                        options.locale_directory,
                        path.join(locale, 'LC_MESSAGES', 'messages.mo'));
-  };
+      },
+      json_file_path = function (locale) {
+        return path.resolve(
+             path.join(__dirname, '..'),
+                       path.join(options.i18n_json_dir, locale, 'client.json'));
+      }, 
+      debug_locale = localeFrom(options.debug_lang);
 
   options.supported_languages.forEach(function (lang, i) {
-    var l = localeFrom(lang);
+    var l = localeFrom(lang),
+        default_locale = localeFrom(options.default_lang);
 
     mo_cache[l] = {
-      exists: path.existsSync(mo_file_path(l)),
+      mo_exists: path.existsSync(mo_file_path(l)),
+      json_exists: path.existsSync(json_file_path(l)),
       gt: null
     };
-
-    if (! mo_cache[l].exists && l !== 'it_CH') {
-      var msg = util.format('Bad locale=[%s] mo file does not exist: [%s]. See locale/README', l, mo_file_path(l));
-      if (l == 'en_US') {
-        logger.warn(msg);
-      } else {
-        logger.error(msg);
-        throw msg;
+    if (l !== debug_locale) {
+      if (! mo_cache[l] || ! mo_cache[l].mo_exists || ! mo_cache[l].json_exists) {
+        var msg = util.format('Bad locale=[%s] file(s) do not exist [%s] or [%s]. See locale/README', 
+                              l, mo_file_path(l), json_file_path(l));
+        if (mo_cache[l].json_exists && l == default_locale) {
+          // mo files aren't critical... carry on
+          logger.warn(msg);
+        } else {
+          logger.error(msg);
+          throw msg;
+        }
       }
     }
   });
@@ -98,7 +110,7 @@ exports.abide = function (options) {
     resp.local('format', format);
     req.format = format;
 
-    if (mo_cache[locale].exists) {
+    if (mo_cache[locale].mo_exists) {
       if (mo_cache[locale].gt === null) {
         mo_cache[locale].gt = new Gettext();
         mo_path = mo_file_path(locale);
-- 
GitLab