summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-09-02 18:23:39 -0400
committerLinus Groh <mail@linusgroh.de>2021-09-04 13:51:40 +0100
commit03d7f01e0ccfde1c37385c58097337de4c9df2ab (patch)
treefc01dda4c90400b4832347786633209627d1aaa9 /Userland
parent3abfe7f7dbff2ecf4be6e28d9782807f7182b774 (diff)
downloadserenity-03d7f01e0ccfde1c37385c58097337de4c9df2ab.zip
LibJS: Add a constructor to create an Intl.Locale object from a LocaleID
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp42
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/Locale.h4
2 files changed, 46 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp b/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp
index 22151f7a98..4136c8419a 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp
@@ -4,15 +4,57 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/StringBuilder.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/Locale.h>
+#include <LibUnicode/Locale.h>
namespace JS::Intl {
+Locale* Locale::create(GlobalObject& global_object, Unicode::LocaleID const& locale_id)
+{
+ return global_object.heap().allocate<Locale>(global_object, locale_id, *global_object.intl_locale_prototype());
+}
+
// 14 Locale Objects, https://tc39.es/ecma402/#locale-objects
Locale::Locale(Object& prototype)
: Object(prototype)
{
}
+Locale::Locale(Unicode::LocaleID const& locale_id, Object& prototype)
+ : Object(prototype)
+{
+ set_locale(locale_id.to_string());
+
+ auto join_keyword_types = [](auto const& types) {
+ StringBuilder builder;
+ builder.join('-', types);
+ return builder.build();
+ };
+
+ for (auto const& extension : locale_id.extensions) {
+ if (!extension.has<Unicode::LocaleExtension>())
+ continue;
+
+ for (auto const& keyword : extension.get<Unicode::LocaleExtension>().keywords) {
+ if (keyword.key == "ca"sv) {
+ set_calendar(join_keyword_types(keyword.types));
+ } else if (keyword.key == "co"sv) {
+ set_collation(join_keyword_types(keyword.types));
+ } else if (keyword.key == "hc"sv) {
+ set_hour_cycle(join_keyword_types(keyword.types));
+ } else if (keyword.key == "kf"sv) {
+ set_case_first(join_keyword_types(keyword.types));
+ } else if (keyword.key == "kn"sv) {
+ set_numeric(keyword.types.is_empty());
+ } else if (keyword.key == "nu"sv) {
+ set_numbering_system(join_keyword_types(keyword.types));
+ }
+ }
+
+ break;
+ }
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Locale.h b/Userland/Libraries/LibJS/Runtime/Intl/Locale.h
index 1c2cc646b2..480a0ffc98 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/Locale.h
+++ b/Userland/Libraries/LibJS/Runtime/Intl/Locale.h
@@ -10,6 +10,7 @@
#include <AK/String.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Value.h>
+#include <LibUnicode/Forward.h>
namespace JS::Intl {
@@ -17,7 +18,10 @@ class Locale final : public Object {
JS_OBJECT(Locale, Object);
public:
+ static Locale* create(GlobalObject&, Unicode::LocaleID const&);
+
Locale(Object& prototype);
+ Locale(Unicode::LocaleID const&, Object& prototype);
virtual ~Locale() override = default;
String const& locale() const { return m_locale; }