You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2018/11/22 08:29:57 UTC

[myfaces-tobago] 03/03: TOBAGO-1956: Cleaning localization in the demo

This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git

commit 45ae1ca16b763795cb3270c5b57d8412aa9dfd28
Author: Udo Schnurpfeil <lo...@apache.org>
AuthorDate: Thu Nov 22 09:29:12 2018 +0100

    TOBAGO-1956: Cleaning localization in the demo
    
    * info page to show current translations
---
 .../tobago/example/demo/BundleController.java      | 60 ++++++++++++++++++++++
 .../myfaces/tobago/example/demo/BundleEntry.java   | 30 +++++++++++
 .../myfaces/tobago/example/demo/LocaleChanged.java |  4 ++
 .../tobago/example/demo/LocaleController.java      | 11 +++-
 .../content/30-concept/14-locale/Locale.xhtml      | 41 ++++++++++++++-
 5 files changed, 144 insertions(+), 2 deletions(-)

diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/BundleController.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/BundleController.java
new file mode 100644
index 0000000..2412202
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/BundleController.java
@@ -0,0 +1,60 @@
+package org.apache.myfaces.tobago.example.demo;
+
+import javax.enterprise.context.SessionScoped;
+import javax.enterprise.event.Observes;
+import javax.faces.application.Application;
+import javax.faces.context.FacesContext;
+import javax.inject.Named;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Locale;
+import java.util.ResourceBundle;
+
+import static org.apache.myfaces.tobago.util.ResourceUtils.TOBAGO_RESOURCE_BUNDLE;
+
+@Named
+@SessionScoped
+public class BundleController implements Serializable {
+
+  private List<BundleEntry> resources = new ArrayList<>();
+  private List<BundleEntry> messages = new ArrayList<>();
+
+  public void clear(@Observes LocaleChanged event) {
+    resources.clear();
+    messages.clear();
+  }
+
+  public List<BundleEntry> getResources() {
+    if (resources.size() == 0) {
+      final FacesContext facesContext = FacesContext.getCurrentInstance();
+      final ResourceBundle bundle =
+          facesContext.getApplication().getResourceBundle(facesContext, TOBAGO_RESOURCE_BUNDLE);
+      final Enumeration<String> keys = bundle.getKeys();
+      while (keys.hasMoreElements()) {
+        final String key = keys.nextElement();
+        resources.add(new BundleEntry(key, bundle.getString(key)));
+      }
+    }
+    return resources;
+  }
+
+  public List<BundleEntry> getMessages() {
+    if (messages.size() == 0) {
+      final FacesContext facesContext = FacesContext.getCurrentInstance();
+      final Application application = facesContext.getApplication();
+      final String bundleName = application.getMessageBundle();
+      final Locale locale = facesContext.getViewRoot() != null
+          ? facesContext.getViewRoot().getLocale() : application.getDefaultLocale();
+      ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);
+      final Enumeration<String> keys = bundle.getKeys();
+      while (keys.hasMoreElements()) {
+        final String key = keys.nextElement();
+        messages.add(new BundleEntry(key, bundle.getString(key)));
+      }
+    }
+    return messages;
+  }
+
+}
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/BundleEntry.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/BundleEntry.java
new file mode 100644
index 0000000..c146b80
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/BundleEntry.java
@@ -0,0 +1,30 @@
+package org.apache.myfaces.tobago.example.demo;
+
+import java.io.Serializable;
+
+public class BundleEntry implements Serializable {
+
+  private String key;
+  private String value;
+
+  public BundleEntry(String key, String value) {
+    this.key = key;
+    this.value = value;
+  }
+
+  public String getKey() {
+    return key;
+  }
+
+  public void setKey(String key) {
+    this.key = key;
+  }
+
+  public String getValue() {
+    return value;
+  }
+
+  public void setValue(String value) {
+    this.value = value;
+  }
+}
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/LocaleChanged.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/LocaleChanged.java
new file mode 100644
index 0000000..c38a3f2
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/LocaleChanged.java
@@ -0,0 +1,4 @@
+package org.apache.myfaces.tobago.example.demo;
+
+public class LocaleChanged {
+}
diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/LocaleController.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/LocaleController.java
index 34d5ca0..bf3aeb5 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/LocaleController.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/LocaleController.java
@@ -19,16 +19,19 @@
 
 package org.apache.myfaces.tobago.example.demo;
 
