From 9340251a33f84dd1bced80464499c0b107727b27 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Sat, 7 Jul 2012 15:19:13 -0600
Subject: [PATCH] make the format() function for l10n more ergonomic - don't
 require a third argument

---
 lib/i18n.js                           | 12 +++++++-----
 resources/static/common/js/gettext.js | 13 +++++++------
 2 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/lib/i18n.js b/lib/i18n.js
index 374c87968..a17cb8678 100644
--- a/lib/i18n.js
+++ b/lib/i18n.js
@@ -251,15 +251,17 @@ exports.languageFrom = function (locale) {
  * of values for positional replacement.
  *
  * Named Example:
- * format("%(salutation)s %(place)s", {salutation: "Hello", place: "World"}, true);
+ * format("%(salutation)s %(place)s", {salutation: "Hello", place: "World"});
  * Positional Example:
  * format("%s %s", ["Hello", "World"]);
  */
 var format = exports.format = function (fmt, obj, named) {
-  if (! fmt) return "";
-  if (named) {
-    return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])});
-  } else {
+  if (!fmt) return "";
+  if (Array.isArray(obj) || named === false) {
     return fmt.replace(/%s/g, function(match){return String(obj.shift())});
+  } else if (typeof obj === 'object' || named === true) {
+    return fmt.replace(/%\(\s*([^)]+)\s*\)/g, function(m, v){
+      return String(obj[v]);
+    });
   }
 };
diff --git a/resources/static/common/js/gettext.js b/resources/static/common/js/gettext.js
index 2b24477c5..0ee2f7e36 100644
--- a/resources/static/common/js/gettext.js
+++ b/resources/static/common/js/gettext.js
@@ -20,14 +20,16 @@
         },
         // See lib/i18n.js format docs
         format: function (fmt, obj, named) {
-          if (! fmt) return "";
-          if (! fmt.replace) {
+          if (!fmt) return "";
+          if (!fmt.replace) {
             return fmt;
           }
-          if (named) {
-            return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])});
-          } else {
+          if (_.isArray(obj) || named === false) {
             return fmt.replace(/%s/g, function(match){return String(obj.shift())});
+          } else if (_.isObject(obj) || named === true) {
+            return fmt.replace(/%\(\s*([^)]+)\s*\)/g, function(m, v){
+              return String(obj[v]);
+            });
           }
         }
       };
@@ -40,5 +42,4 @@
   var gt = new Gettext(params);
   window.gettext = gt.gettext.bind(gt);
   window.format = gt.format.bind(gt);
-
 }());
-- 
GitLab