Locale.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/Runtime/Completion.h>
  12. #include <LibJS/Runtime/Object.h>
  13. #include <LibJS/Runtime/Value.h>
  14. #include <LibUnicode/Forward.h>
  15. namespace JS::Intl {
  16. class Locale final : public Object {
  17. JS_OBJECT(Locale, Object);
  18. public:
  19. static Locale* create(GlobalObject&, Unicode::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. Locale(Object& prototype);
  30. Locale(Unicode::LocaleID const&, Object& prototype);
  31. virtual ~Locale() override = default;
  32. String const& locale() const { return m_locale; }
  33. void set_locale(String locale) { m_locale = move(locale); }
  34. bool has_calendar() const { return m_calendar.has_value(); }
  35. String const& calendar() const { return m_calendar.value(); }
  36. void set_calendar(String calendar) { m_calendar = move(calendar); }
  37. bool has_case_first() const { return m_case_first.has_value(); }
  38. String const& case_first() const { return m_case_first.value(); }
  39. void set_case_first(String case_first) { m_case_first = move(case_first); }
  40. bool has_collation() const { return m_collation.has_value(); }
  41. String const& collation() const { return m_collation.value(); }
  42. void set_collation(String collation) { m_collation = move(collation); }
  43. bool has_hour_cycle() const { return m_hour_cycle.has_value(); }
  44. String const& hour_cycle() const { return m_hour_cycle.value(); }
  45. void set_hour_cycle(String hour_cycle) { m_hour_cycle = move(hour_cycle); }
  46. bool has_numbering_system() const { return m_numbering_system.has_value(); }
  47. String const& numbering_system() const { return m_numbering_system.value(); }
  48. void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
  49. bool numeric() const { return m_numeric; }
  50. void set_numeric(bool numeric) { m_numeric = numeric; }
  51. private:
  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. Array* calendars_of_locale(GlobalObject& global_object, Locale const& locale);
  61. Array* collations_of_locale(GlobalObject& global_object, Locale const& locale);
  62. }