From 0790cac85a8286788bc81f0b294d63e87bad8a86 Mon Sep 17 00:00:00 2001 From: Dan Minor Date: Wed, 9 Dec 2020 16:50:14 +0000 Subject: [PATCH] Bug 1681357 - Use regional prefs locale if locale is not specified in GetDateTimePattern; r=zbraniecki Differential Revision: https://phabricator.services.mozilla.com/D99241 --- intl/locale/OSPreferences.cpp | 17 +++++++++++++---- intl/locale/tests/gtest/TestOSPreferences.cpp | 17 ++++++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/intl/locale/OSPreferences.cpp b/intl/locale/OSPreferences.cpp index 8f7a72a3c119..9262d7a07155 100644 --- a/intl/locale/OSPreferences.cpp +++ b/intl/locale/OSPreferences.cpp @@ -499,8 +499,17 @@ OSPreferences::GetDateTimePattern(int32_t aDateFormatStyle, return NS_OK; } + // If the locale is not specified, default to first regional prefs locale + const nsACString* locale = &aLocale; + AutoTArray rpLocales; + if (aLocale.IsEmpty()) { + LocaleService::GetInstance()->GetRegionalPrefsLocales(rpLocales); + MOZ_ASSERT(rpLocales.Length() > 0); + locale = &rpLocales[0]; + } + // Create a cache key from the locale + style options - nsAutoCString key(aLocale); + nsAutoCString key(*locale); key.Append(':'); key.AppendInt(aDateFormatStyle); key.Append(':'); @@ -512,9 +521,9 @@ OSPreferences::GetDateTimePattern(int32_t aDateFormatStyle, return NS_OK; } - if (!OverrideDateTimePattern(dateStyle, timeStyle, aLocale, pattern)) { - if (!ReadDateTimePattern(dateStyle, timeStyle, aLocale, pattern)) { - if (!GetDateTimePatternForStyle(dateStyle, timeStyle, aLocale, pattern)) { + if (!OverrideDateTimePattern(dateStyle, timeStyle, *locale, pattern)) { + if (!ReadDateTimePattern(dateStyle, timeStyle, *locale, pattern)) { + if (!GetDateTimePatternForStyle(dateStyle, timeStyle, *locale, pattern)) { return NS_ERROR_FAILURE; } } diff --git a/intl/locale/tests/gtest/TestOSPreferences.cpp b/intl/locale/tests/gtest/TestOSPreferences.cpp index 3decbe1997c8..605b301c098f 100644 --- a/intl/locale/tests/gtest/TestOSPreferences.cpp +++ b/intl/locale/tests/gtest/TestOSPreferences.cpp @@ -77,7 +77,22 @@ TEST(Intl_Locale_OSPreferences, GetDateTimePattern) } } - ASSERT_TRUE(1); + // If the locale is not specified, we should get the pattern corresponding to + // the first regional prefs locale. + AutoTArray rpLocales; + LocaleService::GetInstance()->GetRegionalPrefsLocales(rpLocales); + ASSERT_TRUE(rpLocales.Length() > 0); + + nsAutoCString rpLocalePattern; + ASSERT_TRUE(NS_SUCCEEDED( + osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleLong, + mozIOSPreferences::dateTimeFormatStyleLong, + rpLocales[0], rpLocalePattern))); + ASSERT_TRUE(NS_SUCCEEDED( + osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleLong, + mozIOSPreferences::dateTimeFormatStyleLong, + nsDependentCString(""), pattern))); + ASSERT_EQ(rpLocalePattern, pattern); } /**