xref: /aoo41x/main/sal/osl/unx/osxlocale.cxx (revision 87d2adbc)
1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*87d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*87d2adbcSAndrew Rist  * distributed with this work for additional information
6*87d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*87d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*87d2adbcSAndrew Rist  *
11*87d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist  *
13*87d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist  * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*87d2adbcSAndrew Rist  * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist  * under the License.
19*87d2adbcSAndrew Rist  *
20*87d2adbcSAndrew Rist  *************************************************************/
21*87d2adbcSAndrew Rist 
22*87d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <sal/types.h>
28cdf0e10cSrcweir #include <assert.h>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <premac.h>
31cdf0e10cSrcweir #include <CoreServices/CoreServices.h>
32cdf0e10cSrcweir #include <CoreFoundation/CoreFoundation.h>
33cdf0e10cSrcweir #include <postmac.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace /* private */
36cdf0e10cSrcweir {
37cdf0e10cSrcweir 	template <typename T>
38cdf0e10cSrcweir 	class CFGuard
39cdf0e10cSrcweir 	{
40cdf0e10cSrcweir 	public:
CFGuard(T & rT)41cdf0e10cSrcweir 		explicit CFGuard(T& rT) : rT_(rT) {}
~CFGuard()42cdf0e10cSrcweir 		~CFGuard() { if (rT_) CFRelease(rT_); }
43cdf0e10cSrcweir 	private:
44cdf0e10cSrcweir 		T& rT_;
45cdf0e10cSrcweir 	};
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 	typedef CFGuard<CFArrayRef> CFArrayGuard;
48cdf0e10cSrcweir 	typedef CFGuard<CFStringRef> CFStringGuard;
49cdf0e10cSrcweir 	typedef CFGuard<CFPropertyListRef> CFPropertyListGuard;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	/** Get the current process locale from system
52cdf0e10cSrcweir 	*/
getProcessLocale()53cdf0e10cSrcweir 	CFStringRef getProcessLocale()
54cdf0e10cSrcweir 	{
55cdf0e10cSrcweir 		CFPropertyListRef pref = CFPreferencesCopyAppValue(CFSTR("AppleLocale"), kCFPreferencesCurrentApplication);
56cdf0e10cSrcweir 		CFPropertyListGuard proplGuard(pref);
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 		if (pref == NULL) // return fallback value 'en_US'
59cdf0e10cSrcweir 			 return CFStringCreateWithCString(kCFAllocatorDefault, "en_US", kCFStringEncodingASCII);
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 		CFStringRef sref = (CFGetTypeID(pref) == CFArrayGetTypeID()) ? (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)pref, 0) : (CFStringRef)pref;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 		// NOTE: this API is only available with Mac OS X >=10.3. We need to use it because
64cdf0e10cSrcweir 		// Apple used non-ISO values on systems <10.2 like "German" for instance but didn't
65cdf0e10cSrcweir 		// upgrade those values during upgrade to newer Mac OS X versions. See also #i54337#
66cdf0e10cSrcweir 		return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref);
67cdf0e10cSrcweir 	}
68cdf0e10cSrcweir } // namespace private
69cdf0e10cSrcweir 
70cdf0e10cSrcweir /** Grab current locale from system.
71cdf0e10cSrcweir */
72cdf0e10cSrcweir extern "C" {
macosx_getLocale(char * locale,sal_uInt32 bufferLen)73cdf0e10cSrcweir int macosx_getLocale(char *locale, sal_uInt32 bufferLen)
74cdf0e10cSrcweir {
75cdf0e10cSrcweir 	CFStringRef sref = getProcessLocale();
76cdf0e10cSrcweir 	CFStringGuard sGuard(sref);
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	assert(sref != NULL && "osxlocale.cxx: getProcessLocale must return a non-NULL value");
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	// split the string into substrings; the first two (if there are two) substrings
81cdf0e10cSrcweir 	// are language and country
82cdf0e10cSrcweir 	CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("_"));
83cdf0e10cSrcweir 	CFArrayGuard arrGuard(subs);
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0);
86cdf0e10cSrcweir 	CFStringGetCString(lang, locale, bufferLen, kCFStringEncodingASCII);
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	// country also available? Assumption: if the array contains more than one
89cdf0e10cSrcweir 	// value the second value is always the country!
90cdf0e10cSrcweir 	if (CFArrayGetCount(subs) > 1)
91cdf0e10cSrcweir 	{
92cdf0e10cSrcweir 		strlcat(locale, "_", bufferLen - strlen(locale));
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 		CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1);
95cdf0e10cSrcweir 		CFStringGetCString(country, locale + strlen(locale), bufferLen - strlen(locale), kCFStringEncodingASCII);
96cdf0e10cSrcweir 	}
97cdf0e10cSrcweir     // Append 'UTF-8' to the locale because the Mac OS X file
98cdf0e10cSrcweir     // system interface is UTF-8 based and sal tries to determine
99cdf0e10cSrcweir     // the file system locale from the locale information
100cdf0e10cSrcweir 	strlcat(locale, ".UTF-8", bufferLen - strlen(locale));
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	return noErr;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 
108cdf0e10cSrcweir /*
109cdf0e10cSrcweir  * macxp_OSXConvertCFEncodingToIANACharSetName
110cdf0e10cSrcweir  *
111cdf0e10cSrcweir  * Convert a CoreFoundation text encoding to an IANA charset name.
112cdf0e10cSrcweir  */
macxp_OSXConvertCFEncodingToIANACharSetName(char * buffer,unsigned int bufferLen,CFStringEncoding cfEncoding)113cdf0e10cSrcweir extern "C" int macxp_OSXConvertCFEncodingToIANACharSetName( char *buffer, unsigned int bufferLen, CFStringEncoding cfEncoding )
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 	CFStringRef	sCFEncodingName;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	sCFEncodingName = CFStringConvertEncodingToIANACharSetName( cfEncoding );
118cdf0e10cSrcweir 	CFStringGetCString( sCFEncodingName, buffer, bufferLen, cfEncoding );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 	if ( sCFEncodingName )
121cdf0e10cSrcweir 	    CFRelease( sCFEncodingName );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 	return( noErr );
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126