DisplayNames.h 2.3 KB

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