diff --git a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp index ec1d70b1d9a..2dffa4bff50 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp @@ -21,22 +21,7 @@ #include #include -using HourCycleList = Vector; - -template<> -struct AK::Formatter : Formatter { - ErrorOr format(FormatBuilder& builder, Locale::HourCycle hour_cycle) - { - return builder.put_u64(to_underlying(hour_cycle)); - } -}; - struct CLDR { - UniqueStorage unique_hour_cycle_lists; - - HashMap hour_cycles; - Vector hour_cycle_regions; - HashMap minimum_days; Vector minimum_days_regions; @@ -50,50 +35,6 @@ struct CLDR { Vector weekend_end_regions; }; -static ErrorOr parse_hour_cycles(ByteString core_path, CLDR& cldr) -{ - // https://unicode.org/reports/tr35/tr35-dates.html#Time_Data - LexicalPath time_data_path(move(core_path)); - time_data_path = time_data_path.append("supplemental"sv); - time_data_path = time_data_path.append("timeData.json"sv); - - auto time_data = TRY(read_json_file(time_data_path.string())); - auto const& supplemental_object = time_data.as_object().get_object("supplemental"sv).value(); - auto const& time_data_object = supplemental_object.get_object("timeData"sv).value(); - - auto parse_hour_cycle = [](StringView hour_cycle) -> Optional { - if (hour_cycle.is_one_of("h"sv, "hb"sv, "hB"sv)) - return Locale::HourCycle::H12; - if (hour_cycle.is_one_of("H"sv, "Hb"sv, "HB"sv)) - return Locale::HourCycle::H23; - if (hour_cycle == "K"sv) - return Locale::HourCycle::H11; - if (hour_cycle == "k"sv) - return Locale::HourCycle::H24; - return {}; - }; - - time_data_object.for_each_member([&](auto const& key, JsonValue const& value) { - auto allowed_hour_cycles_string = value.as_object().get_byte_string("_allowed"sv).value(); - auto allowed_hour_cycles = allowed_hour_cycles_string.split_view(' '); - - Vector hour_cycles; - - for (auto allowed_hour_cycle : allowed_hour_cycles) { - if (auto hour_cycle = parse_hour_cycle(allowed_hour_cycle); hour_cycle.has_value()) - hour_cycles.append(*hour_cycle); - } - - auto hour_cycles_index = cldr.unique_hour_cycle_lists.ensure(move(hour_cycles)); - cldr.hour_cycles.set(key, hour_cycles_index); - - if (!cldr.hour_cycle_regions.contains_slow(key)) - cldr.hour_cycle_regions.append(key); - }); - - return {}; -} - static ErrorOr parse_week_data(ByteString core_path, CLDR& cldr) { // https://unicode.org/reports/tr35/tr35-dates.html#Week_Data @@ -161,7 +102,6 @@ static ErrorOr parse_week_data(ByteString core_path, CLDR& cldr) static ErrorOr parse_all_locales(ByteString core_path, CLDR& cldr) { - TRY(parse_hour_cycles(core_path, cldr)); TRY(parse_week_data(core_path, cldr)); return {}; } @@ -191,7 +131,6 @@ static ErrorOr generate_unicode_locale_header(Core::InputBufferedFile& fil namespace Locale { )~~~"); - generate_enum(generator, format_identifier, "HourCycleRegion"sv, {}, cldr.hour_cycle_regions); generate_enum(generator, format_identifier, "MinimumDaysRegion"sv, {}, cldr.minimum_days_regions); generate_enum(generator, format_identifier, "FirstDayRegion"sv, {}, cldr.first_day_regions); generate_enum(generator, format_identifier, "WeekendStartRegion"sv, {}, cldr.weekend_start_regions); @@ -225,8 +164,6 @@ static ErrorOr generate_unicode_locale_implementation(Core::InputBufferedF namespace Locale { )~~~"); - cldr.unique_hour_cycle_lists.generate(generator, cldr.unique_hour_cycle_lists.type_that_fits(), "s_hour_cycle_lists"sv); - auto append_mapping = [&](auto const& keys, auto const& map, auto type, auto name, auto mapping_getter) { generator.set("type", type); generator.set("name", name); @@ -248,7 +185,6 @@ static constexpr Array<@type@, @size@> @name@ { {)~~~"); generator.append(" } };"); }; - append_mapping(cldr.hour_cycle_regions, cldr.hour_cycles, cldr.unique_hour_cycle_lists.type_that_fits(), "s_hour_cycles"sv, [](auto const& hour_cycles) { return hour_cycles; }); append_mapping(cldr.minimum_days_regions, cldr.minimum_days, "u8"sv, "s_minimum_days"sv, [](auto minimum_days) { return minimum_days; }); append_mapping(cldr.first_day_regions, cldr.first_day, "u8"sv, "s_first_day"sv, [](auto first_day) { return to_underlying(first_day); }); append_mapping(cldr.weekend_start_regions, cldr.weekend_start, "u8"sv, "s_weekend_start"sv, [](auto weekend_start) { return to_underlying(weekend_start); }); @@ -269,34 +205,11 @@ static constexpr Array<@type@, @size@> @name@ { {)~~~"); return {}; }; - TRY(append_from_string("HourCycleRegion"sv, "hour_cycle_region"sv, cldr.hour_cycle_regions)); TRY(append_from_string("MinimumDaysRegion"sv, "minimum_days_region"sv, cldr.minimum_days_regions)); TRY(append_from_string("FirstDayRegion"sv, "first_day_region"sv, cldr.first_day_regions)); TRY(append_from_string("WeekendStartRegion"sv, "weekend_start_region"sv, cldr.weekend_start_regions)); TRY(append_from_string("WeekendEndRegion"sv, "weekend_end_region"sv, cldr.weekend_end_regions)); - generator.append(R"~~~( -Vector get_regional_hour_cycles(StringView region) -{ - auto region_value = hour_cycle_region_from_string(region); - if (!region_value.has_value()) - return {}; - - auto region_index = to_underlying(*region_value); - - auto regional_hour_cycles_index = s_hour_cycles.at(region_index); - auto const& regional_hour_cycles = s_hour_cycle_lists.at(regional_hour_cycles_index); - - Vector hour_cycles; - hour_cycles.ensure_capacity(regional_hour_cycles.size()); - - for (auto hour_cycle : regional_hour_cycles) - hour_cycles.unchecked_append(static_cast(hour_cycle)); - - return hour_cycles; -} -)~~~"); - auto append_regional_lookup = [&](StringView return_type, StringView lookup_type) { generator.set("return_type", return_type); generator.set("lookup_type", lookup_type); diff --git a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateLocaleData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateLocaleData.cpp index 6e27af0ded8..58bf0110db4 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateLocaleData.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateLocaleData.cpp @@ -578,11 +578,9 @@ Optional get_preferred_keyword_value_for_locale(StringView locale, S // FIXME: Calendar keywords are also region-based, and will need to be handled here when we support non-Gregorian calendars: // https://github.com/unicode-org/cldr-json/blob/main/cldr-json/cldr-core/supplemental/calendarPreferenceData.json if (key == "hc"sv) { - auto hour_cycles = get_locale_hour_cycles(locale); - if (hour_cycles.is_empty()) - return OptionalNone {}; - - return Optional { hour_cycle_to_string(hour_cycles[0]) }; + if (auto hour_cycle = default_hour_cycle(locale); hour_cycle.has_value()) + return hour_cycle_to_string(*hour_cycle); + return {}; } // FIXME: Generate locale-preferred collation data when available in the CLDR. @@ -607,15 +605,9 @@ Vector get_keywords_for_locale(StringView locale, StringView key) // FIXME: Calendar keywords are also region-based, and will need to be handled here when we support non-Gregorian calendars: // https://github.com/unicode-org/cldr-json/blob/main/cldr-json/cldr-core/supplemental/calendarPreferenceData.json if (key == "hc"sv) { - auto hour_cycles = get_locale_hour_cycles(locale); - - Vector values; - values.ensure_capacity(hour_cycles.size()); - - for (auto hour_cycle : hour_cycles) - values.unchecked_append(hour_cycle_to_string(hour_cycle)); - - return values; + if (auto hour_cycle = default_hour_cycle(locale); hour_cycle.has_value()) + return { hour_cycle_to_string(*hour_cycle) }; + return {}; } // FIXME: Generate locale-preferred collation data when available in the CLDR. diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp index 8cfa01ad780..93e393b030f 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp @@ -189,7 +189,7 @@ ThrowCompletionOr> create_date_time_format(VM& vm, // c. If hc is null, set hc to dataLocaleData.[[hourCycle]]. if (!hour_cycle_value.has_value()) - hour_cycle_value = ::Locale::get_default_regional_hour_cycle(data_locale); + hour_cycle_value = ::Locale::default_hour_cycle(data_locale); } // 28. Set dateTimeFormat.[[HourCycle]] to hc. diff --git a/Userland/Libraries/LibLocale/DateTimeFormat.cpp b/Userland/Libraries/LibLocale/DateTimeFormat.cpp index 3fdf6b8986c..597860aa531 100644 --- a/Userland/Libraries/LibLocale/DateTimeFormat.cpp +++ b/Userland/Libraries/LibLocale/DateTimeFormat.cpp @@ -98,6 +98,31 @@ StringView hour_cycle_to_string(HourCycle hour_cycle) VERIFY_NOT_REACHED(); } +Optional default_hour_cycle(StringView locale) +{ + UErrorCode status = U_ZERO_ERROR; + + auto locale_data = LocaleData::for_locale(locale); + if (!locale_data.has_value()) + return {}; + + auto hour_cycle = locale_data->date_time_pattern_generator().getDefaultHourCycle(status); + if (icu_failure(status)) + return {}; + + switch (hour_cycle) { + case UDAT_HOUR_CYCLE_11: + return HourCycle::H11; + case UDAT_HOUR_CYCLE_12: + return HourCycle::H12; + case UDAT_HOUR_CYCLE_23: + return HourCycle::H23; + case UDAT_HOUR_CYCLE_24: + return HourCycle::H24; + } + VERIFY_NOT_REACHED(); +} + static constexpr char icu_hour_cycle(Optional const& hour_cycle, Optional const& hour12) { if (hour12.has_value()) @@ -472,9 +497,6 @@ CalendarPattern CalendarPattern::create_from_pattern(StringView pattern) return format; } -Optional __attribute__((weak)) hour_cycle_region_from_string(StringView) { return {}; } -Vector __attribute__((weak)) get_regional_hour_cycles(StringView) { return {}; } - template static T find_regional_values_for_locale(StringView locale, GetRegionalValues&& get_regional_values) { @@ -508,19 +530,6 @@ static T find_regional_values_for_locale(StringView locale, GetRegionalValues&& return return_default_values(); } -// https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table -Vector get_locale_hour_cycles(StringView locale) -{ - return find_regional_values_for_locale>(locale, get_regional_hour_cycles); -} - -Optional get_default_regional_hour_cycle(StringView locale) -{ - if (auto hour_cycles = get_locale_hour_cycles(locale); !hour_cycles.is_empty()) - return hour_cycles.first(); - return {}; -} - Optional __attribute__((weak)) minimum_days_region_from_string(StringView) { return {}; } Optional __attribute__((weak)) get_regional_minimum_days(StringView) { return {}; } diff --git a/Userland/Libraries/LibLocale/DateTimeFormat.h b/Userland/Libraries/LibLocale/DateTimeFormat.h index d39cbc2efd9..9c980b8c5b2 100644 --- a/Userland/Libraries/LibLocale/DateTimeFormat.h +++ b/Userland/Libraries/LibLocale/DateTimeFormat.h @@ -43,6 +43,7 @@ enum class HourCycle : u8 { }; HourCycle hour_cycle_from_string(StringView hour_cycle); StringView hour_cycle_to_string(HourCycle hour_cycle); +Optional default_hour_cycle(StringView locale); enum class CalendarPatternStyle : u8 { Narrow, @@ -96,11 +97,6 @@ struct CalendarPattern { Optional time_zone_name; }; -Optional hour_cycle_region_from_string(StringView hour_cycle_region); -Vector get_regional_hour_cycles(StringView region); -Vector get_locale_hour_cycles(StringView locale); -Optional get_default_regional_hour_cycle(StringView locale); - Optional minimum_days_region_from_string(StringView minimum_days_region); Optional get_regional_minimum_days(StringView region); Optional get_locale_minimum_days(StringView locale); diff --git a/Userland/Libraries/LibLocale/Forward.h b/Userland/Libraries/LibLocale/Forward.h index 54cba296fcd..48eeb9d8887 100644 --- a/Userland/Libraries/LibLocale/Forward.h +++ b/Userland/Libraries/LibLocale/Forward.h @@ -13,7 +13,6 @@ namespace Locale { enum class CalendarPatternStyle : u8; enum class FirstDayRegion : u8; enum class HourCycle : u8; -enum class HourCycleRegion : u16; enum class Key : u8; enum class KeywordCalendar : u8; enum class KeywordCollation : u8;