< prev index next >

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

Print this page
rev 16780 : [mq]: zhhant


  29 #include <objc/objc-runtime.h>
  30 
  31 #include <Security/AuthSession.h>
  32 #include <CoreFoundation/CoreFoundation.h>
  33 #include <SystemConfiguration/SystemConfiguration.h>
  34 #include <Foundation/Foundation.h>
  35 
  36 #include "java_props_macosx.h"
  37 
  38 char *getPosixLocale(int cat) {
  39     char *lc = setlocale(cat, NULL);
  40     if ((lc == NULL) || (strcmp(lc, "C") == 0)) {
  41         lc = getenv("LANG");
  42     }
  43     if (lc == NULL) return NULL;
  44     return strdup(lc);
  45 }
  46 
  47 #define LOCALEIDLENGTH  128
  48 char *getMacOSXLocale(int cat) {


  49     switch (cat) {
  50     case LC_MESSAGES:
  51         {
  52             // get preferred language code
  53             CFArrayRef languages = CFLocaleCopyPreferredLanguages();
  54             if (languages == NULL) {
  55                 return NULL;
  56             }
  57             if (CFArrayGetCount(languages) <= 0) {
  58                 CFRelease(languages);
  59                 return NULL;
  60             }
  61 
  62             CFStringRef primaryLanguage = (CFStringRef)CFArrayGetValueAtIndex(languages, 0);
  63             if (primaryLanguage == NULL) {
  64                 CFRelease(languages);
  65                 return NULL;
  66             }
  67             char languageString[LOCALEIDLENGTH];
  68             if (CFStringGetCString(primaryLanguage, languageString,
  69                                    LOCALEIDLENGTH, CFStringGetSystemEncoding()) == false) {
  70                 CFRelease(languages);
  71                 return NULL;
  72             }
  73             CFRelease(languages);
  74 





























  75             // Language IDs use the language designators and (optional) region
  76             // and script designators of BCP 47.  So possible formats are:
  77             //
  78             // "en"         (language designator only)
  79             // "haw"        (3-letter lanuage designator)
  80             // "en-GB"      (language with alpha-2 region designator)
  81             // "es-419"     (language with 3-digit UN M.49 area code)
  82             // "zh-Hans"    (language with ISO 15924 script designator)
  83             // "zh-Hans-US"  (language with ISO 15924 script designator and region)
  84             // "zh-Hans-419" (language with ISO 15924 script designator and UN M.49)
  85             //
  86             // In the case of region designators (alpha-2 and/or UN M.49), we convert
  87             // to our locale string format by changing '-' to '_'.  That is, if
  88             // the '-' is followed by fewer than 4 chars.
  89             char* scriptOrRegion = strchr(languageString, '-');
  90             if (scriptOrRegion != NULL) {
  91                 int length = strlen(scriptOrRegion);
  92                 if (length > 5) {
  93                     // Region and script both exist. Honor the script for now
  94                     scriptOrRegion[5] = '\0';
  95                 } else if (length < 5) {
  96                     *scriptOrRegion = '_';
  97 
  98                     assert((length == 3 &&
  99                         // '-' followed by a 2 character region designator
 100                           isalpha(scriptOrRegion[1]) &&
 101                           isalpha(scriptOrRegion[2])) ||
 102                            (length == 4 &&
 103                         // '-' followed by a 3-digit UN M.49 area code
 104                           isdigit(scriptOrRegion[1]) &&
 105                           isdigit(scriptOrRegion[2]) &&
 106                           isdigit(scriptOrRegion[3])));
 107                 }
 108             }
 109             const char* retVal = languageString;
 110 
 111             // Special case for Portuguese in Brazil:
 112             // The language code needs the "_BR" region code (to distinguish it
 113             // from Portuguese in Portugal), but this is missing when using the
 114             // "Portuguese (Brazil)" language.
 115             // If language is "pt" and the current locale is pt_BR, return pt_BR.
 116             char localeString[LOCALEIDLENGTH];
 117             if (strcmp(retVal, "pt") == 0 &&
 118                     CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
 119                                        localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding()) &&
 120                     strcmp(localeString, "pt_BR") == 0) {
 121                 retVal = localeString;
 122             }
 123             return strdup(retVal);
 124         }
 125         break;
 126     default:
 127         {
 128             char localeString[LOCALEIDLENGTH];
 129             if (CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
 130                                    localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding())) {
 131                 return strdup(localeString);
 132             }
 133         }
 134         break;
 135     }
 136 
 137     return NULL;
 138 }
 139 
 140 char *setupMacOSXLocale(int cat) {
 141     char * ret = getMacOSXLocale(cat);
 142 
 143     if (ret == NULL) {
 144         return getPosixLocale(cat);
 145     } else {
 146         return ret;
 147     }
 148 }
 149 
 150 int isInAquaSession() {
 151     // environment variable to bypass the aqua session check
 152     char *ev = getenv("AWT_FORCE_HEADFUL");
 153     if (ev && (strncasecmp(ev, "true", 4) == 0)) {
 154         // if "true" then tell the caller we're in an Aqua session without actually checking
 155         return 1;
 156     }




  29 #include <objc/objc-runtime.h>
  30 
  31 #include <Security/AuthSession.h>
  32 #include <CoreFoundation/CoreFoundation.h>
  33 #include <SystemConfiguration/SystemConfiguration.h>
  34 #include <Foundation/Foundation.h>
  35 
  36 #include "java_props_macosx.h"
  37 
  38 char *getPosixLocale(int cat) {
  39     char *lc = setlocale(cat, NULL);
  40     if ((lc == NULL) || (strcmp(lc, "C") == 0)) {
  41         lc = getenv("LANG");
  42     }
  43     if (lc == NULL) return NULL;
  44     return strdup(lc);
  45 }
  46 
  47 #define LOCALEIDLENGTH  128
  48 char *getMacOSXLocale(int cat) {
  49     const char* retVal = NULL;
  50 
  51     switch (cat) {
  52     case LC_MESSAGES:
  53         {
  54             // get preferred language code
  55             CFArrayRef languages = CFLocaleCopyPreferredLanguages();
  56             if (languages == NULL) {
  57                 return NULL;
  58             }
  59             if (CFArrayGetCount(languages) <= 0) {
  60                 CFRelease(languages);
  61                 return NULL;
  62             }
  63 
  64             CFStringRef primaryLanguage = (CFStringRef)CFArrayGetValueAtIndex(languages, 0);
  65             if (primaryLanguage == NULL) {
  66                 CFRelease(languages);
  67                 return NULL;
  68             }
  69             char languageString[LOCALEIDLENGTH];
  70             if (CFStringGetCString(primaryLanguage, languageString,
  71                                    LOCALEIDLENGTH, CFStringGetSystemEncoding()) == false) {
  72                 CFRelease(languages);
  73                 return NULL;
  74             }
  75             CFRelease(languages);
  76 
  77             retVal = languageString;
  78 
  79             // Special case for Portuguese in Brazil:
  80             // The language code needs the "_BR" region code (to distinguish it
  81             // from Portuguese in Portugal), but this is missing when using the
  82             // "Portuguese (Brazil)" language.
  83             // If language is "pt" and the current locale is pt_BR, return pt_BR.
  84             char localeString[LOCALEIDLENGTH];
  85             if (strcmp(retVal, "pt") == 0 &&
  86                     CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
  87                                        localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding()) &&
  88                     strcmp(localeString, "pt_BR") == 0) {
  89                 retVal = localeString;
  90             }
  91         }
  92         break;
  93     default:
  94         {
  95             char localeString[LOCALEIDLENGTH];
  96             if (!CFStringGetCString(CFLocaleGetIdentifier(CFLocaleCopyCurrent()),
  97                                    localeString, LOCALEIDLENGTH, CFStringGetSystemEncoding())) {
  98                 return NULL;
  99             }
 100             retVal = localeString;
 101         }
 102         break;
 103     }
 104 
 105     if (retVal != NULL) {
 106         // Language IDs use the language designators and (optional) region
 107         // and script designators of BCP 47.  So possible formats are:
 108         //
 109         // "en"         (language designator only)
 110         // "haw"        (3-letter lanuage designator)
 111         // "en-GB"      (language with alpha-2 region designator)
 112         // "es-419"     (language with 3-digit UN M.49 area code)
 113         // "zh-Hans"    (language with ISO 15924 script designator)
 114         // "zh-Hans-US"  (language with ISO 15924 script designator and region)
 115         // "zh-Hans-419" (language with ISO 15924 script designator and UN M.49)
 116         //
 117         // In the case of region designators (alpha-2 and/or UN M.49), we convert
 118         // to our locale string format by changing '-' to '_'.  That is, if
 119         // the '-' is followed by fewer than 4 chars.
 120         char* scriptOrRegion = strchr(retVal, '-');
 121         if (scriptOrRegion != NULL) {
 122             int length = strlen(scriptOrRegion);
 123             if (length > 5) {
 124                 // Region and script both exist. Honor the script for now
 125                 scriptOrRegion[5] = '\0';
 126             } else if (length < 5) {
 127                 *scriptOrRegion = '_';
 128 
 129                 assert((length == 3 &&
 130                     // '-' followed by a 2 character region designator
 131                       isalpha(scriptOrRegion[1]) &&
 132                       isalpha(scriptOrRegion[2])) ||
 133                        (length == 4 &&
 134                     // '-' followed by a 3-digit UN M.49 area code
 135                       isdigit(scriptOrRegion[1]) &&
 136                       isdigit(scriptOrRegion[2]) &&
 137                       isdigit(scriptOrRegion[3])));
 138             }
 139         }

 140 












 141         return strdup(retVal);
 142     }












 143     return NULL;
 144 }
 145 
 146 char *setupMacOSXLocale(int cat) {
 147     char * ret = getMacOSXLocale(cat);
 148 
 149     if (ret == NULL) {
 150         return getPosixLocale(cat);
 151     } else {
 152         return ret;
 153     }
 154 }
 155 
 156 int isInAquaSession() {
 157     // environment variable to bypass the aqua session check
 158     char *ev = getenv("AWT_FORCE_HEADFUL");
 159     if (ev && (strncasecmp(ev, "true", 4) == 0)) {
 160         // if "true" then tell the caller we're in an Aqua session without actually checking
 161         return 1;
 162     }


< prev index next >