Line 0
Link Here
|
|
|
1 |
/************************************************************************* |
2 |
* |
3 |
* OpenOffice.org - a multi-platform office productivity suite |
4 |
* |
5 |
* $RCSfile: osxlocale.cxx,v $ |
6 |
* |
7 |
* $Revision: 1.4 $ |
8 |
* |
9 |
* last change: $Author: obo $ $Date: 2006/09/17 09:06:24 $ |
10 |
* |
11 |
* The Contents of this file are made available subject to |
12 |
* the terms of GNU Lesser General Public License Version 2.1. |
13 |
* |
14 |
* |
15 |
* GNU Lesser General Public License Version 2.1 |
16 |
* ============================================= |
17 |
* Copyright 2005 by Sun Microsystems, Inc. |
18 |
* 901 San Antonio Road, Palo Alto, CA 94303, USA |
19 |
* |
20 |
* This library is free software; you can redistribute it and/or |
21 |
* modify it under the terms of the GNU Lesser General Public |
22 |
* License version 2.1, as published by the Free Software Foundation. |
23 |
* |
24 |
* This library is distributed in the hope that it will be useful, |
25 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
26 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
27 |
* Lesser General Public License for more details. |
28 |
* |
29 |
* You should have received a copy of the GNU Lesser General Public |
30 |
* License along with this library; if not, write to the Free Software |
31 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
32 |
* MA 02111-1307 USA |
33 |
* |
34 |
************************************************************************/ |
35 |
|
36 |
// MARKER(update_precomp.py): autogen include statement, do not remove |
37 |
#include "precompiled_sal.hxx" |
38 |
|
39 |
#include <sal/types.h> |
40 |
#include <assert.h> |
41 |
|
42 |
#include <premac.h> |
43 |
#include <CoreServices/CoreServices.h> |
44 |
#include <CoreFoundation/CoreFoundation.h> |
45 |
#include <postmac.h> |
46 |
|
47 |
namespace /* private */ |
48 |
{ |
49 |
template <typename T> |
50 |
class CFGuard |
51 |
{ |
52 |
public: |
53 |
explicit CFGuard(T& rT) : rT_(rT) {} |
54 |
~CFGuard() { if (rT_) CFRelease(rT_); } |
55 |
private: |
56 |
T& rT_; |
57 |
}; |
58 |
|
59 |
typedef CFGuard<CFArrayRef> CFArrayGuard; |
60 |
typedef CFGuard<CFStringRef> CFStringGuard; |
61 |
typedef CFGuard<CFPropertyListRef> CFPropertyListGuard; |
62 |
|
63 |
/** Get the current process locale from system |
64 |
*/ |
65 |
CFStringRef getProcessLocale() |
66 |
{ |
67 |
CFPropertyListRef pref = CFPreferencesCopyAppValue(CFSTR("AppleLocale"), kCFPreferencesCurrentApplication); |
68 |
CFPropertyListGuard proplGuard(pref); |
69 |
|
70 |
if (pref == NULL) // return fallback value 'en_US' |
71 |
return CFStringCreateWithCString(kCFAllocatorDefault, "en_US", kCFStringEncodingASCII); |
72 |
|
73 |
CFStringRef sref = (CFGetTypeID(pref) == CFArrayGetTypeID()) ? (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)pref, 0) : (CFStringRef)pref; |
74 |
|
75 |
// NOTE: this API is only available with Mac OS X >=10.3. We need to use it because |
76 |
// Apple used non-ISO values on systems <10.2 like "German" for instance but didn't |
77 |
// upgrade those values during upgrade to newer Mac OS X versions. See also #i54337# |
78 |
return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref); |
79 |
} |
80 |
} // namespace private |
81 |
|
82 |
/** Grab current locale from system. |
83 |
*/ |
84 |
extern "C" int macosx_getLocale(char *locale, sal_uInt32 bufferLen) |
85 |
{ |
86 |
CFStringRef sref = getProcessLocale(); |
87 |
CFStringGuard sGuard(sref); |
88 |
|
89 |
assert(sref != NULL && "osxlocale.cxx: getProcessLocale must return a non-NULL value"); |
90 |
|
91 |
// split the string into substrings; the first two (if there are two) substrings |
92 |
// are language and country |
93 |
CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("_")); |
94 |
CFArrayGuard arrGuard(subs); |
95 |
|
96 |
CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0); |
97 |
CFStringGetCString(lang, locale, bufferLen, kCFStringEncodingASCII); |
98 |
|
99 |
// country also available? Assumption: if the array contains more than one |
100 |
// value the second value is always the country! |
101 |
if (CFArrayGetCount(subs) > 1) |
102 |
{ |
103 |
strlcat(locale, "_", bufferLen - strlen(locale)); |
104 |
|
105 |
CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1); |
106 |
CFStringGetCString(country, locale + strlen(locale), bufferLen - strlen(locale), kCFStringEncodingASCII); |
107 |
} |
108 |
// Append 'UTF-8' to the locale because the Mac OS X file |
109 |
// system interface is UTF-8 based and sal tries to determine |
110 |
// the file system locale from the locale information |
111 |
strlcat(locale, ".UTF-8", bufferLen - strlen(locale)); |
112 |
|
113 |
return noErr; |
114 |
} |
115 |
|
116 |
|
117 |
|
118 |
/* |
119 |
* macxp_OSXConvertCFEncodingToIANACharSetName |
120 |
* |
121 |
* Convert a CoreFoundation text encoding to an IANA charset name. |
122 |
*/ |
123 |
extern "C" int macxp_OSXConvertCFEncodingToIANACharSetName( char *buffer, unsigned int bufferLen, CFStringEncoding cfEncoding ) |
124 |
{ |
125 |
CFStringRef sCFEncodingName; |
126 |
|
127 |
sCFEncodingName = CFStringConvertEncodingToIANACharSetName( cfEncoding ); |
128 |
CFStringGetCString( sCFEncodingName, buffer, bufferLen, cfEncoding ); |
129 |
|
130 |
return( noErr ); |
131 |
} |
132 |
|