| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
#include "mozilla/intl/DisplayNames.h" |
| 5 |
#include "ScopedICUObject.h" |
| 6 |
|
| 7 |
namespace mozilla::intl { |
| 8 |
|
| 9 |
DisplayNames::~DisplayNames() { |
| 10 |
// The mDisplayNames will not exist when the DisplayNames is being |
| 11 |
// moved. |
| 12 |
if (auto* uldn = mULocaleDisplayNames.GetMut()) { |
| 13 |
uldn_close(uldn); |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
DisplayNamesError DisplayNames::ToError(ICUError aError) const { |
| 18 |
switch (aError) { |
| 19 |
case ICUError::InternalError: |
| 20 |
case ICUError::OverflowError: |
| 21 |
return DisplayNamesError::InternalError; |
| 22 |
case ICUError::OutOfMemory: |
| 23 |
return DisplayNamesError::OutOfMemory; |
| 24 |
} |
| 25 |
MOZ_ASSERT_UNREACHABLE(); |
| 26 |
return DisplayNamesError::InternalError; |
| 27 |
} |
| 28 |
|
| 29 |
DisplayNamesError DisplayNames::ToError( |
| 30 |
Locale::CanonicalizationError aError) const { |
| 31 |
switch (aError) { |
| 32 |
case Locale::CanonicalizationError::DuplicateVariant: |
| 33 |
return DisplayNamesError::DuplicateVariantSubtag; |
| 34 |
case Locale::CanonicalizationError::InternalError: |
| 35 |
return DisplayNamesError::InternalError; |
| 36 |
case Locale::CanonicalizationError::OutOfMemory: |
| 37 |
return DisplayNamesError::OutOfMemory; |
| 38 |
} |
| 39 |
MOZ_ASSERT_UNREACHABLE(); |
| 40 |
return DisplayNamesError::InternalError; |
| 41 |
} |
| 42 |
|
| 43 |
/* static */ |
| 44 |
Result<UniquePtr<DisplayNames>, ICUError> DisplayNames::TryCreate( |
| 45 |
const char* aLocale, Options aOptions) { |
| 46 |
UErrorCode status = U_ZERO_ERROR; |
| 47 |
UDisplayContext contexts[] = { |
| 48 |
// Use either standard or dialect names. |
| 49 |
// For example either "English (GB)" or "British English". |
| 50 |
aOptions.languageDisplay == DisplayNames::LanguageDisplay::Standard |
| 51 |
? UDISPCTX_STANDARD_NAMES |
| 52 |
: UDISPCTX_DIALECT_NAMES, |
| 53 |
|
| 54 |
// Assume the display names are used in a stand-alone context. |
| 55 |
UDISPCTX_CAPITALIZATION_FOR_STANDALONE, |
| 56 |
|
| 57 |
// Select either the long or short form. There's no separate narrow form |
| 58 |
// available in ICU, therefore we equate "narrow"/"short" styles here. |
| 59 |
aOptions.style == DisplayNames::Style::Long ? UDISPCTX_LENGTH_FULL |
| 60 |
: UDISPCTX_LENGTH_SHORT, |
| 61 |
|
| 62 |
// Don't apply substitutes, because we need to apply our own fallbacks. |
| 63 |
UDISPCTX_NO_SUBSTITUTE, |
| 64 |
}; |
| 65 |
|
| 66 |
const char* locale = IcuLocale(aLocale); |
| 67 |
|
| 68 |
ULocaleDisplayNames* uLocaleDisplayNames = |
| 69 |
uldn_openForContext(locale, contexts, std::size(contexts), &status); |
| 70 |
|
| 71 |
if (U_FAILURE(status)) { |
| 72 |
return Err(ToICUError(status)); |
| 73 |
} |
| 74 |
return MakeUnique<DisplayNames>(uLocaleDisplayNames, MakeStringSpan(locale), |
| 75 |
aOptions); |
| 76 |
}; |
| 77 |
|
| 78 |
#ifdef DEBUG |
| 79 |
static bool IsStandaloneMonth(UDateFormatSymbolType symbolType) { |
| 80 |
switch (symbolType) { |
| 81 |
case UDAT_STANDALONE_MONTHS: |
| 82 |
case UDAT_STANDALONE_SHORT_MONTHS: |
| 83 |
case UDAT_STANDALONE_NARROW_MONTHS: |
| 84 |
return true; |
| 85 |
|
| 86 |
case UDAT_ERAS: |
| 87 |
case UDAT_MONTHS: |
| 88 |
case UDAT_SHORT_MONTHS: |
| 89 |
case UDAT_WEEKDAYS: |
| 90 |
case UDAT_SHORT_WEEKDAYS: |
| 91 |
case UDAT_AM_PMS: |
| 92 |
case UDAT_LOCALIZED_CHARS: |
| 93 |
case UDAT_ERA_NAMES: |
| 94 |
case UDAT_NARROW_MONTHS: |
| 95 |
case UDAT_NARROW_WEEKDAYS: |
| 96 |
case UDAT_STANDALONE_WEEKDAYS: |
| 97 |
case UDAT_STANDALONE_SHORT_WEEKDAYS: |
| 98 |
case UDAT_STANDALONE_NARROW_WEEKDAYS: |
| 99 |
case UDAT_QUARTERS: |
| 100 |
case UDAT_SHORT_QUARTERS: |
| 101 |
case UDAT_STANDALONE_QUARTERS: |
| 102 |
case UDAT_STANDALONE_SHORT_QUARTERS: |
| 103 |
case UDAT_SHORTER_WEEKDAYS: |
| 104 |
case UDAT_STANDALONE_SHORTER_WEEKDAYS: |
| 105 |
case UDAT_CYCLIC_YEARS_WIDE: |
| 106 |
case UDAT_CYCLIC_YEARS_ABBREVIATED: |
| 107 |
case UDAT_CYCLIC_YEARS_NARROW: |
| 108 |
case UDAT_ZODIAC_NAMES_WIDE: |
| 109 |
case UDAT_ZODIAC_NAMES_ABBREVIATED: |
| 110 |
case UDAT_ZODIAC_NAMES_NARROW: |
| 111 |
case UDAT_NARROW_QUARTERS: |
| 112 |
case UDAT_STANDALONE_NARROW_QUARTERS: |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
MOZ_ASSERT_UNREACHABLE("unenumerated, undocumented symbol type"); |
| 117 |
return false; |
| 118 |
} |
| 119 |
#endif |
| 120 |
|
| 121 |
Result<Ok, DisplayNamesError> DisplayNames::ComputeDateTimeDisplayNames( |
| 122 |
UDateFormatSymbolType symbolType, mozilla::Span<const int32_t> indices, |
| 123 |
Span<const char> aCalendar) { |
| 124 |
if (!mDateTimeDisplayNames.empty()) { |
| 125 |
// No need to re-compute the display names. |
| 126 |
return Ok(); |
| 127 |
} |
| 128 |
mozilla::intl::Locale tag; |
| 129 |
// Do not use mLocale.AsSpan() as it includes the null terminator inside the |
| 130 |
// span. |
| 131 |
if (LocaleParser::TryParse(Span(mLocale.Elements(), mLocale.Length() - 1), |
| 132 |
tag) |
| 133 |
.isErr()) { |
| 134 |
return Err(DisplayNamesError::InvalidLanguageTag); |
| 135 |
} |
| 136 |
|
| 137 |
if (!aCalendar.empty()) { |
| 138 |
// Add the calendar extension to the locale. This is only available via |
| 139 |
// the MozExtension. |
| 140 |
Vector<char, 32> extension; |
| 141 |
Span<const char> prefix = MakeStringSpan("u-ca-"); |
| 142 |
if (!extension.append(prefix.data(), prefix.size()) || |
| 143 |
!extension.append(aCalendar.data(), aCalendar.size())) { |
| 144 |
return Err(DisplayNamesError::OutOfMemory); |
| 145 |
} |
| 146 |
// This overwrites any other Unicode extensions, but should be okay to do |
| 147 |
// here. |
| 148 |
if (auto result = tag.SetUnicodeExtension(extension); result.isErr()) { |
| 149 |
return Err(ToError(result.unwrapErr())); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
constexpr char16_t* timeZone = nullptr; |
| 154 |
constexpr int32_t timeZoneLength = 0; |
| 155 |
|
| 156 |
constexpr char16_t* pattern = nullptr; |
| 157 |
constexpr int32_t patternLength = 0; |
| 158 |
|
| 159 |
Vector<char, DisplayNames::LocaleVecLength> localeWithCalendar; |
| 160 |
VectorToBufferAdaptor buffer(localeWithCalendar); |
| 161 |
if (auto result = tag.ToString(buffer); result.isErr()) { |
| 162 |
return Err(ToError(result.unwrapErr())); |
| 163 |
} |
| 164 |
if (!localeWithCalendar.append('\0')) { |
| 165 |
return Err(DisplayNamesError::OutOfMemory); |
| 166 |
} |
| 167 |
|
| 168 |
UErrorCode status = U_ZERO_ERROR; |
| 169 |
UDateFormat* fmt = udat_open( |
| 170 |
UDAT_DEFAULT, UDAT_DEFAULT, |
| 171 |
IcuLocale( |
| 172 |
// IcuLocale takes a Span that does not include the null terminator. |
| 173 |
Span(localeWithCalendar.begin(), localeWithCalendar.length() - 1)), |
| 174 |
timeZone, timeZoneLength, pattern, patternLength, &status); |
| 175 |
if (U_FAILURE(status)) { |
| 176 |
return Err(DisplayNamesError::InternalError); |
| 177 |
} |
| 178 |
ScopedICUObject<UDateFormat, udat_close> datToClose(fmt); |
| 179 |
|
| 180 |
Vector<char16_t, DisplayNames::LocaleVecLength> name; |
| 181 |
for (int32_t index : indices) { |
| 182 |
auto result = FillBufferWithICUCall(name, [&](UChar* target, int32_t length, |
| 183 |
UErrorCode* status) { |
| 184 |
return udat_getSymbols(fmt, symbolType, index, target, length, status); |
| 185 |
}); |
| 186 |
if (result.isErr()) { |
| 187 |
return Err(ToError(result.unwrapErr())); |
| 188 |
} |
| 189 |
|
| 190 |
// Everything except Undecimber should always have a non-empty name. |
| 191 |
MOZ_ASSERT_IF(!IsStandaloneMonth(symbolType) || index != UCAL_UNDECIMBER, |
| 192 |
!name.empty()); |
| 193 |
|
| 194 |
if (!mDateTimeDisplayNames.emplaceBack(Span(name.begin(), name.length()))) { |
| 195 |
return Err(DisplayNamesError::OutOfMemory); |
| 196 |
} |
| 197 |
} |
| 198 |
return Ok(); |
| 199 |
} |
| 200 |
|
| 201 |
Span<const char> DisplayNames::ToCodeString(Month aMonth) { |
| 202 |
switch (aMonth) { |
| 203 |
case Month::January: |
| 204 |
return MakeStringSpan("1"); |
| 205 |
case Month::February: |
| 206 |
return MakeStringSpan("2"); |
| 207 |
case Month::March: |
| 208 |
return MakeStringSpan("3"); |
| 209 |
case Month::April: |
| 210 |
return MakeStringSpan("4"); |
| 211 |
case Month::May: |
| 212 |
return MakeStringSpan("5"); |
| 213 |
case Month::June: |
| 214 |
return MakeStringSpan("6"); |
| 215 |
case Month::July: |
| 216 |
return MakeStringSpan("7"); |
| 217 |
case Month::August: |
| 218 |
return MakeStringSpan("8"); |
| 219 |
case Month::September: |
| 220 |
return MakeStringSpan("9"); |
| 221 |
case Month::October: |
| 222 |
return MakeStringSpan("10"); |
| 223 |
case Month::November: |
| 224 |
return MakeStringSpan("11"); |
| 225 |
case Month::December: |
| 226 |
return MakeStringSpan("12"); |
| 227 |
case Month::Undecimber: |
| 228 |
return MakeStringSpan("13"); |
| 229 |
} |
| 230 |
MOZ_ASSERT_UNREACHABLE(); |
| 231 |
return MakeStringSpan("1"); |
| 232 |
}; |
| 233 |
|
| 234 |
} // namespace mozilla::intl |