DisplayNames.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <AK/String.h>
  9. #include <AK/StringView.h>
  10. #include <LibJS/Runtime/Object.h>
  11. #include <LibLocale/Locale.h>
  12. namespace JS::Intl {
  13. class DisplayNames final : public Object {
  14. JS_OBJECT(DisplayNames, Object);
  15. JS_DECLARE_ALLOCATOR(DisplayNames);
  16. enum class Type {
  17. Invalid,
  18. Language,
  19. Region,
  20. Script,
  21. Currency,
  22. Calendar,
  23. DateTimeField,
  24. };
  25. enum class Fallback {
  26. Invalid,
  27. None,
  28. Code,
  29. };
  30. enum class LanguageDisplay {
  31. Dialect,
  32. Standard,
  33. };
  34. public:
  35. virtual ~DisplayNames() override = default;
  36. String const& locale() const { return m_locale; }
  37. void set_locale(String locale) { m_locale = move(locale); }
  38. ::Locale::Style style() const { return m_style; }
  39. void set_style(StringView style) { m_style = ::Locale::style_from_string(style); }
  40. StringView style_string() const { return ::Locale::style_to_string(m_style); }
  41. Type type() const { return m_type; }
  42. void set_type(StringView type);
  43. StringView type_string() const;
  44. Fallback fallback() const { return m_fallback; }
  45. void set_fallback(StringView fallback);
  46. StringView fallback_string() const;
  47. bool has_language_display() const { return m_language_display.has_value(); }
  48. LanguageDisplay language_display() const { return *m_language_display; }
  49. void set_language_display(StringView language_display);
  50. StringView language_display_string() const;
  51. private:
  52. DisplayNames(Object& prototype);
  53. String m_locale; // [[Locale]]
  54. ::Locale::Style m_style { ::Locale::Style::Long }; // [[Style]]
  55. Type m_type { Type::Invalid }; // [[Type]]
  56. Fallback m_fallback { Fallback::Invalid }; // [[Fallback]]
  57. Optional<LanguageDisplay> m_language_display {}; // [[LanguageDisplay]]
  58. };
  59. ThrowCompletionOr<Value> canonical_code_for_display_names(VM&, DisplayNames::Type, StringView code);
  60. bool is_valid_date_time_field_code(StringView field);
  61. }