From 27b4052f18934553bf5a8e8d02999f6874289c77 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Sat, 7 Jul 2012 08:03:21 -0600
Subject: [PATCH] move off of gettext, read strings from .json files rather
 than binary .mo files

---
 lib/i18n.js  | 96 ++++++++++++++++++++++++----------------------------
 package.json |  2 --
 2 files changed, 44 insertions(+), 54 deletions(-)

diff --git a/lib/i18n.js b/lib/i18n.js
index 374c87968..2f34b95dd 100644
--- a/lib/i18n.js
+++ b/lib/i18n.js
@@ -16,7 +16,6 @@
  */
 
 var logger = require('./logging.js').logger,
-    Gettext = require('node-gettext'),
     path = require('path'),
     util = require('util'),
     fs = require('fs');
@@ -30,7 +29,7 @@ if (typeof fs.existsSync === 'function') {
 
 const BIDI_RTL_LANGS = ['ar', 'db-LB', 'fa', 'he'];
 
-var mo_cache = {};
+var translations = {};
 
 /**
  * Connect middleware which is i18n aware.
@@ -55,39 +54,41 @@ exports.abide = function (options) {
   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(
-             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);
+  var json_dir = path.resolve(
+          path.join(__dirname, '..'),
+          path.join(options.i18n_json_dir));
+
+  var debug_locale = localeFrom(options.debug_lang);
 
   options.supported_languages.forEach(function (lang, i) {
-    var l = localeFrom(lang),
-        default_locale = localeFrom(options.default_lang);
+    var l = (lang === options.debug_lang ? 'db_LB' : localeFrom(lang));
+
+    // ignore .json files for en-US
+    if (lang == 'en-US') return;
+
+    try {
+      // populate the in-memory translation cache with client.json, which contains
+      // strings relevant on the server
+
+      // XXX: these files should be json.  not javascript.
+      var json_locale_data; // for jshint
+      eval(fs.readFileSync(path.join(json_dir, l, 'messages.json')).toString());
+      translations[l] = json_locale_data.messages;
 
-    mo_cache[l] = {
-      mo_exists: existsSync(mo_file_path(l)),
-      json_exists: existsSync(json_file_path(l)),
-      gt: null
-    };
-    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
-          if (! options.disable_locale_check) logger.warn(msg);
-        } else {
-          logger.error(msg);
-          throw msg;
-        }
+      // verify that client.json is present
+      if (!existsSync(path.join(json_dir, l, 'client.json'))) {
+        throw 'client.json';
+      }
+    } catch(e) {
+      // an exception here means that there was a problem with the translation files for
+      // this locale!
+      var msg = util.format('Bad locale=[%s] missing .json files in [%s]. See locale/README (%s)',
+                            l, path.join(json_dir, l), e);
+      if (!options.disable_locale_check) {
+        logger.warn(msg);
+      } else {
+        logger.error(msg);
+        throw msg;
       }
     }
   });
@@ -119,27 +120,18 @@ exports.abide = function (options) {
     resp.local('format', format);
     req.format = format;
 
-    if (mo_cache[locale].mo_exists) {
-      if (mo_cache[locale].gt === null) {
-        mo_cache[locale].gt = new Gettext();
-        var mo_path = mo_file_path(locale);
-        mo_cache[locale].gt.addTextdomain(locale,
-                                           fs.readFileSync(mo_path));
-        mo_cache[locale].gt.textdomain(locale);
-      }
-      var gt = mo_cache[locale].gt;
-      resp.local(options.gettext_alias, gt.gettext.bind(gt));
-      req.gettext = gt.gettext.bind(gt);
-      resp.local(options.ngettext_alias, gt.ngettext.bind(gt));
-      req.ngettext = gt.ngettext.bind(gt);
-   } else {
-      // en-US in a non gettext environment... fake it
-      var identity = function (a, b) { return a; };
-      resp.local(options.gettext_alias, identity);
-      req.gettext = identity;
-      resp.local(options.ngettext_alias, identity);
-      req.ngettext = identity;
+    var gt;
+
+    if (translations[locale]) {
+      gt = function(sid) {
+        return (translations[locale][sid] ? translations[locale][sid][1] : sid);
+      };
+    } else {
+      gt = function(a) { return a; }
     }
+    resp.local(options.gettext_alias, gt);
+    req.gettext = gt;
+
     next();
   };
 };
diff --git a/package.json b/package.json
index 925c8fd96..2ace3bd19 100644
--- a/package.json
+++ b/package.json
@@ -16,11 +16,9 @@
         "ejs": "0.4.3",
         "etagify": "0.0.2",
         "express": "2.5.0",
-        "iconv": "1.2.1",
         "mustache": "0.3.1-dev",
         "jwcrypto": "0.3.2",
         "mysql": "0.9.5",
-        "node-gettext": "0.1.1",
         "node-statsd": "https://github.com/downloads/lloyd/node-statsd/0509f85.tgz",
         "nodemailer": "0.1.24",
         "mkdirp": "0.3.0",
-- 
GitLab