< prev index next >

src/java.base/macosx/native/libjava/java_props_macosx.c

Print this page
rev 16780 : [mq]: zhhant

@@ -44,10 +44,12 @@
     return strdup(lc);
 }
 
 #define LOCALEIDLENGTH  128
 char *getMacOSXLocale(int cat) {
+    const char* retVal = NULL;
+
     switch (cat) {
     case LC_MESSAGES:
         {
             // get preferred language code
             CFArrayRef languages = CFLocaleCopyPreferredLanguages();

@@ -70,10 +72,39 @@
                 CFRelease(languages);
                 return NULL;
             }
             CFRelease(languages);
 
+            retVal = languageString;
+
+            // Special case for Portuguese in Brazil:
+            // The language code needs the "_BR" region code (to distinguish it
+            // from Portuguese in Portugal), but this is missing when using the
+            // "Portuguese (Brazil)" language.
+            // If language is "pt" and the current locale is pt_BR, return pt_BR.
+            char localeString[LOCALEIDLENGTH];
+            if (strcmp(retVal, "pt") == 0 &&
+                    CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
+                                       localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding()) &&
+                    strcmp(localeString, "pt_BR") == 0) {
+                retVal = localeString;
+            }
+        }
+        break;
+    default:
+        {
+            char localeString[LOCALEIDLENGTH];
+            if (!CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
+                                   localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding())) {
+                return NULL;
+            }
+            retVal = localeString;
+        }
+        break;
+    }
+
+    if (retVal != NULL) {
             // Language IDs use the language designators and (optional) region
             // and script designators of BCP 47.  So possible formats are:
             //
             // "en"         (language designator only)
             // "haw"        (3-letter lanuage designator)

@@ -84,11 +115,11 @@
             // "zh-Hans-419" (language with ISO 15924 script designator and UN M.49)
             //
             // In the case of region designators (alpha-2 and/or UN M.49), we convert
             // to our locale string format by changing '-' to '_'.  That is, if
             // the '-' is followed by fewer than 4 chars.
-            char* scriptOrRegion = strchr(languageString, '-');
+        char* scriptOrRegion = strchr(retVal, '-');
             if (scriptOrRegion != NULL) {
                 int length = strlen(scriptOrRegion);
                 if (length > 5) {
                     // Region and script both exist. Honor the script for now
                     scriptOrRegion[5] = '\0';

@@ -104,38 +135,13 @@
                           isdigit(scriptOrRegion[1]) &&
                           isdigit(scriptOrRegion[2]) &&
                           isdigit(scriptOrRegion[3])));
                 }
             }
-            const char* retVal = languageString;
 
-            // Special case for Portuguese in Brazil:
-            // The language code needs the "_BR" region code (to distinguish it
-            // from Portuguese in Portugal), but this is missing when using the
-            // "Portuguese (Brazil)" language.
-            // If language is "pt" and the current locale is pt_BR, return pt_BR.
-            char localeString[LOCALEIDLENGTH];
-            if (strcmp(retVal, "pt") == 0 &&
-                    CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
-                                       localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding()) &&
-                    strcmp(localeString, "pt_BR") == 0) {
-                retVal = localeString;
-            }
             return strdup(retVal);
         }
-        break;
-    default:
-        {
-            char localeString[LOCALEIDLENGTH];
-            if (CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
-                                   localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding())) {
-                return strdup(localeString);
-            }
-        }
-        break;
-    }
-
     return NULL;
 }
 
 char *setupMacOSXLocale(int cat) {
     char * ret = getMacOSXLocale(cat);
< prev index next >