PluralRules.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2022-2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <AK/StringView.h>
  9. #include <LibJS/Runtime/Completion.h>
  10. #include <LibJS/Runtime/Intl/NumberFormat.h>
  11. #include <LibJS/Runtime/Object.h>
  12. #include <LibLocale/PluralRules.h>
  13. namespace JS::Intl {
  14. class PluralRules final : public NumberFormatBase {
  15. JS_OBJECT(PluralRules, NumberFormatBase);
  16. public:
  17. virtual ~PluralRules() override = default;
  18. ::Locale::PluralForm type() const { return m_type; }
  19. StringView type_string() const { return ::Locale::plural_form_to_string(m_type); }
  20. void set_type(StringView type) { m_type = ::Locale::plural_form_from_string(type); }
  21. private:
  22. explicit PluralRules(Object& prototype);
  23. ::Locale::PluralForm m_type { ::Locale::PluralForm::Cardinal }; // [[Type]]
  24. };
  25. struct ResolvedPlurality {
  26. ::Locale::PluralCategory plural_category; // [[PluralCategory]]
  27. String formatted_string; // [[FormattedString]]
  28. };
  29. ::Locale::PluralOperands get_operands(StringView string);
  30. ::Locale::PluralCategory plural_rule_select(StringView locale, ::Locale::PluralForm type, Value number, ::Locale::PluralOperands operands);
  31. ResolvedPlurality resolve_plural(PluralRules const&, Value number);
  32. ResolvedPlurality resolve_plural(NumberFormatBase const& number_format, ::Locale::PluralForm type, Value number);
  33. ::Locale::PluralCategory plural_rule_select_range(StringView locale, ::Locale::PluralForm, ::Locale::PluralCategory start, ::Locale::PluralCategory end);
  34. ThrowCompletionOr<::Locale::PluralCategory> resolve_plural_range(VM&, PluralRules const&, Value start, Value end);
  35. }