You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2012/10/25 00:49:33 UTC

[1/8] git commit: Finish the job of exposing the application message catalog to the client as a module

Updated Branches:
  refs/heads/5.4-js-rewrite dd4ea5b52 -> 5d4c89709


Finish the job of exposing the application message catalog to the client as a module


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/5d4c8970
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/5d4c8970
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/5d4c8970

Branch: refs/heads/5.4-js-rewrite
Commit: 5d4c89709ab3f8a44d82e1887e8c901e3d0d98ad
Parents: cb8d22e
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Wed Oct 24 15:42:59 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Wed Oct 24 15:42:59 2012 -0700

----------------------------------------------------------------------
 .../META-INF/modules/core/messages.coffee          |   34 +++++++++
 .../services/javascript/LocaleEmitterFilter.java   |   53 +++++++++++++++
 .../services/javascript/JavaScriptModule.java      |   11 +++-
 tapestry-core/src/test/app1/WEB-INF/app.properties |    2 +
 .../integration/app1/pages/test-messages.coffee    |   10 +++
 .../integration/app1/pages/JavaScriptTests.java    |    5 +-
 6 files changed, 113 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/5d4c8970/tapestry-core/src/main/coffeescript/META-INF/modules/core/messages.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/messages.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/messages.coffee
new file mode 100644
index 0000000..a8df8fc
--- /dev/null
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/messages.coffee
@@ -0,0 +1,34 @@
+# Copyright 2012 The Apache Software Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http:#www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# For all of these modules, we've turned off CoffeeScript's normal outer function
+# wrapper, as each module is just a call to `define()` with a function that fulfills
+# the same purpose. This one is different, as it is necessary to compute one of the dependencies.
+# On the server `core/messages/<locale>` is actually generated dynamically, as is a simple
+# mapping of message keys to message values, from the global application message catalog.
+do ->
+  # In the unexpected case that the data-locale attribute is missing, assume English
+  locale = (document.documentElement.getAttribute "data-locale") or "en"
+
+  define ["core/messages/#{locale}"],
+    (messages) ->
+
+      # Returns the application message catalog message for the given key. Returns
+      # a placeholder if the key is not found.
+      get = (key) ->
+        return messages[key] || "[[Missing Key: '#{key}']]"
+
+      # Export get as the main function; perhaps later we'll add a "format"
+      # or something similar as a property of get.
+      return get

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/5d4c8970/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/LocaleEmitterFilter.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/LocaleEmitterFilter.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/LocaleEmitterFilter.java
new file mode 100644
index 0000000..eaefaec
--- /dev/null
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/LocaleEmitterFilter.java
@@ -0,0 +1,53 @@
+// Copyright 2012 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.internal.services.javascript;
+
+import org.apache.tapestry5.MarkupWriter;
+import org.apache.tapestry5.dom.Element;
+import org.apache.tapestry5.ioc.services.ThreadLocale;
+import org.apache.tapestry5.services.MarkupRenderer;
+import org.apache.tapestry5.services.MarkupRendererFilter;
+
+/**
+ * Responsible for the {@code data-locale} attribute written into the HTML element.
+ *
+ * @since 5.4
+ */
+public class LocaleEmitterFilter implements MarkupRendererFilter
+{
+    private final ThreadLocale threadLocale;
+
+    public LocaleEmitterFilter(ThreadLocale threadLocale)
+    {
+        this.threadLocale = threadLocale;
+    }
+
+    @Override
+    public void renderMarkup(MarkupWriter writer, MarkupRenderer renderer)
+    {
+        renderer.renderMarkup(writer);
+
+        // After that's done (i.e., pretty much all rendering), touch it up a little.
+
+        Element html = writer.getDocument().find("html");
+
+        // If it is an HTML document, with a root HTML node, put data-locale
+        // attribute inplace for the client.
+        if (html != null)
+        {
+            html.attributes("data-locale", threadLocale.getLocale().toString());
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/5d4c8970/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/JavaScriptModule.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/JavaScriptModule.java b/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/JavaScriptModule.java
index b735715..2017fd6 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/JavaScriptModule.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/javascript/JavaScriptModule.java
@@ -198,10 +198,19 @@ public class JavaScriptModule
     {
         for (Locale locale : localizationSetter.getSupportedLocales())
         {
-            MessageCatalogResource resource = new MessageCatalogResource(locale, messagesSource,  resourceChangeTracker, compactJSON);
+            MessageCatalogResource resource = new MessageCatalogResource(locale, messagesSource, resourceChangeTracker, compactJSON);
 
             configuration.add("core/messages/" + locale.toString(), new ShimModule(resource, null, null));
         }
     }
 
+    /**
+     * Contributes 'LocaleEmitter', which writes the {@code data-locale} attribute into the HTML tag.
+     */
+    @Contribute(MarkupRenderer.class)
+    public static void renderLocaleAttributeIntoPages(OrderedConfiguration<MarkupRendererFilter> configuration)
+    {
+        configuration.addInstance("LocaleEmitter", LocaleEmitterFilter.class);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/5d4c8970/tapestry-core/src/test/app1/WEB-INF/app.properties
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/app1/WEB-INF/app.properties b/tapestry-core/src/test/app1/WEB-INF/app.properties
index 17192b6..f60825b 100644
--- a/tapestry-core/src/test/app1/WEB-INF/app.properties
+++ b/tapestry-core/src/test/app1/WEB-INF/app.properties
@@ -16,3 +16,5 @@ app-catalog-status=Application Catalog Working
 
 viewlink-label=View
 overridden-by-app=[app]
+
+client-accessible=Client Accessible

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/5d4c8970/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-messages.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-messages.coffee b/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-messages.coffee
new file mode 100644
index 0000000..c41949f
--- /dev/null
+++ b/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-messages.coffee
@@ -0,0 +1,10 @@
+require ["core/messages"], (messages) ->
+
+  module "core/messages"
+
+  test "access known key", ->
+    equal messages("client-accessible"), "Client Accessible"
+
+  test "unknown messages key", ->
+
+    equal messages("gnip-gnop"), "[[Missing Key: 'gnip-gnop']]"

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/5d4c8970/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.java
index 9c98348..64326c1 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.java
@@ -3,7 +3,10 @@ package org.apache.tapestry5.integration.app1.pages;
 import org.apache.tapestry5.annotations.Import;
 
 @Import(library = {
-        "context:qunit/qunit-1.9.0.js", "test-pubsub.js", "test-spi.js"}, stylesheet = "context:qunit/qunit-1.9.0.css")
+        "context:qunit/qunit-1.9.0.js",
+        "test-pubsub.js",
+        "test-spi.js",
+        "test-messages.js"}, stylesheet = "context:qunit/qunit-1.9.0.css")
 public class JavaScriptTests
 {
 }