Locale.h 3.8 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/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. public:
  20. static NonnullGCPtr<Locale> create(Realm&, ::Locale::LocaleID);
  21. static constexpr auto relevant_extension_keys()
  22. {
  23. // 14.2.2 Internal slots, https://tc39.es/ecma402/#sec-intl.locale-internal-slots
  24. // The value of the [[RelevantExtensionKeys]] internal slot is « "ca", "co", "hc", "kf", "kn", "nu" ».
  25. // If %Collator%.[[RelevantExtensionKeys]] does not contain "kf", then remove "kf" from %Locale%.[[RelevantExtensionKeys]].
  26. // If %Collator%.[[RelevantExtensionKeys]] does not contain "kn", then remove "kn" from %Locale%.[[RelevantExtensionKeys]].
  27. // 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.
  28. return AK::Array { "ca"sv, "co"sv, "hc"sv, "kf"sv, "kn"sv, "nu"sv };
  29. }
  30. virtual ~Locale() override = default;
  31. String const& locale() const { return m_locale; }
  32. void set_locale(String locale) { m_locale = move(locale); }
  33. bool has_calendar() const { return m_calendar.has_value(); }
  34. String const& calendar() const { return m_calendar.value(); }
  35. void set_calendar(String calendar) { m_calendar = move(calendar); }
  36. bool has_case_first() const { return m_case_first.has_value(); }
  37. String const& case_first() const { return m_case_first.value(); }
  38. void set_case_first(String case_first) { m_case_first = move(case_first); }
  39. bool has_collation() const { return m_collation.has_value(); }
  40. String const& collation() const { return m_collation.value(); }
  41. void set_collation(String collation) { m_collation = move(collation); }
  42. bool has_hour_cycle() const { return m_hour_cycle.has_value(); }
  43. String const& hour_cycle() const { return m_hour_cycle.value(); }
  44. void set_hour_cycle(String hour_cycle) { m_hour_cycle = move(hour_cycle); }
  45. bool has_numbering_system() const { return m_numbering_system.has_value(); }
  46. String const& numbering_system() const { return m_numbering_system.value(); }
  47. void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
  48. bool numeric() const { return m_numeric; }
  49. void set_numeric(bool numeric) { m_numeric = numeric; }
  50. private:
  51. explicit Locale(Object& prototype);
  52. String m_locale; // [[Locale]]
  53. Optional<String> m_calendar; // [[Calendar]]
  54. Optional<String> m_case_first; // [[CaseFirst]]
  55. Optional<String> m_collation; // [[Collation]]
  56. Optional<String> m_hour_cycle; // [[HourCycle]]
  57. Optional<String> 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. NonnullGCPtr<Array> calendars_of_locale(VM&, Locale const&);
  67. NonnullGCPtr<Array> collations_of_locale(VM&, Locale const& locale);
  68. NonnullGCPtr<Array> hour_cycles_of_locale(VM&, Locale const& locale);
  69. NonnullGCPtr<Array> numbering_systems_of_locale(VM&, Locale const&);
  70. NonnullGCPtr<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. }