summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibUnicode/Locale.cpp73
-rw-r--r--Userland/Libraries/LibUnicode/Locale.h15
2 files changed, 88 insertions, 0 deletions
diff --git a/Userland/Libraries/LibUnicode/Locale.cpp b/Userland/Libraries/LibUnicode/Locale.cpp
index 8d3f3c87a9..4794523977 100644
--- a/Userland/Libraries/LibUnicode/Locale.cpp
+++ b/Userland/Libraries/LibUnicode/Locale.cpp
@@ -886,4 +886,77 @@ String resolve_most_likely_territory([[maybe_unused]] LanguageID const& language
return aliases[0].to_string();
}
+String LanguageID::to_string() const
+{
+ StringBuilder builder;
+
+ auto append_segment = [&](Optional<String> const& segment) {
+ if (!segment.has_value())
+ return;
+ if (!builder.is_empty())
+ builder.append('-');
+ builder.append(*segment);
+ };
+
+ append_segment(language);
+ append_segment(script);
+ append_segment(region);
+ for (auto const& variant : variants)
+ append_segment(variant);
+
+ return builder.build();
+}
+
+String LocaleID::to_string() const
+{
+ StringBuilder builder;
+
+ auto append_segment = [&](Optional<String> const& segment) {
+ if (!segment.has_value())
+ return;
+ if (!builder.is_empty())
+ builder.append('-');
+ builder.append(*segment);
+ };
+
+ auto append_key_value_list = [&](auto const& key, auto const& values) {
+ append_segment(key);
+ for (auto const& value : values)
+ append_segment(value);
+ };
+
+ append_segment(language_id.to_string());
+
+ for (auto const& extension : extensions) {
+ extension.visit(
+ [&](LocaleExtension const& ext) {
+ builder.append("-u"sv);
+ for (auto const& attribute : ext.attributes)
+ append_segment(attribute);
+ for (auto const& keyword : ext.keywords)
+ append_key_value_list(keyword.key, keyword.types);
+ },
+ [&](TransformedExtension const& ext) {
+ builder.append("-t"sv);
+ if (ext.language.has_value())
+ append_segment(ext.language->to_string());
+ for (auto const& field : ext.fields)
+ append_key_value_list(field.key, field.values);
+ },
+ [&](OtherExtension const& ext) {
+ builder.appendff("-{}", ext.key);
+ for (auto const& value : ext.values)
+ append_segment(value);
+ });
+ }
+
+ if (!private_use_extensions.is_empty()) {
+ builder.append("-x"sv);
+ for (auto const& extension : private_use_extensions)
+ append_segment(extension);
+ }
+
+ return builder.build();
+}
+
}
diff --git a/Userland/Libraries/LibUnicode/Locale.h b/Userland/Libraries/LibUnicode/Locale.h
index b5938061fa..72eebf2e3d 100644
--- a/Userland/Libraries/LibUnicode/Locale.h
+++ b/Userland/Libraries/LibUnicode/Locale.h
@@ -16,6 +16,8 @@
namespace Unicode {
struct LanguageID {
+ String to_string() const;
+
bool is_root { false };
Optional<String> language {};
Optional<String> script {};
@@ -51,6 +53,19 @@ struct OtherExtension {
using Extension = Variant<LocaleExtension, TransformedExtension, OtherExtension>;
struct LocaleID {
+ String to_string() const;
+
+ template<typename ExtensionType>
+ void remove_extension_type()
+ {
+ auto tmp_extensions = move(extensions);
+
+ for (auto& extension : tmp_extensions) {
+ if (!extension.has<ExtensionType>())
+ extensions.append(move(extension));
+ }
+ }
+
LanguageID language_id {};
Vector<Extension> extensions {};
Vector<String> private_use_extensions {};