OSPreferences_gtk.cpp (2581B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include <locale.h> 8 #include "mozilla/LookAndFeel.h" 9 #include "mozilla/intl/Locale.h" 10 #include "OSPreferences.h" 11 12 #include "nsServiceManagerUtils.h" 13 14 using namespace mozilla; 15 using namespace mozilla::intl; 16 17 OSPreferences::OSPreferences() = default; 18 19 bool OSPreferences::ReadSystemLocales(nsTArray<nsCString>& aLocaleList) { 20 MOZ_ASSERT(aLocaleList.IsEmpty()); 21 22 nsAutoCString defaultLang(Locale::GetDefaultLocale()); 23 24 if (CanonicalizeLanguageTag(defaultLang)) { 25 aLocaleList.AppendElement(defaultLang); 26 return true; 27 } 28 return false; 29 } 30 31 bool OSPreferences::ReadRegionalPrefsLocales(nsTArray<nsCString>& aLocaleList) { 32 MOZ_ASSERT(aLocaleList.IsEmpty()); 33 34 // For now we're just taking the LC_TIME from POSIX environment for all 35 // regional preferences. 36 nsAutoCString localeStr(setlocale(LC_TIME, nullptr)); 37 38 if (CanonicalizeLanguageTag(localeStr)) { 39 aLocaleList.AppendElement(localeStr); 40 return true; 41 } 42 43 return false; 44 } 45 46 /** 47 * Since Gtk does not provide a way to customize or format date/time patterns, 48 * we're reusing ICU data here, but we do modify it according to the only 49 * setting Gtk gives us - hourCycle. 50 * 51 * This means that for gtk we will return a pattern from ICU altered to 52 * represent h12/h24 hour cycle if the user modified the default value. 53 * 54 * In short, this should work like this: 55 * 56 * * gtk defaults, pl: 24h 57 * * gtk defaults, en: 12h 58 * 59 * * gtk 12h, pl: 12h 60 * * gtk 12h, en: 12h 61 * 62 * * gtk 24h, pl: 24h 63 * * gtk 12h, en: 12h 64 */ 65 bool OSPreferences::ReadDateTimePattern(DateTimeFormatStyle aDateStyle, 66 DateTimeFormatStyle aTimeStyle, 67 const nsACString& aLocale, 68 nsACString& aRetVal) { 69 nsAutoCString skeleton; 70 if (!GetDateTimeSkeletonForStyle(aDateStyle, aTimeStyle, aLocale, skeleton)) { 71 return false; 72 } 73 74 // Customize the skeleton if necessary to reflect user's 12/24hr pref 75 int hourCycle = LookAndFeel::GetInt(LookAndFeel::IntID::HourCycle); 76 if (hourCycle == 12 || hourCycle == 24) { 77 OverrideSkeletonHourCycle(hourCycle == 24, skeleton); 78 } 79 80 if (!GetPatternForSkeleton(skeleton, aLocale, aRetVal)) { 81 return false; 82 } 83 84 return true; 85 } 86 87 void OSPreferences::RemoveObservers() {}