[Yanel-commits] rev 27970 - in public/yanel/trunk/src: core/java/org/wyona/yanel/core core/java/org/wyona/yanel/core/map impl/java/org/wyona/yanel/impl

josias at wyona.com josias at wyona.com
Wed Oct 10 15:38:16 CEST 2007


Author: josias
Date: 2007-10-10 15:38:15 +0200 (Wed, 10 Oct 2007)
New Revision: 27970

Added:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/LanguageHandler.java
   public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/DefaultLanguageHandler.java
   public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/StaticLanguageHandler.java
Modified:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/Resource.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/map/Realm.java
Log:
allow realm-specific language determination. fixes bug #5582

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/LanguageHandler.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/LanguageHandler.java	                        (rev 0)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/LanguageHandler.java	2007-10-10 13:38:15 UTC (rev 27970)
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2007 Wyona
+ *
+ *  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.wyona.org/licenses/APACHE-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.wyona.yanel.core;
+
+/**
+ * A LanguageHandler determines the language for the localization of a resource.
+ * It may consider e.g. request parameters, http headers, cookies, etc.
+ */
+public interface LanguageHandler {
+
+    /**
+     * Gets the localization language for a resource.
+     */
+    public String getLocalizationLanguage(Resource resource) throws Exception;
+
+}


Property changes on: public/yanel/trunk/src/core/java/org/wyona/yanel/core/LanguageHandler.java
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/Resource.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/Resource.java	2007-10-10 08:43:06 UTC (rev 27969)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/Resource.java	2007-10-10 13:38:15 UTC (rev 27970)
@@ -22,12 +22,10 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.wyona.yanel.core.api.attributes.TranslatableV1;
+import org.apache.log4j.Category;
+import org.wyona.yanel.core.attributes.translatable.TranslationManager;
 import org.wyona.yanel.core.map.Realm;
-import org.wyona.yanel.core.util.ResourceAttributeHelper;
 
-import org.apache.log4j.Category;
-
 /**
  *
  */
@@ -47,7 +45,7 @@
     private Environment environment;
     protected Map parameters;
 
