PluralRules.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. JS_DECLARE_ALLOCATOR(PluralRules);
  17. public:
  18. virtual ~PluralRules() override = default;
  19. ::Locale::PluralForm type() const { return m_type; }
  20. StringView type_string() const { return ::Locale::plural_form_to_string(m_type); }
  21. void set_type(StringView type) { m_type = ::Locale::plural_form_from_string(type); }
  22. private:
  23. explicit PluralRules(Object& prototype);
  24. ::Locale::PluralForm m_type { ::Locale::PluralForm::Cardinal }; // [[Type]]
  25. };
  26. struct ResolvedPlurality {
  27. ::Locale::PluralCategory plural_category; // [[PluralCategory]]
  28. String formatted_string; // [[FormattedString]]
  29. };
  30. ::Locale::PluralOperands get_operands(StringView string);
  31. ::Locale::PluralCategory plural_rule_select(StringView locale, ::Locale::PluralForm type, Value number, ::Locale::PluralOperands operands);
  32. ResolvedPlurality resolve_plural(PluralRules const&, Value number);
  33. ResolvedPlurality resolve_plural(NumberFormatBase const& number_format, ::Locale::PluralForm type, Value number);
  34. ::Locale::PluralCategory plural_rule_select_range(StringView locale, ::Locale::PluralForm, ::Locale::PluralCategory start, ::Locale::PluralCategory end);
  35. ThrowCompletionOr<::Locale::PluralCategory> resolve_plural_range(VM&, PluralRules const&, Value start, Value end);
  36. }