mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 18:10:56 +09:00
LibUnicode: Templatize our naive implementation of plurality selection
As we didn't (and still don't) have Intl.PluralRules when we implemented Intl.NumberFormat, we use a locale-unaware basic implementation to pick a pattern based on a number's value. Templatize this method for now to work other other format-like structures (will be used for relative-time formatting).
This commit is contained in:
parent
789f093b2e
commit
f8892fdea2
Notes:
sideshowbarker
2024-07-17 20:08:34 +09:00
Author: https://github.com/trflynn89
Commit: f8892fdea2
Pull-request: https://github.com/SerenityOS/serenity/pull/12157
Reviewed-by: https://github.com/linusg ✅
2 changed files with 29 additions and 29 deletions
|
@ -61,34 +61,6 @@ String replace_digits_for_number_system(StringView system, StringView number)
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<NumberFormat> select_pattern_with_plurality(Vector<NumberFormat> const& formats, double number)
|
|
||||||
{
|
|
||||||
// FIXME: This is a rather naive and locale-unaware implementation Unicode's TR-35 pluralization
|
|
||||||
// rules: https://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
|
|
||||||
// Once those rules are implemented for LibJS, we better use them instead.
|
|
||||||
auto find_plurality = [&](auto plurality) -> Optional<NumberFormat> {
|
|
||||||
if (auto it = formats.find_if([&](auto& patterns) { return patterns.plurality == plurality; }); it != formats.end())
|
|
||||||
return *it;
|
|
||||||
return {};
|
|
||||||
};
|
|
||||||
|
|
||||||
if (number == 0) {
|
|
||||||
if (auto patterns = find_plurality(NumberFormat::Plurality::Zero); patterns.has_value())
|
|
||||||
return patterns;
|
|
||||||
} else if (number == 1) {
|
|
||||||
if (auto patterns = find_plurality(NumberFormat::Plurality::One); patterns.has_value())
|
|
||||||
return patterns;
|
|
||||||
} else if (number == 2) {
|
|
||||||
if (auto patterns = find_plurality(NumberFormat::Plurality::Two); patterns.has_value())
|
|
||||||
return patterns;
|
|
||||||
} else if (number > 2) {
|
|
||||||
if (auto patterns = find_plurality(NumberFormat::Plurality::Many); patterns.has_value())
|
|
||||||
return patterns;
|
|
||||||
}
|
|
||||||
|
|
||||||
return find_plurality(NumberFormat::Plurality::Other);
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://www.unicode.org/reports/tr35/tr35-numbers.html#Currencies
|
// https://www.unicode.org/reports/tr35/tr35-numbers.html#Currencies
|
||||||
Optional<String> augment_currency_format_pattern([[maybe_unused]] StringView currency_display, [[maybe_unused]] StringView base_pattern)
|
Optional<String> augment_currency_format_pattern([[maybe_unused]] StringView currency_display, [[maybe_unused]] StringView base_pattern)
|
||||||
{
|
{
|
||||||
|
|
|
@ -79,7 +79,35 @@ Optional<NumberFormat> get_standard_number_system_format(StringView locale, Stri
|
||||||
Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type);
|
Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type);
|
||||||
Vector<NumberFormat> get_unit_formats(StringView locale, StringView unit, Style style);
|
Vector<NumberFormat> get_unit_formats(StringView locale, StringView unit, Style style);
|
||||||
|
|
||||||
Optional<NumberFormat> select_pattern_with_plurality(Vector<NumberFormat> const& formats, double number);
|
|
||||||
Optional<String> augment_currency_format_pattern(StringView currency_display, StringView base_pattern);
|
Optional<String> augment_currency_format_pattern(StringView currency_display, StringView base_pattern);
|
||||||
|
|
||||||
|
template<typename FormatType>
|
||||||
|
Optional<FormatType> select_pattern_with_plurality(Vector<FormatType> const& formats, double number)
|
||||||
|
{
|
||||||
|
// FIXME: This is a rather naive and locale-unaware implementation Unicode's TR-35 pluralization
|
||||||
|
// rules: https://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
|
||||||
|
// Once those rules are implemented for LibJS, we better use them instead.
|
||||||
|
auto find_plurality = [&](auto plurality) -> Optional<FormatType> {
|
||||||
|
if (auto it = formats.find_if([&](auto& patterns) { return patterns.plurality == plurality; }); it != formats.end())
|
||||||
|
return *it;
|
||||||
|
return {};
|
||||||
|
};
|
||||||
|
|
||||||
|
if (number == 0) {
|
||||||
|
if (auto patterns = find_plurality(FormatType::Plurality::Zero); patterns.has_value())
|
||||||
|
return patterns;
|
||||||
|
} else if (number == 1) {
|
||||||
|
if (auto patterns = find_plurality(FormatType::Plurality::One); patterns.has_value())
|
||||||
|
return patterns;
|
||||||
|
} else if (number == 2) {
|
||||||
|
if (auto patterns = find_plurality(FormatType::Plurality::Two); patterns.has_value())
|
||||||
|
return patterns;
|
||||||
|
} else if (number > 2) {
|
||||||
|
if (auto patterns = find_plurality(FormatType::Plurality::Many); patterns.has_value())
|
||||||
|
return patterns;
|
||||||
|
}
|
||||||
|
|
||||||
|
return find_plurality(FormatType::Plurality::Other);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue