merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / osl / unx / osxlocale.cxx
blob9a20fd9ceb12eb89177c42d6e71cee4428f5e39e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
31 #include <sal/types.h>
32 #include <assert.h>
34 #include <premac.h>
35 #include <CoreServices/CoreServices.h>
36 #include <CoreFoundation/CoreFoundation.h>
37 #include <postmac.h>
39 namespace /* private */
41 template <typename T>
42 class CFGuard
44 public:
45 explicit CFGuard(T& rT) : rT_(rT) {}
46 ~CFGuard() { if (rT_) CFRelease(rT_); }
47 private:
48 T& rT_;
51 typedef CFGuard<CFArrayRef> CFArrayGuard;
52 typedef CFGuard<CFStringRef> CFStringGuard;
53 typedef CFGuard<CFPropertyListRef> CFPropertyListGuard;
55 /** Get the current process locale from system
57 CFStringRef getProcessLocale()
59 CFPropertyListRef pref = CFPreferencesCopyAppValue(CFSTR("AppleLocale"), kCFPreferencesCurrentApplication);
60 CFPropertyListGuard proplGuard(pref);
62 if (pref == NULL) // return fallback value 'en_US'
63 return CFStringCreateWithCString(kCFAllocatorDefault, "en_US", kCFStringEncodingASCII);
65 CFStringRef sref = (CFGetTypeID(pref) == CFArrayGetTypeID()) ? (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)pref, 0) : (CFStringRef)pref;
67 // NOTE: this API is only available with Mac OS X >=10.3. We need to use it because
68 // Apple used non-ISO values on systems <10.2 like "German" for instance but didn't
69 // upgrade those values during upgrade to newer Mac OS X versions. See also #i54337#
70 return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref);
72 } // namespace private
74 /** Grab current locale from system.
76 extern "C" {
77 int macosx_getLocale(char *locale, sal_uInt32 bufferLen)
79 CFStringRef sref = getProcessLocale();
80 CFStringGuard sGuard(sref);
82 assert(sref != NULL && "osxlocale.cxx: getProcessLocale must return a non-NULL value");
84 // split the string into substrings; the first two (if there are two) substrings
85 // are language and country
86 CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("_"));
87 CFArrayGuard arrGuard(subs);
89 CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0);
90 CFStringGetCString(lang, locale, bufferLen, kCFStringEncodingASCII);
92 // country also available? Assumption: if the array contains more than one
93 // value the second value is always the country!
94 if (CFArrayGetCount(subs) > 1)
96 strlcat(locale, "_", bufferLen - strlen(locale));
98 CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1);
99 CFStringGetCString(country, locale + strlen(locale), bufferLen - strlen(locale), kCFStringEncodingASCII);
101 // Append 'UTF-8' to the locale because the Mac OS X file
102 // system interface is UTF-8 based and sal tries to determine
103 // the file system locale from the locale information
104 strlcat(locale, ".UTF-8", bufferLen - strlen(locale));
106 return noErr;
113 * macxp_OSXConvertCFEncodingToIANACharSetName
115 * Convert a CoreFoundation text encoding to an IANA charset name.
117 extern "C" int macxp_OSXConvertCFEncodingToIANACharSetName( char *buffer, unsigned int bufferLen, CFStringEncoding cfEncoding )
119 CFStringRef sCFEncodingName;
121 sCFEncodingName = CFStringConvertEncodingToIANACharSetName( cfEncoding );
122 CFStringGetCString( sCFEncodingName, buffer, bufferLen, cfEncoding );
124 if ( sCFEncodingName )
125 CFRelease( sCFEncodingName );
127 return( noErr );