You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by me...@apache.org on 2022/11/16 12:28:40 UTC

[myfaces] branch 2.3.x updated: Fixing localized composite behaviour (2.3.x) (#367)

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

melloware pushed a commit to branch 2.3.x
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/2.3.x by this push:
     new b371e54d9 Fixing localized composite behaviour (2.3.x) (#367)
b371e54d9 is described below

commit b371e54d91acee33192fc9181369f7c64025aa25
Author: Paulo Cristovão de Araújo Silva Filho <pc...@gmail.com>
AuthorDate: Wed Nov 16 09:28:34 2022 -0300

    Fixing localized composite behaviour (2.3.x) (#367)
    
    * [localized-composite] Fixing localized properties resource loading
    
    * [localized-composite-2.3.x] Fixing style
    
    * Relocated new code before the internal classes definitions
---
 .../java/javax/faces/component/UIComponent.java    | 50 +++++++++++++++++++++-
 1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/api/src/main/java/javax/faces/component/UIComponent.java b/api/src/main/java/javax/faces/component/UIComponent.java
index 234ea78de..21248dcda 100755
--- a/api/src/main/java/javax/faces/component/UIComponent.java
+++ b/api/src/main/java/javax/faces/component/UIComponent.java
@@ -679,8 +679,7 @@ public abstract class UIComponent
                     // Call ResourceHandler.createResource(java.lang.String,java.lang.String), passing the derived
                     // resourceName and
                     // libraryName.
-                    Resource bundleResource = context.getApplication().getResourceHandler()
-                            .createResource(resourceName, componentResource.getLibraryName());
+                    Resource bundleResource = getLocalizedCompositeResource(resourceName, componentResource.getLibraryName(), context);
 
                     if (bundleResource != null)
                     {
@@ -1374,6 +1373,52 @@ public abstract class UIComponent
         }
         return paramValue;
     }
+    
+    private Resource getLocalizedCompositeResource(String resourceName, String libraryName, FacesContext context)
+    {
+        List<String> localizedPaths = getLocalizedPropertiesPaths(resourceName, context);
+        Resource resource = null;
+        for (String localizedPath: localizedPaths)
+        {
+            resource = context.getApplication().getResourceHandler().createResource(localizedPath, libraryName);
+            if (resource != null)
+            {
+              break;
+            }
+        }
+        
+        return resource;
+    }
+    
+    private List<String> getLocalizedPropertiesPaths(String path, FacesContext ctx)
+    {
+        Locale loc = (ctx != null && ctx.getViewRoot() != null) ? ctx.getViewRoot().getLocale() : null;
+        if (!path.endsWith(".properties") || loc == null)
+        {
+            return Collections.singletonList(path);
+        }
+        
+        List<String> list = new ArrayList<>();
+        String base = path.substring(0, path.lastIndexOf(".properties"));
+        
+        if (!loc.getVariant().isEmpty())
+        {
+            list.add(String.format("%s_%s_%s_%s.properties", base, loc.getLanguage(), loc.getCountry(), loc.getVariant()));
+        }
+        
+        if (!loc.getCountry().isEmpty())
+        {
+            list.add(String.format("%s_%s_%s.properties", base, loc.getLanguage(), loc.getCountry()));
+        }
+        
+        if (!loc.getLanguage().isEmpty())
+        {
+          list.add(String.format("%s_%s.properties", base, loc.getLanguage()));
+        }
+        list.add(path);
+        
+        return list;
+    }
 
     private static class BundleMap implements Map<String, String>
     {
@@ -1831,4 +1876,5 @@ public abstract class UIComponent
             }
         }
     }
+    
 }