--- /so/env/O-Laufwerk/SRC680/src.m109/sal/systools/macxp_extras/x11osx/osxlocale.c Wed Feb 4 20:53:33 2004 +++ osxlocale.c.new Thu Jun 23 22:13:10 2005 @@ -1,3 +1,4 @@ + /************************************************************************* * * $RCSfile: osxlocale.c,v $ @@ -72,66 +73,38 @@ * Grab current locale from system. */ int macxp_getOSXLocale( char *locale, sal_uInt32 bufferLen ) -{ - LocaleRef lref; - CFArrayRef aref; - CFStringRef sref; - CFStringRef locNameRef; - - aref = (CFArrayRef)CFPreferencesCopyAppValue( CFSTR( "AppleLanguages" ), kCFPreferencesCurrentApplication ); - if ( aref != NULL ) +{ + CFStringRef sref = (CFStringRef)CFPreferencesCopyAppValue(CFSTR("AppleLocale"), kCFPreferencesCurrentApplication); + + if ((sref != NULL) && (CFGetTypeID(sref) == CFStringGetTypeID())) { - if ( (CFGetTypeID(aref) == CFArrayGetTypeID()) && (CFArrayGetCount(aref) > 0) ) + // split the string into substrings; the first two (if there are two) substrings + // are language and country + CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("_")); + + CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0); + CFStringGetCString(lang, locale, bufferLen, kCFStringEncodingASCII); + + // country also available? Assumption: if the array contains more than one + // value the second value is always the country! + if (CFArrayGetCount(subs) > 1) { - sref = (CFStringRef)CFArrayGetValueAtIndex( aref, 0 ); - if ( (sref != NULL) && (CFGetTypeID(sref) == CFStringGetTypeID()) ) - { -#if (BUILD_OS_MAJOR==10) && (BUILD_OS_MINOR==3) -// Panther code - // This function only exists in Panther and above - locNameRef = CFLocaleCreateCanonicalLocaleIdentifierFromString( kCFAllocatorDefault, sref ); - - if ( locNameRef != NULL ) - { - CFStringGetCString( locNameRef, locale, bufferLen, kCFStringEncodingASCII ); - CFRelease( locNameRef ); - - // If its just en, we want en_US. Since all the locales are also - // UTF-8, we'll append UTF-8 to the end of all returned locales - if ( strcmp(locale, "en") == 0 ) - strlcpy( locale, "en_US", bufferLen ); -// else if ( strchr(locale, '.') == NULL ) -// strlcat( locale, ".UTF-8", bufferLen ); - } - else - fprintf( stderr, "Could not get Canonical Locale Identifier from AppleLanguages value!\n" ); -#endif - -#if (BUILD_OS_MAJOR == 10) && (BUILD_OS_MINOR == 2) -// Jaguar code - if ( CFStringGetCString( sref, locale, bufferLen, CFStringGetSystemEncoding() ) ) - { - LocaleRefFromLocaleString( locale, &lref ); - LocaleRefGetPartString( lref, kLocaleAllPartsMask, bufferLen, locale ); - - /* Hack for US english locales. OS X returns only "en", but we want - * "en_US". So add it. - */ - if ( (strlen(locale) == 2) && (strncmp(locale, "en", 2) == 0) ) - strncat( locale, "_US", bufferLen - strlen(locale) - 1 ); - } -#endif - } - else - fprintf( stderr, "Could not get array index 0 value of CFPref AppleLanguages!\n" ); + strlcat(locale, "_", bufferLen - strlen(locale)); + + CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1); + CFStringGetCString(country, locale + strlen(locale), bufferLen - strlen(locale), kCFStringEncodingASCII); } + + strlcat(locale, ".UTF-8", bufferLen - strlen(locale)); + + CFRelease(subs); + } + else + fprintf( stderr, "Could not get Canonical Locale Identifier from AppleLanguages value!\n" ); - CFRelease( aref ); - } - else - fprintf( stderr, "Could not get value of CFPref AppleLanguages! Please reset your locale in the International control panel.\n" ); - - return( noErr ); + CFRelease(sref); + + return noErr; } @@ -149,3 +122,4 @@ return( noErr ); } +