mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 10:38:13 +00:00
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.
This commit is contained in:
parent
d0fc61e79b
commit
89ead8c00a
6 changed files with 236 additions and 121 deletions
|
@ -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;
|
||||
|
|
|
@ -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 {}; }
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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 {};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue