Reviewers: shindig-dev,

Description:
Allows using ${Msg.key} to get localized messages by key from the gadget
Prefs object via getMsg(key).

Please review this at http://codereview.appspot.com/6463

Affected files:
  features/opensocial-templates/base.js
  features/opensocial-templates/compiler.js


Index: features/opensocial-templates/base.js
===================================================================
--- features/opensocial-templates/base.js       (revision 701197)
+++ features/opensocial-templates/base.js       (working copy)
@@ -79,6 +79,7 @@
 os.VAR_my = "$my";
 os.VAR_cur = "$cur";
 os.VAR_node = "$node";
+os.VAR_msg = "Msg";
 os.VAR_parentnode = "$parentnode";
 os.VAR_uniqueId = "$uniqueId";
 os.VAR_identifierresolver = "$_ir";
@@ -172,6 +173,38 @@
 };

 /**
+ * A singleton instance of the current gadget Prefs - only instantiated if
+ * we are in a gadget container.
+ * @type gadgets.Prefs
+ */
+os.gadgetPrefs_ = null;
+if (window['gadgets'] && window['gadgets']['Prefs']) {
+  os.gadgetPrefs_ = new window['gadgets']['Prefs']();
+};
+
+/**
+ * A convenience function to get a localized message by key from the shared
+ * gadgets.Prefs object.
+ * @param {string} key The message key to get
+ * @return {string|null} The localized message for a given key, or null if not
+ * found, or not in the gadgets environment.
+ */
+os.getPrefMessage = function(key) {
+  if (!os.gadgetPrefs_) {
+    return null;
+  }
+  return os.gadgetPrefs_.getMsg(key);
+};
+
+/**
+ * A convenience function for identifier resolver to be able to use
+ * getPrefMessage() both as ${Msg.foo} and ${Msg('foo')}.
+ */
+os.getPrefMessage.get = function(key) {
+  return os.getPrefMessage(key);
+};
+
+/**
  * Globally disallowed dynamic attributes. These are the attributes where
  * ${} notation will be ignored reguardless of the tag.
  */
Index: features/opensocial-templates/compiler.js
===================================================================
--- features/opensocial-templates/compiler.js   (revision 701197)
+++ features/opensocial-templates/compiler.js   (working copy)
@@ -516,9 +516,9 @@
  */
 os.createContext = function(data, opt_globals) {
   var context = JsEvalContext.create(data);
-  //context.setVariable(os.VAR_cur, os.getValueFromObject_);
   context.setVariable(os.VAR_callbacks, []);
   context.setVariable(os.VAR_identifierresolver, os.getFromContext);
+  context.setVariable(os.VAR_msg, os.getPrefMessage);
   if (opt_globals) {
     for (var global in opt_globals) {
       context.setVariable(global, opt_globals[global]);


Reply via email to