diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeLocale.cpp b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeLocale.cpp index b07456f284e..52f0226dfe9 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeLocale.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeLocale.cpp @@ -57,6 +57,7 @@ struct NumberSystem { Vector decimal_long_formats {}; Vector decimal_short_formats {}; Vector currency_short_formats {}; + NumberFormat percent_format {}; }; struct ListPatterns { @@ -558,6 +559,7 @@ static void parse_number_systems(String locale_numbers_path, UnicodeLocaleData& constexpr auto symbols_prefix = "symbols-numberSystem-"sv; constexpr auto decimal_formats_prefix = "decimalFormats-numberSystem-"sv; constexpr auto currency_formats_prefix = "currencyFormats-numberSystem-"sv; + constexpr auto percent_formats_prefix = "percentFormats-numberSystem-"sv; if (key.starts_with(symbols_prefix)) { auto system = key.substring(symbols_prefix.length()); @@ -587,6 +589,12 @@ static void parse_number_systems(String locale_numbers_path, UnicodeLocaleData& auto const& short_format = value.as_object().get("short"sv).as_object().get("standard"sv); number_system.currency_short_formats = parse_number_format(short_format.as_object()); } + } else if (key.starts_with(percent_formats_prefix)) { + auto system = key.substring(decimal_formats_prefix.length()); + auto& number_system = ensure_number_system(system); + + auto format_object = value.as_object().get("standard"sv); + number_system.percent_format.format_index = ensure_unique_string(locale_data, format_object.as_string()); } }); } @@ -813,6 +821,7 @@ Optional get_locale_key_mapping(StringView locale, StringView key); Optional key_from_string(StringView key); Optional get_number_system_symbol(StringView locale, StringView system, StringView numeric_symbol); +Optional get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type); Vector get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type); Optional numeric_symbol_from_string(StringView numeric_symbol); @@ -910,6 +919,7 @@ struct NumberSystem { Span decimal_long_formats {}; Span decimal_short_formats {}; Span currency_short_formats {}; + NumberFormat percent_format {}; }; )~~~"); @@ -970,6 +980,13 @@ static constexpr Array<@string_index_type@, @size@> @name@ { { )~~~"); }; + auto append_number_format = [&](auto const& number_format) { + generator.set("magnitude"sv, String::number(number_format.magnitude)); + generator.set("plurality"sv, String::number(static_cast(number_format.plurality))); + generator.set("format"sv, String::number(number_format.format_index)); + generator.append("{ @magnitude@, @plurality@, @format@ },"); + }; + auto append_number_formats = [&](String name, auto const& number_formats) { generator.set("name"sv, move(name)); generator.set("size"sv, String::number(number_formats.size())); @@ -985,10 +1002,7 @@ static constexpr Array @name@ { { if (values_in_current_row++ > 0) generator.append(" "); - generator.set("magnitude"sv, String::number(number_format.magnitude)); - generator.set("plurality"sv, String::number(static_cast(number_format.plurality))); - generator.set("format"sv, String::number(number_format.format_index)); - generator.append("{ @magnitude@, @plurality@, @format@ },"); + append_number_format(number_format); if (values_in_current_row == max_values_per_row) { values_in_current_row = 0; @@ -1032,7 +1046,9 @@ static constexpr Array @name@ { {)~~~"); generator.append(" @index@,"); } - generator.append(" }, @decimal_long_formats@.span(), @decimal_short_formats@.span(), @currency_short_formats@.span() },"); + generator.append(" }, @decimal_long_formats@.span(), @decimal_short_formats@.span(), @currency_short_formats@.span(), "); + append_number_format(number_system.value.percent_format); + generator.append(" },"); } generator.append(R"~~~( @@ -1400,6 +1416,18 @@ Optional get_number_system_symbol(StringView locale, StringView syst return {}; } +Optional get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type) +{ + if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) { + switch (type) { + case StandardNumberFormatType::Percent: + return number_system->percent_format.to_unicode_number_format(); + } + } + + return {}; +} + Vector get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type) { Vector formats; diff --git a/Userland/Libraries/LibUnicode/Forward.h b/Userland/Libraries/LibUnicode/Forward.h index 5f8a5b5a875..5015f324b15 100644 --- a/Userland/Libraries/LibUnicode/Forward.h +++ b/Userland/Libraries/LibUnicode/Forward.h @@ -19,6 +19,7 @@ enum class ListPatternType : u8; enum class Locale : u16; enum class Property : u8; enum class Script : u8; +enum class StandardNumberFormatType : u8; enum class Territory : u8; enum class WordBreakProperty : u8; diff --git a/Userland/Libraries/LibUnicode/Locale.cpp b/Userland/Libraries/LibUnicode/Locale.cpp index 6dbef122666..ab7c8416ba3 100644 --- a/Userland/Libraries/LibUnicode/Locale.cpp +++ b/Userland/Libraries/LibUnicode/Locale.cpp @@ -821,6 +821,15 @@ Vector get_compact_number_system_formats([[maybe_unused]] StringVi #endif } +Optional get_standard_number_system_format([[maybe_unused]] StringView locale, [[maybe_unused]] StringView system, [[maybe_unused]] StandardNumberFormatType type) +{ +#if ENABLE_UNICODE_DATA + return Detail::get_standard_number_system_format(locale, system, type); +#else + return {}; +#endif +} + Optional get_locale_list_patterns([[maybe_unused]] StringView locale, [[maybe_unused]] StringView type, [[maybe_unused]] StringView style) { #if ENABLE_UNICODE_DATA diff --git a/Userland/Libraries/LibUnicode/Locale.h b/Userland/Libraries/LibUnicode/Locale.h index 95a00047831..9694b0cb1da 100644 --- a/Userland/Libraries/LibUnicode/Locale.h +++ b/Userland/Libraries/LibUnicode/Locale.h @@ -78,6 +78,10 @@ struct LocaleID { Vector private_use_extensions {}; }; +enum class StandardNumberFormatType : u8 { + Percent, +}; + enum class CompactNumberFormatType : u8 { DecimalLong, DecimalShort, @@ -163,6 +167,7 @@ Optional get_locale_script_mapping(StringView locale, StringView scr Optional get_locale_currency_mapping(StringView locale, StringView currency); Vector get_locale_key_mapping(StringView locale, StringView keyword); Optional get_number_system_symbol(StringView locale, StringView system, StringView symbol); +Optional get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type); Vector get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type); Optional get_locale_list_patterns(StringView locale, StringView type, StringView style);