Bump for 3.6-28
[LibreOffice.git] / sal / osl / unx / osxlocale.cxx
blob4b3f992d4e37960659d6c2a08a1b9dcc6db78816
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <sal/types.h>
31 #include <assert.h>
33 #include <premac.h>
34 #ifndef IOS
35 #include <CoreServices/CoreServices.h>
36 #endif
37 #include <CoreFoundation/CoreFoundation.h>
38 #include <postmac.h>
40 namespace /* private */
42 template <typename T>
43 class CFGuard
45 public:
46 explicit CFGuard(T& rT) : rT_(rT) {}
47 ~CFGuard() { if (rT_) CFRelease(rT_); }
48 private:
49 T& rT_;
52 typedef CFGuard<CFArrayRef> CFArrayGuard;
53 typedef CFGuard<CFStringRef> CFStringGuard;
54 typedef CFGuard<CFPropertyListRef> CFPropertyListGuard;
56 /** Get the current process locale from system
58 CFStringRef getProcessLocale()
60 CFPropertyListRef pref = CFPreferencesCopyAppValue(CFSTR("AppleLocale"), kCFPreferencesCurrentApplication);
61 CFPropertyListGuard proplGuard(pref);
63 if (pref == NULL) // return fallback value 'en_US'
64 return CFStringCreateWithCString(kCFAllocatorDefault, "en_US", kCFStringEncodingASCII);
66 CFStringRef sref = (CFGetTypeID(pref) == CFArrayGetTypeID()) ? (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)pref, 0) : (CFStringRef)pref;
68 // NOTE: this API is only available with Mac OS X >=10.3. We need to use it because
69 // Apple used non-ISO values on systems <10.2 like "German" for instance but didn't
70 // upgrade those values during upgrade to newer Mac OS X versions. See also #i54337#
71 return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref);
73 } // namespace private
75 /** Grab current locale from system.
77 extern "C" {
78 int macosx_getLocale(char *locale, sal_uInt32 bufferLen)
80 CFStringRef sref = getProcessLocale();
81 CFStringGuard sGuard(sref);
83 assert(sref != NULL && "osxlocale.cxx: getProcessLocale must return a non-NULL value");
85 // split the string into substrings; the first two (if there are two) substrings
86 // are language and country
87 CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("_"));
88 CFArrayGuard arrGuard(subs);
90 CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0);
91 CFStringGetCString(lang, locale, bufferLen, kCFStringEncodingASCII);
93 // country also available? Assumption: if the array contains more than one
94 // value the second value is always the country!
95 if (CFArrayGetCount(subs) > 1)
97 strlcat(locale, "_", bufferLen - strlen(locale));
99 CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1);
100 CFStringGetCString(country, locale + strlen(locale), bufferLen - strlen(locale), kCFStringEncodingASCII);
102 // Append 'UTF-8' to the locale because the Mac OS X file
103 // system interface is UTF-8 based and sal tries to determine
104 // the file system locale from the locale information
105 strlcat(locale, ".UTF-8", bufferLen - strlen(locale));
107 return noErr;
114 * macxp_OSXConvertCFEncodingToIANACharSetName
116 * Convert a CoreFoundation text encoding to an IANA charset name.
118 extern "C" int macxp_OSXConvertCFEncodingToIANACharSetName( char *buffer, unsigned int bufferLen, CFStringEncoding cfEncoding )
120 CFStringRef sCFEncodingName;
122 sCFEncodingName = CFStringConvertEncodingToIANACharSetName( cfEncoding );
123 CFStringGetCString( sCFEncodingName, buffer, bufferLen, cfEncoding );
125 if ( sCFEncodingName )
126 CFRelease( sCFEncodingName );
128 return( noErr );
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */