| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-02-28 08:54:42 -05:00
										 |  |  |  * Copyright (c) 2021-2025, Tim Flynn <trflynn89@ladybird.org> | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 19:11:49 -05:00
										 |  |  | #include <AK/Array.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | #include <AK/Optional.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-19 13:13:57 -05:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-11 11:15:42 -04:00
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | #include <LibGC/Ptr.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-05 12:10:24 -04:00
										 |  |  | #include <LibJS/Runtime/Completion.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | #include <LibJS/Runtime/Object.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							| 
									
										
										
										
											2024-06-23 09:14:27 -04:00
										 |  |  | #include <LibUnicode/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS::Intl { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Locale final : public Object { | 
					
						
							|  |  |  |     JS_OBJECT(Locale, Object); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC_DECLARE_ALLOCATOR(Locale); | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     static GC::Ref<Locale> create(Realm&, GC::Ref<Locale> source_locale, String); | 
					
						
							| 
									
										
										
										
											2021-09-02 18:23:39 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 14:33:34 -04:00
										 |  |  |     static constexpr auto locale_extension_keys() | 
					
						
							| 
									
										
										
										
											2021-11-29 19:11:49 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-28 08:54:42 -05:00
										 |  |  |         // 15.2.2 Internal slots, https://tc39.es/ecma402/#sec-intl.locale-internal-slots
 | 
					
						
							| 
									
										
										
										
											2023-10-03 10:18:18 -04:00
										 |  |  |         // 1.3.2 Internal slots, https://tc39.es/proposal-intl-locale-info/#sec-intl.locale-internal-slots
 | 
					
						
							| 
									
										
										
										
											2025-04-07 14:33:34 -04:00
										 |  |  |         // The value of the [[LocaleExtensionKeys]] internal slot is « "ca", "co", "fw", "hc", "kf", "kn", "nu" ».
 | 
					
						
							|  |  |  |         // If %Intl.Collator%.[[RelevantExtensionKeys]] does not contain "kf", then remove "kf" from %Intl.Locale%.[[LocaleExtensionKeys]].
 | 
					
						
							|  |  |  |         // If %Intl.Collator%.[[RelevantExtensionKeys]] does not contain "kn", then remove "kn" from %Intl.Locale%.[[LocaleExtensionKeys]].
 | 
					
						
							| 
									
										
										
										
											2023-10-03 10:18:18 -04:00
										 |  |  |         return AK::Array { "ca"sv, "co"sv, "fw"sv, "hc"sv, "kf"sv, "kn"sv, "nu"sv }; | 
					
						
							| 
									
										
										
										
											2021-11-29 19:11:49 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-11 11:15:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  |     virtual ~Locale() override = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-19 13:13:57 -05:00
										 |  |  |     String const& locale() const { return m_locale; } | 
					
						
							|  |  |  |     void set_locale(String locale) { m_locale = move(locale); } | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool has_calendar() const { return m_calendar.has_value(); } | 
					
						
							| 
									
										
										
										
											2023-01-19 13:13:57 -05:00
										 |  |  |     String const& calendar() const { return m_calendar.value(); } | 
					
						
							|  |  |  |     void set_calendar(String calendar) { m_calendar = move(calendar); } | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool has_case_first() const { return m_case_first.has_value(); } | 
					
						
							| 
									
										
										
										
											2023-01-19 13:13:57 -05:00
										 |  |  |     String const& case_first() const { return m_case_first.value(); } | 
					
						
							|  |  |  |     void set_case_first(String case_first) { m_case_first = move(case_first); } | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool has_collation() const { return m_collation.has_value(); } | 
					
						
							| 
									
										
										
										
											2023-01-19 13:13:57 -05:00
										 |  |  |     String const& collation() const { return m_collation.value(); } | 
					
						
							|  |  |  |     void set_collation(String collation) { m_collation = move(collation); } | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-03 10:18:18 -04:00
										 |  |  |     bool has_first_day_of_week() const { return m_first_day_of_week.has_value(); } | 
					
						
							| 
									
										
										
										
											2024-07-11 09:29:06 -04:00
										 |  |  |     String const& first_day_of_week() const { return m_first_day_of_week.value(); } | 
					
						
							|  |  |  |     void set_first_day_of_week(String first_day_of_week) { m_first_day_of_week = move(first_day_of_week); } | 
					
						
							| 
									
										
										
										
											2023-10-03 10:18:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  |     bool has_hour_cycle() const { return m_hour_cycle.has_value(); } | 
					
						
							| 
									
										
										
										
											2023-01-19 13:13:57 -05:00
										 |  |  |     String const& hour_cycle() const { return m_hour_cycle.value(); } | 
					
						
							|  |  |  |     void set_hour_cycle(String hour_cycle) { m_hour_cycle = move(hour_cycle); } | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool has_numbering_system() const { return m_numbering_system.has_value(); } | 
					
						
							| 
									
										
										
										
											2023-01-19 13:13:57 -05:00
										 |  |  |     String const& numbering_system() const { return m_numbering_system.value(); } | 
					
						
							|  |  |  |     void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); } | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool numeric() const { return m_numeric; } | 
					
						
							|  |  |  |     void set_numeric(bool numeric) { m_numeric = numeric; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-08-28 23:51:28 +02:00
										 |  |  |     explicit Locale(Object& prototype); | 
					
						
							| 
									
										
										
										
											2023-01-19 13:13:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-11 09:29:06 -04:00
										 |  |  |     String m_locale;                      // [[Locale]]
 | 
					
						
							|  |  |  |     Optional<String> m_calendar;          // [[Calendar]]
 | 
					
						
							|  |  |  |     Optional<String> m_case_first;        // [[CaseFirst]]
 | 
					
						
							|  |  |  |     Optional<String> m_collation;         // [[Collation]]
 | 
					
						
							|  |  |  |     Optional<String> m_first_day_of_week; // [[FirstDayOfWeek]]
 | 
					
						
							|  |  |  |     Optional<String> m_hour_cycle;        // [[HourCycle]]
 | 
					
						
							|  |  |  |     Optional<String> m_numbering_system;  // [[NumberingSystem]]
 | 
					
						
							|  |  |  |     bool m_numeric { false };             // [[Numeric]]
 | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-06 08:30:53 -04:00
										 |  |  | // Table 1: WeekInfo Record Fields, https://tc39.es/proposal-intl-locale-info/#table-locale-weekinfo-record
 | 
					
						
							|  |  |  | struct WeekInfo { | 
					
						
							|  |  |  |     u8 minimal_days { 0 }; // [[MinimalDays]]
 | 
					
						
							|  |  |  |     u8 first_day { 0 };    // [[FirstDay]]
 | 
					
						
							|  |  |  |     Vector<u8> weekend;    // [[Weekend]]
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC::Ref<Array> calendars_of_locale(VM&, Locale const&); | 
					
						
							|  |  |  | GC::Ref<Array> collations_of_locale(VM&, Locale const& locale); | 
					
						
							|  |  |  | GC::Ref<Array> hour_cycles_of_locale(VM&, Locale const& locale); | 
					
						
							|  |  |  | GC::Ref<Array> numbering_systems_of_locale(VM&, Locale const&); | 
					
						
							|  |  |  | GC::Ref<Array> time_zones_of_locale(VM&, StringView region); | 
					
						
							| 
									
										
										
										
											2023-08-22 15:39:18 -04:00
										 |  |  | StringView character_direction_of_locale(Locale const&); | 
					
						
							| 
									
										
										
										
											2024-07-11 09:29:06 -04:00
										 |  |  | StringView weekday_to_string(StringView weekday); | 
					
						
							|  |  |  | Optional<u8> string_to_weekday_value(StringView weekday); | 
					
						
							| 
									
										
										
										
											2023-08-30 11:50:02 -04:00
										 |  |  | WeekInfo week_info_of_locale(Locale const&); | 
					
						
							| 
									
										
										
										
											2022-07-05 12:10:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-02 08:32:43 -04:00
										 |  |  | } |