-    static private String DEFAULT_LANGUAGE = "en";
+    public static final String DEFAULT_LANGUAGE = "en";
 
     /**
      *
@@ -256,9 +254,13 @@
      */
     public String getContentLanguage() throws Exception {
         String language = null;
-        if (ResourceAttributeHelper.hasAttributeImplemented(this, "Translatable", "1")) {
-            language = ((TranslatableV1)this).getLanguage(); 
+        TranslationManager translationManager = getRealm().getTranslationManager();
+        if (translationManager != null) {
+            language = translationManager.getLanguage(this);
         }
+        //if (ResourceAttributeHelper.hasAttributeImplemented(this, "Translatable", "1")) {
+        //    language = ((TranslatableV1)this).getLanguage(); 
+        //}
         if (language != null) return language;
 
         language = getResourceConfigProperty("language");
@@ -266,44 +268,25 @@
 
         // TODO: Shouldn't we move the check of the realm translation manager into the generic resource? (also see XMLResource and translatable interface)
 
-        return getRequestedLanguage();
+        language = getRealm().getDefaultLanguage();
+        if (language != null) return language;
+        
+        return DEFAULT_LANGUAGE;
+
+        //return getRequestedLanguage();
     }
 
     /**
      * Get language (localization) with the following priorization: <br><br>
      * 1) yanel.meta.language query string parameter<br> 
+     * 1.5) Realm-specific cookie
      * 2) Accept-Language header<br>
      * 3) Realm default language<br>
      * 4) Default "en"<br>
      */
     public String getRequestedLanguage() throws Exception {
-        // TODO: Make this reusable. Also see org/wyona/yanel/servlet/YanelServlet.java
-
-        // TODO: Use user profile setting resp. session (allow switching the locale)
-	    
-        // (1)
-        String language = getRequest().getParameter("yanel.meta.language");
-        if (language != null) return language;
+        return getRealm().getLanguageHandler().getLocalizationLanguage(this);
         
-        // (2)
-        language = getRequest().getHeader("Accept-Language");
-        if (language != null) {
-            if (language.indexOf(",") > 0) {
-                language = language.substring(0, language.indexOf(","));
-            }
-            int dashIndex = language.indexOf("-");
-            if (dashIndex > 0) {
-                language = language.substring(0, dashIndex);
-            }
-        }
-        if (language != null) return language;
-
-        // (3)
-        language = getRealm().getDefaultLanguage();
-        if (language != null) return language;
-        
-        // (4)
-        return DEFAULT_LANGUAGE;
     }
 
     /**

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/map/Realm.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/map/Realm.java	2007-10-10 08:43:06 UTC (rev 27969)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/map/Realm.java	2007-10-10 13:38:15 UTC (rev 27970)
@@ -27,6 +27,7 @@
 import org.wyona.security.core.PolicyManagerFactory;
 import org.wyona.security.core.api.IdentityManager;
 import org.wyona.security.core.api.PolicyManager;
+import org.wyona.yanel.core.LanguageHandler;
 import org.wyona.yanel.core.Yanel;
 import org.wyona.yanel.core.attributes.translatable.DefaultTranslationManager;
 import org.wyona.yanel.core.attributes.translatable.TranslationManager;
@@ -56,6 +57,7 @@
     private PolicyManager policyManager;
     private IdentityManager identityManager;
     private TranslationManager translationManager;
+    private LanguageHandler languageHandler;
     private File configFile;
     private File rootDir;
     private String[] languages;
@@ -166,6 +168,17 @@
         translationManager.init(this);
         setTranslationManager(translationManager);
         
+        configElement = config.getChild("language-handler", false);
+        LanguageHandler languageHandler = null;
+        if (configElement != null) {
+            String className = configElement.getAttribute("class");
+            languageHandler = (LanguageHandler)Class.forName(className).newInstance();
+        } else {
+            languageHandler = (LanguageHandler)Class.forName("org.wyona.yanel.impl.DefaultLanguageHandler").newInstance();
+        }
+        setLanguageHandler(languageHandler);
+        
+        
         Configuration rootDirConfig = config.getChild("root-dir", false);
         if (rootDirConfig != null) {
             setRootDir(FileUtil.resolve(getConfigFile(), new File(rootDirConfig.getValue())));
@@ -389,4 +402,12 @@
             return null;
         }
     }
+
+    public LanguageHandler getLanguageHandler() {
+        return languageHandler;
+    }
+
+    public void setLanguageHandler(LanguageHandler languageHandler) {
+        this.languageHandler = languageHandler;
+    }
 }

Added: public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/DefaultLanguageHandler.java
===================================================================
--- public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/DefaultLanguageHandler.java	                        (rev 0)
+++ public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/DefaultLanguageHandler.java	2007-10-10 13:38:15 UTC (rev 27970)
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2007 Wyona
+ *
+ *  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.wyona.org/licenses/APACHE-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.wyona.yanel.impl;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.wyona.yanel.core.LanguageHandler;
+import org.wyona.yanel.core.Resource;
+
+/**
+ *
+ */
+public class DefaultLanguageHandler implements LanguageHandler {
+
+    /**
+     * Get language (localization) with the following priorization: <br><br>
+     * 1) yanel.meta.language query string parameter<br> 
+     * 2) Accept-Language header<br>
+     * 3) Realm default language<br>
+     * 4) Default "en"<br>
+     */
+    public String getLocalizationLanguage(Resource resource) throws Exception {
+        HttpServletRequest request = resource.getEnvironment().getRequest();
+        
+        // TODO: Make this reusable. Also see org/wyona/yanel/servlet/YanelServlet.java
+
+        // TODO: Use user profile setting resp. session (allow switching the locale)
+        
+        // (1)
+        String language = request.getParameter("yanel.meta.language");
+        if (language != null) return language;
+        
+        /*String cookieName = "yanel." + getRealm().getID() + ".language";
+        Cookie[] cookies = getRequest().getCookies();
+        for (int i = 0 ; i < cookies.length; i++) {
+            if (cookies[i].getName().equals(cookieName)) {
+                language = cookies[i].getValue();
+                log.debug("language-cookie is: " + language);
+            }
+        }
+        if (language != null) return language;
+        */
+        
+        // (2)
+        language = request.getHeader("Accept-Language");
+        if (language != null) {
+            if (language.indexOf(",") > 0) {
+                language = language.substring(0, language.indexOf(","));
+            }
+            int dashIndex = language.indexOf("-");
+            if (dashIndex > 0) {
+                language = language.substring(0, dashIndex);
+            }
+            
+            // only accept language if it is supported by the realm:
+            /*List realmLanguages = Arrays.asList(getRealm().getLanguages());
+            if (!realmLanguages.contains(language)) {
+                language = getRealm().getDefaultLanguage();
+            }*/
+            return language;
+        }
+
+        // (3)
+        language = resource.getRealm().getDefaultLanguage();
+        if (language != null) return language;
+        
+        // (4)
+        return Resource.DEFAULT_LANGUAGE;
+    }
+
+}


Property changes on: public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/DefaultLanguageHandler.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/StaticLanguageHandler.java
===================================================================
--- public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/StaticLanguageHandler.java	                        (rev 0)
+++ public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/StaticLanguageHandler.java	2007-10-10 13:38:15 UTC (rev 27970)
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2007 Wyona
+ *
+ *  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.wyona.org/licenses/APACHE-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.wyona.yanel.impl;
+
+import org.wyona.yanel.core.LanguageHandler;
+import org.wyona.yanel.core.Resource;
+
+/**
+ *
+ */
+public class StaticLanguageHandler implements LanguageHandler {
+
+    /**
+     * Get language (localization) from the static content language: <br><br>
+     */
+    public String getLocalizationLanguage(Resource resource) throws Exception {
+        return resource.getContentLanguage();
+    }
+
+}


Property changes on: public/yanel/trunk/src/impl/java/org/wyona/yanel/impl/StaticLanguageHandler.java
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Yanel-commits mailing list