Locale.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/Array.h>
  8. #include <AK/DeprecatedString.h>
  9. #include <AK/Optional.h>
  10. #include <AK/Vector.h>
  11. #include <LibJS/Runtime/Completion.h>
  12. #include <LibJS/Runtime/Object.h>
  13. #include <LibJS/Runtime/Value.h>
  14. #include <LibLocale/Forward.h>
  15. namespace JS::Intl {
  16. class Locale final : public Object {
  17. JS_OBJECT(Locale, Object);
  18. public:
  19. static Locale* create(Realm&, ::Locale::LocaleID const&);
  20. static constexpr auto relevant_extension_keys()
  21. {
  22. // 14.2.2 Internal slots, https://tc39.es/ecma402/#sec-intl.locale-internal-slots
  23. // The value of the [[RelevantExtensionKeys]] internal slot is « "ca", "co", "hc", "kf", "kn", "nu" ».
  24. // If %Collator%.[[RelevantExtensionKeys]] does not contain "kf", then remove "kf" from %Locale%.[[RelevantExtensionKeys]].
  25. // If %Collator%.[[RelevantExtensionKeys]] does not contain "kn", then remove "kn" from %Locale%.[[RelevantExtensionKeys]].
  26. // FIXME: We do not yet have an Intl.Collator object. For now, we behave as if "kf" and "kn" exist, as test262 depends on it.
  27. return AK::Array { "ca"sv, "co"sv, "hc"sv, "kf"sv, "kn"sv, "nu"sv };
  28. }
  29. virtual ~Locale() override = default;
  30. DeprecatedString const& locale() const { return m_locale; }
  31. void set_locale(DeprecatedString locale) { m_locale = move(locale); }
  32. bool has_calendar() const { return m_calendar.has_value(); }
  33. DeprecatedString const& calendar() const { return m_calendar.value(); }
  34. void set_calendar(DeprecatedString calendar) { m_calendar = move(calendar); }
  35. bool has_case_first() const { return m_case_first.has_value(); }
  36. DeprecatedString const& case_first() const { return m_case_first.value(); }
  37. void set_case_first(DeprecatedString case_first) { m_case_first = move(case_first); }
  38. bool has_collation() const { return m_collation.has_value(); }
  39. DeprecatedString const& collation() const { return m_collation.value(); }
  40. void set_collation(DeprecatedString collation) { m_collation = move(collation); }
  41. bool has_hour_cycle() const { return m_hour_cycle.has_value(); }
  42. DeprecatedString const& hour_cycle() const { return m_hour_cycle.value(); }
  43. void set_hour_cycle(DeprecatedString hour_cycle) { m_hour_cycle = move(hour_cycle); }
  44. bool has_numbering_system() const { return m_numbering_system.has_value(); }
  45. DeprecatedString const& numbering_system() const { return m_numbering_system.value(); }
  46. void set_numbering_system(DeprecatedString numbering_system) { m_numbering_system = move(numbering_system); }
  47. bool numeric() const { return m_numeric; }
  48. void set_numeric(bool numeric) { m_numeric = numeric; }
  49. private:
  50. explicit Locale(Object& prototype);
  51. Locale(::Locale::LocaleID const&, Object& prototype);
  52. DeprecatedString m_locale; // [[Locale]]
  53. Optional<DeprecatedString> m_calendar; // [[Calendar]]
  54. Optional<DeprecatedString> m_case_first; // [[CaseFirst]]
  55. Optional<DeprecatedString> m_collation; // [[Collation]]
  56. Optional<DeprecatedString> m_hour_cycle; // [[HourCycle]]
  57. Optional<DeprecatedString> m_numbering_system; // [[NumberingSystem]]
  58. bool m_numeric { false }; // [[Numeric]]
  59. };
  60. // Table 1: WeekInfo Record Fields, https://tc39.es/proposal-intl-locale-info/#table-locale-weekinfo-record
  61. struct WeekInfo {
  62. u8 minimal_days { 0 }; // [[MinimalDays]]
  63. u8 first_day { 0 }; // [[FirstDay]]
  64. Vector<u8> weekend; // [[Weekend]]
  65. };
  66. Array* calendars_of_locale(VM&, Locale const&);
  67. Array* collations_of_locale(VM&, Locale const& locale);
  68. Array* hour_cycles_of_locale(VM&, Locale const& locale);
  69. Array* numbering_systems_of_locale(VM&, Locale const&);
  70. Array* time_zones_of_locale(VM&, StringView region);
  71. StringView character_direction_of_locale(Locale const&);
  72. WeekInfo week_info_of_locale(Locale const&);
  73. }