Browse files

Added client side gettext.js implementation from mozilla/browserid

  • Loading branch information...
1 parent abc4332 commit d3b48c1e0fad9f94274ccd9d8f5f7ca56c2486c1 @ozten ozten committed Apr 18, 2012
Showing with 44 additions and 0 deletions.
  1. +44 −0 static/gettext.js
View
44 static/gettext.js
@@ -0,0 +1,44 @@
+/*globals json_locale_data: true */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+(function() {
+ "use strict";
+
+ function Gettext(params) {
+ return {
+ gettext: function (msgid) {
+ if (json_locale_data && json_locale_data["client"]) {
+ var dict = json_locale_data["client"];
+ if (dict[msgid] && dict[msgid].length >= 2 &&
+ dict[msgid][1].trim() != "") {
+ return dict[msgid][1];
+ }
+ }
+ return msgid;
+ },
+ // See lib/i18n.js format docs
+ format: function (fmt, obj, named) {
+ 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 {
+ return fmt.replace(/%s/g, function(match){return String(obj.shift())});
+ }
+ }
+ };
+ };
+
+ var params = {
+ "domain" : "client",
+ "locale_data" : json_locale_data
+ };
+ var gt = new Gettext(params);
+ window.gettext = gt.gettext.bind(gt);
+ window.format = gt.format.bind(gt);
+
+}());

0 comments on commit d3b48c1

Please sign in to comment.