summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibUnicode/NumberFormat.h
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries/LibUnicode/NumberFormat.h')
-rw-r--r--Userland/Libraries/LibUnicode/NumberFormat.h30
1 files changed, 29 insertions, 1 deletions
diff --git a/Userland/Libraries/LibUnicode/NumberFormat.h b/Userland/Libraries/LibUnicode/NumberFormat.h
index 2f3fcfac13..34b69f523a 100644
--- a/Userland/Libraries/LibUnicode/NumberFormat.h
+++ b/Userland/Libraries/LibUnicode/NumberFormat.h
@@ -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_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);
+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);
+}
+
}