tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / sal / osl / unx / osxlocale.cxx
blobf82ea1436c71458596b9d2b434e390d6ca2be3b9
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 <rtl/ustrbuf.hxx>
32 #include "nlsupport.hxx"
34 namespace
36 template <typename T>
37 class CFGuard
39 public:
40 explicit CFGuard(T& rT) : rT_(rT) {}
41 ~CFGuard() { if (rT_) CFRelease(rT_); }
42 private:
43 T& rT_;
46 typedef CFGuard<CFArrayRef> CFArrayGuard;
47 typedef CFGuard<CFStringRef> CFStringGuard;
48 typedef CFGuard<CFPropertyListRef> CFPropertyListGuard;
50 /** Get the current process locale from system
52 CFStringRef getProcessLocale()
54 CFPropertyListRef pref = CFPreferencesCopyAppValue(CFSTR("AppleLanguages"), kCFPreferencesCurrentApplication);
55 CFPropertyListGuard proplGuard(pref);
57 if (pref == nullptr) // return fallback value 'en_US'
58 return CFStringCreateWithCString(kCFAllocatorDefault, "en_US", kCFStringEncodingASCII);
60 CFStringRef sref = (CFGetTypeID(pref) == CFArrayGetTypeID()) ? static_cast<CFStringRef>(CFArrayGetValueAtIndex(static_cast<CFArrayRef>(pref), 0)) : static_cast<CFStringRef>(pref);
62 return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref);
65 void append(OUStringBuffer & buffer, CFStringRef string) {
66 CFIndex n = CFStringGetLength(string);
67 CFStringGetCharacters(
68 string, CFRangeMake(0, n),
69 reinterpret_cast<UniChar *>(buffer.appendUninitialized(n)));
73 /** Grab current locale from system.
75 OUString macosx_getLocale()
77 CFStringRef sref = getProcessLocale();
78 CFStringGuard sGuard(sref);
80 assert(sref != nullptr && "osxlocale.cxx: getProcessLocale must return a non-NULL value");
82 // split the string into substrings; the first two (if there are two) substrings
83 // are language and country
84 CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(nullptr, sref, CFSTR("-"));
85 CFArrayGuard arrGuard(subs);
87 OUStringBuffer buf;
88 append(buf, static_cast<CFStringRef>(CFArrayGetValueAtIndex(subs, 0)));
90 // country also available? Assumption: if the array contains more than one
91 // value the second value is always the country!
92 if (CFArrayGetCount(subs) > 1)
94 buf.append("_");
95 append(buf, static_cast<CFStringRef>(CFArrayGetValueAtIndex(subs, 1)));
97 // Append 'UTF-8' to the locale because the macOS file
98 // system interface is UTF-8 based and sal tries to determine
99 // the file system locale from the locale information
100 buf.append(".UTF-8");
101 return buf.makeStringAndClear();
104 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */