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:47 UTC

[myfaces] branch 2.3-next updated: Fixing localized properties behaviour (2.3-next) (#368)

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

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


The following commit(s) were added to refs/heads/2.3-next by this push:
     new f2f4ab572 Fixing localized properties behaviour (2.3-next) (#368)
f2f4ab572 is described below

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

    Fixing localized properties behaviour (2.3-next) (#368)
    
    * [localized-composite] Fixing localized properties resource loading
    
    * [localized-composite-2.3-next] Fixing style
    
    * Relocated new methods
    * Fixed tabs and spaces
---
 .../java/javax/faces/component/UIComponent.java    | 51 ++++++++++++++++++++--
 1 file changed, 48 insertions(+), 3 deletions(-)

diff --git a/api/src/main/java/javax/faces/component/UIComponent.java b/api/src/main/java/javax/faces/component/UIComponent.java
index d7c0c849e..954f056a2 100755
--- a/api/src/main/java/javax/faces/component/UIComponent.java
+++ b/api/src/main/java/javax/faces/component/UIComponent.java
@@ -665,9 +665,8 @@ 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)
                     {
                         // If the resultant Resource exists and can be found, the InputStream for the resource
@@ -1334,6 +1333,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>
     {
         private ResourceBundle _bundle;