DisplayNames.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2021-2022, Tim Flynn <trflynn89@pm.me>
  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. namespace JS::Intl {
  12. class DisplayNames final : public Object {
  13. JS_OBJECT(DisplayNames, Object);
  14. enum class Style {
  15. Invalid,
  16. Narrow,
  17. Short,
  18. Long,
  19. };
  20. enum class Type {
  21. Invalid,
  22. Language,
  23. Region,
  24. Script,
  25. Currency,
  26. Calendar,
  27. DateTimeField,
  28. };
  29. enum class Fallback {
  30. Invalid,
  31. None,
  32. Code,
  33. };
  34. enum class LanguageDisplay {
  35. Dialect,
  36. Standard,
  37. };
  38. public:
  39. DisplayNames(Object& prototype);
  40. virtual ~DisplayNames() override = default;
  41. String const& locale() const { return m_locale; }
  42. void set_locale(String locale) { m_locale = move(locale); }
  43. Style style() const { return m_style; }
  44. void set_style(StringView style);
  45. StringView style_string() const;
  46. Type type() const { return m_type; }
  47. void set_type(StringView type);
  48. StringView type_string() const;
  49. Fallback fallback() const { return m_fallback; }
  50. void set_fallback(StringView fallback);
  51. StringView fallback_string() const;
  52. bool has_language_display() const { return m_language_display.has_value(); }
  53. LanguageDisplay language_display() const { return *m_language_display; }
  54. void set_language_display(StringView language_display);
  55. StringView language_display_string() const;
  56. private:
  57. String m_locale; // [[Locale]]
  58. Style m_style { Style::Invalid }; // [[Style]]
  59. Type m_type { Type::Invalid }; // [[Type]]
  60. Fallback m_fallback { Fallback::Invalid }; // [[Fallback]]
  61. Optional<LanguageDisplay> m_language_display {}; // [[LanguageDisplay]]
  62. };
  63. ThrowCompletionOr<Value> canonical_code_for_display_names(GlobalObject& global_object, DisplayNames::Type type, StringView code);
  64. bool is_valid_date_time_field_code(StringView field);
  65. }