DisplayNames.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include <LibUnicode/Locale.h>
  12. namespace JS::Intl {
  13. class DisplayNames final : public Object {
  14. JS_OBJECT(DisplayNames, Object);
  15. enum class Type {
  16. Invalid,
  17. Language,
  18. Region,
  19. Script,
  20. Currency,
  21. Calendar,
  22. DateTimeField,
  23. };
  24. enum class Fallback {
  25. Invalid,
  26. None,
  27. Code,
  28. };
  29. enum class LanguageDisplay {
  30. Dialect,
  31. Standard,
  32. };
  33. public:
  34. DisplayNames(Object& prototype);
  35. virtual ~DisplayNames() override = default;
  36. String const& locale() const { return m_locale; }
  37. void set_locale(String locale) { m_locale = move(locale); }
  38. Unicode::Style style() const { return m_style; }
  39. void set_style(StringView style) { m_style = Unicode::style_from_string(style); }
  40. StringView style_string() const { return Unicode::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. String m_locale; // [[Locale]]
  53. Unicode::Style m_style { Unicode::Style::Long }; // [[Style]]
  54. Type m_type { Type::Invalid }; // [[Type]]
  55. Fallback m_fallback { Fallback::Invalid }; // [[Fallback]]
  56. Optional<LanguageDisplay> m_language_display {}; // [[LanguageDisplay]]
  57. };
  58. ThrowCompletionOr<Value> canonical_code_for_display_names(GlobalObject& global_object, DisplayNames::Type type, StringView code);
  59. bool is_valid_date_time_field_code(StringView field);
  60. }