summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-02-15 12:23:26 -0500
committerTim Flynn <trflynn89@pm.me>2022-02-16 07:23:07 -0500
commit89ead8c00a7c25aca8ab0022f09babcea9485bc2 (patch)
tree855a3784dea7be6e58223975ccee01b8cbd859ba /Userland
parentd0fc61e79bc3580c9557c22de3299623a9099c6c (diff)
downloadserenity-89ead8c00a7c25aca8ab0022f09babcea9485bc2.zip
LibJS+LibUnicode: Parse Unicode keywords from the BCP 47 CLDR package
We have a fair amount of hard-coded keywords / aliases that can now be replaced with real data from BCP 47. As a result, the also changes the awkward way we were previously generating keys. Before, we were more or less generating keywords as a CSV list of keys, e.g. for the "nu" key, we'd generate "latn,arab,grek" (ordered by locale preference). Then at runtime, we'd split on the comma. We now just generate spans of keywords directly.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp2
-rw-r--r--Userland/Libraries/LibUnicode/Forward.h5
-rw-r--r--Userland/Libraries/LibUnicode/Locale.cpp29
-rw-r--r--Userland/Libraries/LibUnicode/Locale.h11
-rw-r--r--Userland/Libraries/LibUnicode/NumberFormat.cpp7
5 files changed, 20 insertions, 34 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp
index 2e645036dc..6844758727 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp
@@ -427,7 +427,7 @@ LocaleResult resolve_locale(Vector<String> const& requested_locales, LocaleOptio
// b. Assert: Type(foundLocaleData) is Record.
// c. Let keyLocaleData be foundLocaleData.[[<key>]].
// d. Assert: Type(keyLocaleData) is List.
- auto key_locale_data = Unicode::get_locale_key_mapping_list(found_locale, key);
+ auto key_locale_data = Unicode::get_keywords_for_locale(found_locale, key);
// e. Let value be keyLocaleData[0].
// f. Assert: Type(value) is either String or Null.
diff --git a/Userland/Libraries/LibUnicode/Forward.h b/Userland/Libraries/LibUnicode/Forward.h
index 3b5d68c034..52140c7d24 100644
--- a/Userland/Libraries/LibUnicode/Forward.h
+++ b/Userland/Libraries/LibUnicode/Forward.h
@@ -14,7 +14,6 @@ enum class Block : u16;
enum class Calendar : u8;
enum class CalendarFormatType : u8;
enum class CalendarPatternStyle : u8;
-enum class CalendarName : u8;
enum class CalendarSymbol : u8;
enum class CompactNumberFormatType : u8;
enum class Condition : u8;
@@ -27,6 +26,10 @@ enum class GraphemeBreakProperty : u8;
enum class HourCycle : u8;
enum class HourCycleRegion : u8;
enum class Key : u8;
+enum class KeywordCalendar : u8;
+enum class KeywordColCaseFirst : u8;
+enum class KeywordColNumeric : u8;
+enum class KeywordNumbers : u8;
enum class Language : u16;
enum class ListPatternStyle : u8;
enum class ListPatternType : u8;
diff --git a/Userland/Libraries/LibUnicode/Locale.cpp b/Userland/Libraries/LibUnicode/Locale.cpp
index fa191ca651..71b7c11912 100644
--- a/Userland/Libraries/LibUnicode/Locale.cpp
+++ b/Userland/Libraries/LibUnicode/Locale.cpp
@@ -771,10 +771,14 @@ Optional<Language> __attribute__((weak)) language_from_string(StringView) { retu
Optional<Territory> __attribute__((weak)) territory_from_string(StringView) { return {}; }
Optional<ScriptTag> __attribute__((weak)) script_tag_from_string(StringView) { return {}; }
Optional<Currency> __attribute__((weak)) currency_from_string(StringView) { return {}; }
-Optional<CalendarName> __attribute__((weak)) calendar_name_from_string(StringView) { return {}; }
Optional<DateField> __attribute__((weak)) date_field_from_string(StringView) { return {}; }
-Optional<Key> __attribute__((weak)) key_from_string(StringView) { return {}; }
Optional<ListPatternType> __attribute__((weak)) list_pattern_type_from_string(StringView) { return {}; }
+Optional<Key> __attribute__((weak)) key_from_string(StringView) { return {}; }
+Optional<KeywordCalendar> __attribute__((weak)) keyword_ca_from_string(StringView) { return {}; }
+Optional<KeywordColCaseFirst> __attribute__((weak)) keyword_kf_from_string(StringView) { return {}; }
+Optional<KeywordColNumeric> __attribute__((weak)) keyword_kn_from_string(StringView) { return {}; }
+Optional<KeywordNumbers> __attribute__((weak)) keyword_nu_from_string(StringView) { return {}; }
+Vector<StringView> __attribute__((weak)) get_keywords_for_locale(StringView, StringView) { return {}; }
Optional<DisplayPattern> __attribute__((weak)) get_locale_display_patterns(StringView) { return {}; }
Optional<StringView> __attribute__((weak)) get_locale_language_mapping(StringView, StringView) { return {}; }
Optional<StringView> __attribute__((weak)) get_locale_territory_mapping(StringView, StringView) { return {}; }
@@ -787,7 +791,6 @@ Optional<StringView> __attribute__((weak)) get_locale_calendar_mapping(StringVie
Optional<StringView> __attribute__((weak)) get_locale_long_date_field_mapping(StringView, StringView) { return {}; }
Optional<StringView> __attribute__((weak)) get_locale_short_date_field_mapping(StringView, StringView) { return {}; }
Optional<StringView> __attribute__((weak)) get_locale_narrow_date_field_mapping(StringView, StringView) { return {}; }
-Optional<StringView> __attribute__((weak)) get_locale_key_mapping(StringView, StringView) { return {}; }
// https://www.unicode.org/reports/tr35/tr35-39/tr35-general.html#Display_Name_Elements
Optional<String> format_locale_for_display(StringView locale, LocaleID locale_id)
@@ -823,26 +826,6 @@ Optional<String> format_locale_for_display(StringView locale, LocaleID locale_id
return patterns->locale_pattern.replace("{0}"sv, primary_tag).replace("{1}"sv, *secondary_tag);
}
-Vector<StringView> get_locale_key_mapping_list(StringView locale, StringView keyword)
-{
- if (keyword == "hc"sv) {
- auto hour_cycles = get_locale_hour_cycles(locale);
-
- Vector<StringView> values;
- values.ensure_capacity(hour_cycles.size());
-
- for (auto hour_cycle : hour_cycles)
- values.unchecked_append(hour_cycle_to_string(hour_cycle));
-
- return values;
- }
-
- if (auto values = get_locale_key_mapping(locale, keyword); values.has_value())
- return values->split_view(',');
-
- return {};
-}
-
Optional<ListPatterns> __attribute__((weak)) get_locale_list_patterns(StringView, StringView, Style) { return {}; }
Optional<StringView> __attribute__((weak)) resolve_language_alias(StringView) { return {}; }
Optional<StringView> __attribute__((weak)) resolve_territory_alias(StringView) { return {}; }
diff --git a/Userland/Libraries/LibUnicode/Locale.h b/Userland/Libraries/LibUnicode/Locale.h
index e556b14a9d..18f66ba17d 100644
--- a/Userland/Libraries/LibUnicode/Locale.h
+++ b/Userland/Libraries/LibUnicode/Locale.h
@@ -153,11 +153,16 @@ Optional<Language> language_from_string(StringView language);
Optional<Territory> territory_from_string(StringView territory);
Optional<ScriptTag> script_tag_from_string(StringView script_tag);
Optional<Currency> currency_from_string(StringView currency);
-Optional<CalendarName> calendar_name_from_string(StringView calendar);
Optional<DateField> date_field_from_string(StringView calendar);
-Optional<Key> key_from_string(StringView key);
Optional<ListPatternType> list_pattern_type_from_string(StringView list_pattern_type);
+Optional<Key> key_from_string(StringView key);
+Optional<KeywordCalendar> keyword_ca_from_string(StringView ca);
+Optional<KeywordColCaseFirst> keyword_kf_from_string(StringView kf);
+Optional<KeywordColNumeric> keyword_kn_from_string(StringView kn);
+Optional<KeywordNumbers> keyword_nu_from_string(StringView nu);
+Vector<StringView> get_keywords_for_locale(StringView locale, StringView key);
+
Optional<DisplayPattern> get_locale_display_patterns(StringView locale);
Optional<String> format_locale_for_display(StringView locale, LocaleID locale_id);
@@ -172,8 +177,6 @@ Optional<StringView> get_locale_calendar_mapping(StringView locale, StringView c
Optional<StringView> get_locale_long_date_field_mapping(StringView locale, StringView date_field);
Optional<StringView> get_locale_short_date_field_mapping(StringView locale, StringView date_field);
Optional<StringView> get_locale_narrow_date_field_mapping(StringView locale, StringView date_field);
-Optional<StringView> get_locale_key_mapping(StringView locale, StringView keyword);
-Vector<StringView> get_locale_key_mapping_list(StringView locale, StringView keyword);
Optional<ListPatterns> get_locale_list_patterns(StringView locale, StringView type, Style style);
diff --git a/Userland/Libraries/LibUnicode/NumberFormat.cpp b/Userland/Libraries/LibUnicode/NumberFormat.cpp
index f4d0e65e08..e9cbc2a4b4 100644
--- a/Userland/Libraries/LibUnicode/NumberFormat.cpp
+++ b/Userland/Libraries/LibUnicode/NumberFormat.cpp
@@ -26,11 +26,8 @@ Vector<NumberFormat> __attribute__((weak)) get_unit_formats(StringView, StringVi
Optional<StringView> get_default_number_system(StringView locale)
{
- if (auto systems = get_locale_key_mapping(locale, "nu"sv); systems.has_value()) {
- auto index = systems->find(',');
- return index.has_value() ? systems->substring_view(0, *index) : *systems;
- }
-
+ if (auto systems = get_keywords_for_locale(locale, "nu"sv); !systems.is_empty())
+ return systems[0];
return {};
}