nemahaja commented on code in PR #17:
URL: 
https://github.com/apache/sling-org-apache-sling-i18n/pull/17#discussion_r1808330000


##########
src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java:
##########
@@ -646,43 +675,95 @@ private void preloadBundles() {
      * languages and countries provided by the platform. Any unsupported
      * language or country is replaced by the platform default language and
      * country.
+     * Locale string is also parsed for script tag. Any unsupported script is 
ignored.
      * @param localeString the locale as string
      * @return the {@link Locale} being generated from the {@code localeString}
      */
     static Locale toLocale(String localeString) {
         if (localeString == null || localeString.length() == 0) {
             return Locale.getDefault();
         }
+
         // support BCP 47 compliant strings as well (using a different 
separator "-" instead of "_")
         localeString = localeString.replaceAll("-", "_");
-
         // check language and country
         final String[] parts = localeString.split("_");
         if (parts.length == 0) {
             return Locale.getDefault();
         }
 
         // at least language is available
-        String lang = parts[0];
-        boolean isValidLanguageCode = false;
-        String[] langs = Locale.getISOLanguages();
-        for (int i = 0; i < langs.length; i++) {
-            if (langs[i].equalsIgnoreCase(lang)) {
-                isValidLanguageCode = true;
-                break;
+        String lang = getValidLanguage(parts[0]);
+        if (parts.length == 1) {
+            return new Locale(lang);
+        }
+
+        Locale localeWithBuilder = createLocaleWithBuilder(parts, lang);
+        if (localeWithBuilder != null) {
+            return localeWithBuilder;
+        }
+
+        return createLocaleWithConstructor(lang, parts);
+    }
+
+    private static Locale createLocaleWithBuilder(String[] parts, String lang) 
{
+        if (isScript(parts[1])) {
+            try {
+                switch (parts.length) {
+                    case 2:
+                        return new Locale.Builder()
+                                .setLanguage(lang)
+                                .setScript(parts[1])
+                                .build();
+                    case 3:
+                        return new Locale.Builder()
+                                .setLanguage(lang)
+                                .setScript(parts[1])
+                                .setRegion(getValidCountry(parts[2]))
+                                .build();
+                    default: // case >= 4
+                        return processMultipleParts(parts, lang);
+                }
+            } catch (IllformedLocaleException e) {
+                LoggerFactory.getLogger(JcrResourceBundleProvider.class)
+                        .warn("Failed to create locale with LocaleBuilder", e);

Review Comment:
   Thank you for the input. I have added parts in the warning message for 
debugging issues.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to