Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / sal / osl / unx / osxlocale.cxx
bloba64238ec67671029822dd205fa0f618e544ccb01
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
21 #include <assert.h>
23 #include <premac.h>
24 #ifndef IOS
25 #include <CoreServices/CoreServices.h>
26 #endif
27 #include <CoreFoundation/CoreFoundation.h>
28 #include <postmac.h>
30 #include <nlsupport.h>
32 namespace
34 template <typename T>
35 class CFGuard
37 public:
38 explicit CFGuard(T& rT) : rT_(rT) {}
39 ~CFGuard() { if (rT_) CFRelease(rT_); }
40 private:
41 T& rT_;
44 typedef CFGuard<CFArrayRef> CFArrayGuard;
45 typedef CFGuard<CFStringRef> CFStringGuard;
46 typedef CFGuard<CFPropertyListRef> CFPropertyListGuard;
48 /** Get the current process locale from system
50 CFStringRef getProcessLocale()
52 CFPropertyListRef pref = CFPreferencesCopyAppValue(CFSTR("AppleLanguages"), kCFPreferencesCurrentApplication);
53 CFPropertyListGuard proplGuard(pref);
55 if (pref == NULL) // return fallback value 'en_US'
56 return CFStringCreateWithCString(kCFAllocatorDefault, "en_US", kCFStringEncodingASCII);
58 CFStringRef sref = (CFGetTypeID(pref) == CFArrayGetTypeID()) ? (CFStringRef)CFArrayGetValueAtIndex((CFArrayRef)pref, 0) : (CFStringRef)pref;
60 return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref);
64 /** Grab current locale from system.
66 int macosx_getLocale(char *locale, sal_uInt32 bufferLen)
68 CFStringRef sref = getProcessLocale();
69 CFStringGuard sGuard(sref);
71 assert(sref != NULL && "osxlocale.cxx: getProcessLocale must return a non-NULL value");
73 // split the string into substrings; the first two (if there are two) substrings
74 // are language and country
75 CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("-"));
76 CFArrayGuard arrGuard(subs);
78 CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0);
79 CFStringGetCString(lang, locale, bufferLen, kCFStringEncodingASCII);
81 // country also available? Assumption: if the array contains more than one
82 // value the second value is always the country!
83 if (CFArrayGetCount(subs) > 1)
85 strlcat(locale, "_", bufferLen - strlen(locale));
87 CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1);
88 CFStringGetCString(country, locale + strlen(locale), bufferLen - strlen(locale), kCFStringEncodingASCII);
90 // Append 'UTF-8' to the locale because the Mac OS X file
91 // system interface is UTF-8 based and sal tries to determine
92 // the file system locale from the locale information
93 strlcat(locale, ".UTF-8", bufferLen - strlen(locale));
95 return noErr;
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */