Locale.h 4.3 KB

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