+import org.apache.myfaces.tobago.internal.util.ObjectUtils;
 import org.apache.myfaces.tobago.internal.util.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.faces.application.Application;
 import javax.enterprise.context.SessionScoped;
+import javax.enterprise.event.Event;
+import javax.faces.application.Application;
 import javax.faces.application.FacesMessage;
 import javax.faces.component.UIViewRoot;
 import javax.faces.context.FacesContext;
 import javax.faces.model.SelectItem;
+import javax.inject.Inject;
 import javax.inject.Named;
 import javax.servlet.ServletContext;
 import java.io.Serializable;
@@ -47,6 +50,9 @@ public class LocaleController implements Serializable {
 
   private Locale locale;
 
+  @Inject
+  private Event<LocaleChanged> events;
+
   public LocaleController() {
 
     final FacesContext facesContext = FacesContext.getCurrentInstance();
@@ -149,6 +155,9 @@ public class LocaleController implements Serializable {
   }
 
   public void setLocale(final Locale locale) {
+    if (!ObjectUtils.equals(this.locale , locale)) {
+      events.fire(new LocaleChanged());
+    }
     this.locale = locale;
   }
 
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/14-locale/Locale.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/14-locale/Locale.xhtml
index 6ffcb56..cae2b9c 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/14-locale/Locale.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/14-locale/Locale.xhtml
@@ -18,9 +18,10 @@
 -->
 
 <ui:composition template="/main.xhtml"
+                xmlns="http://www.w3.org/1999/xhtml"
                 xmlns:tc="http://myfaces.apache.org/tobago/component"
                 xmlns:ui="http://java.sun.com/jsf/facelets"
-                xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
+                xmlns:f="http://java.sun.com/jsf/core">
 
   Tobago supports internationalization and localization.
   <ul>
@@ -54,4 +55,42 @@
     The image url will be evaluated by the Java Bean  <code>LocaleController</code>.
   </p>
 
+  <tc:tabGroup>
+    <tc:tab label="Tobago Resource Bundle">
+      <p>
+      Tobago comes with a resource bundle
+      <code>org.apache.myfaces.tobago.context.TobagoResourceBundle</code> mapped to
+      <code>tobagoResourceBundle</code>.
+      This bundle contails translated text fragments used by Tobago e. g.
+      for label of components.
+      It's easy to expand this bundle to support more languages.
+      </p>
+      <p>
+        The next table shows the keys and the translation with the locale of the current session.
+        The locale can be changed with the selector above.
+      </p>
+      <tc:sheet var="entry" value="#{bundleController.resources}" markup="small">
+        <tc:style maxHeight="500px"/>
+        <tc:column label="Key" sortable="true">
+          <tc:out value="#{entry.key}" labelLayout="skip"/>
+        </tc:column>
+        <tc:column label="Value" sortable="true">
+          <tc:out value="#{entry.value}" labelLayout="skip"/>
+        </tc:column>
+      </tc:sheet>
+    </tc:tab>
+    <tc:tab label="Tobago Message Bundle">
+      Tobago comes with a message bundle for Tobago specific (info/warn/error) messages.
+      <tc:sheet var="entry" value="#{bundleController.messages}" markup="small">
+        <tc:style maxHeight="500px"/>
+        <tc:column label="Key" sortable="true">
+          <tc:out value="#{entry.key}" labelLayout="skip"/>
+        </tc:column>
+        <tc:column label="Value" sortable="true">
+          <tc:out value="#{entry.value}" labelLayout="skip"/>
+        </tc:column>
+      </tc:sheet>
+    </tc:tab>
+  </tc:tabGroup>
+
 </ui:composition>