2021-08-25 02:15:38 +00:00
|
|
|
/*
|
2022-01-31 18:07:22 +00:00
|
|
|
* Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
|
2021-08-25 02:15:38 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-09-03 01:49:24 +00:00
|
|
|
#include <AK/CharacterTypes.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2021-08-25 02:15:38 +00:00
|
|
|
#include <AK/Optional.h>
|
|
|
|
#include <AK/StringView.h>
|
2021-08-27 20:38:06 +00:00
|
|
|
#include <AK/Variant.h>
|
2021-08-25 02:15:38 +00:00
|
|
|
#include <AK/Vector.h>
|
2022-09-02 16:11:30 +00:00
|
|
|
#include <LibLocale/Forward.h>
|
2021-08-25 02:15:38 +00:00
|
|
|
|
2022-09-02 16:01:10 +00:00
|
|
|
namespace Locale {
|
2021-08-25 02:15:38 +00:00
|
|
|
|
|
|
|
struct LanguageID {
|
2022-12-06 01:12:49 +00:00
|
|
|
DeprecatedString to_deprecated_string() const;
|
2021-09-03 01:44:12 +00:00
|
|
|
bool operator==(LanguageID const&) const = default;
|
2021-09-01 21:54:24 +00:00
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
bool is_root { false };
|
2022-12-04 18:02:33 +00:00
|
|
|
Optional<DeprecatedString> language {};
|
|
|
|
Optional<DeprecatedString> script {};
|
|
|
|
Optional<DeprecatedString> region {};
|
|
|
|
Vector<DeprecatedString> variants {};
|
2021-08-25 02:15:38 +00:00
|
|
|
};
|
|
|
|
|
2021-08-27 20:38:06 +00:00
|
|
|
struct Keyword {
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString key {};
|
|
|
|
DeprecatedString value {};
|
2021-08-27 20:38:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct LocaleExtension {
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> attributes {};
|
2021-08-27 20:38:06 +00:00
|
|
|
Vector<Keyword> keywords {};
|
|
|
|
};
|
|
|
|
|
2021-08-27 21:11:48 +00:00
|
|
|
struct TransformedField {
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString key {};
|
|
|
|
DeprecatedString value {};
|
2021-08-27 21:11:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TransformedExtension {
|
|
|
|
Optional<LanguageID> language {};
|
|
|
|
Vector<TransformedField> fields {};
|
|
|
|
};
|
|
|
|
|
2021-08-27 21:24:20 +00:00
|
|
|
struct OtherExtension {
|
|
|
|
char key {};
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString value {};
|
2021-08-27 21:24:20 +00:00
|
|
|
};
|
|
|
|
|
2022-09-02 15:02:34 +00:00
|
|
|
using Extension = AK::Variant<LocaleExtension, TransformedExtension, OtherExtension>;
|
2021-08-27 20:38:06 +00:00
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
struct LocaleID {
|
2022-12-06 01:12:49 +00:00
|
|
|
DeprecatedString to_deprecated_string() const;
|
2021-09-01 21:54:24 +00:00
|
|
|
|
|
|
|
template<typename ExtensionType>
|
2021-09-05 22:29:11 +00:00
|
|
|
Vector<Extension> remove_extension_type()
|
2021-09-01 21:54:24 +00:00
|
|
|
{
|
2021-09-05 22:29:11 +00:00
|
|
|
Vector<Extension> removed_extensions {};
|
2021-09-01 21:54:24 +00:00
|
|
|
auto tmp_extensions = move(extensions);
|
|
|
|
|
|
|
|
for (auto& extension : tmp_extensions) {
|
2021-09-05 22:29:11 +00:00
|
|
|
if (extension.has<ExtensionType>())
|
|
|
|
removed_extensions.append(move(extension));
|
|
|
|
else
|
2021-09-01 21:54:24 +00:00
|
|
|
extensions.append(move(extension));
|
|
|
|
}
|
2021-09-05 22:29:11 +00:00
|
|
|
|
|
|
|
return removed_extensions;
|
2021-09-01 21:54:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
LanguageID language_id {};
|
2021-08-27 20:38:06 +00:00
|
|
|
Vector<Extension> extensions {};
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> private_use_extensions {};
|
2021-08-25 02:15:38 +00:00
|
|
|
};
|
|
|
|
|
LibJS+LibUnicode: Generate all styles of currency localizations
Currently, LibUnicode is only parsing and generating the "long" style of
currency display names. However, the CLDR contains "short" and "narrow"
forms as well that need to be handled. Parse these, and update LibJS to
actually respect the "style" option provided by the user for displaying
currencies with Intl.DisplayNames.
Note: There are some discrepencies between the engines on how style is
handled. In particular, running:
new Intl.DisplayNames('en', {type:'currency', style:'narrow'}).of('usd')
Gives:
SpiderMoney: "USD"
V8: "US Dollar"
LibJS: "$"
And running:
new Intl.DisplayNames('en', {type:'currency', style:'short'}).of('usd')
Gives:
SpiderMonkey: "$"
V8: "US Dollar"
LibJS: "$"
My best guess is V8 isn't handling style, and just returning the long
form (which is what LibJS did before this commit). And SpiderMoney can
handle some styles, but if they don't have a value for the requested
style, they fall back to the canonicalized code passed into of().
2021-11-12 18:11:30 +00:00
|
|
|
enum class Style : u8 {
|
|
|
|
Long,
|
|
|
|
Short,
|
|
|
|
Narrow,
|
|
|
|
};
|
|
|
|
|
2022-01-13 18:10:24 +00:00
|
|
|
struct DisplayPattern {
|
|
|
|
StringView locale_pattern;
|
|
|
|
StringView locale_separator;
|
|
|
|
};
|
|
|
|
|
2021-09-06 17:56:44 +00:00
|
|
|
struct ListPatterns {
|
|
|
|
StringView start;
|
|
|
|
StringView middle;
|
|
|
|
StringView end;
|
|
|
|
StringView pair;
|
|
|
|
};
|
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
// Note: These methods only verify that the provided strings match the EBNF grammar of the
|
|
|
|
// Unicode identifier subtag (i.e. no validation is done that the tags actually exist).
|
2021-09-03 01:49:24 +00:00
|
|
|
constexpr bool is_unicode_language_subtag(StringView subtag)
|
|
|
|
{
|
|
|
|
// unicode_language_subtag = alpha{2,3} | alpha{5,8}
|
|
|
|
if ((subtag.length() < 2) || (subtag.length() == 4) || (subtag.length() > 8))
|
|
|
|
return false;
|
|
|
|
return all_of(subtag, is_ascii_alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool is_unicode_script_subtag(StringView subtag)
|
|
|
|
{
|
|
|
|
// unicode_script_subtag = alpha{4}
|
|
|
|
if (subtag.length() != 4)
|
|
|
|
return false;
|
|
|
|
return all_of(subtag, is_ascii_alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool is_unicode_region_subtag(StringView subtag)
|
|
|
|
{
|
|
|
|
// unicode_region_subtag = (alpha{2} | digit{3})
|
|
|
|
if (subtag.length() == 2)
|
|
|
|
return all_of(subtag, is_ascii_alpha);
|
|
|
|
if (subtag.length() == 3)
|
|
|
|
return all_of(subtag, is_ascii_digit);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool is_unicode_variant_subtag(StringView subtag)
|
|
|
|
{
|
|
|
|
// unicode_variant_subtag = (alphanum{5,8} | digit alphanum{3})
|
|
|
|
if ((subtag.length() >= 5) && (subtag.length() <= 8))
|
|
|
|
return all_of(subtag, is_ascii_alphanumeric);
|
|
|
|
if (subtag.length() == 4)
|
|
|
|
return is_ascii_digit(subtag[0]) && all_of(subtag.substring_view(1), is_ascii_alphanumeric);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-01 11:48:02 +00:00
|
|
|
bool is_type_identifier(StringView);
|
2021-08-25 02:15:38 +00:00
|
|
|
|
|
|
|
Optional<LanguageID> parse_unicode_language_id(StringView);
|
|
|
|
Optional<LocaleID> parse_unicode_locale_id(StringView);
|
2021-09-09 01:56:52 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void canonicalize_unicode_extension_values(StringView key, DeprecatedString& value, bool remove_true);
|
|
|
|
Optional<DeprecatedString> canonicalize_unicode_locale_id(LocaleID&);
|
2021-08-25 02:15:38 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString const& default_locale();
|
2021-08-25 02:17:08 +00:00
|
|
|
bool is_locale_available(StringView locale);
|
2022-01-04 17:22:25 +00:00
|
|
|
|
2022-07-14 18:06:17 +00:00
|
|
|
Span<StringView const> get_available_keyword_values(StringView key);
|
2022-02-15 19:51:48 +00:00
|
|
|
Span<StringView const> get_available_calendars();
|
2022-07-14 13:37:36 +00:00
|
|
|
Span<StringView const> get_available_collation_case_orderings();
|
|
|
|
Span<StringView const> get_available_collation_numeric_orderings();
|
|
|
|
Span<StringView const> get_available_collation_types();
|
2022-09-02 15:27:31 +00:00
|
|
|
Span<StringView const> get_available_currencies();
|
2022-07-14 13:37:36 +00:00
|
|
|
Span<StringView const> get_available_hour_cycles();
|
2022-02-15 19:51:48 +00:00
|
|
|
Span<StringView const> get_available_number_systems();
|
|
|
|
|
2022-01-25 16:27:02 +00:00
|
|
|
Style style_from_string(StringView style);
|
|
|
|
StringView style_to_string(Style style);
|
|
|
|
|
2021-09-05 16:16:35 +00:00
|
|
|
Optional<Locale> locale_from_string(StringView locale);
|
2022-01-04 17:22:25 +00:00
|
|
|
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);
|
2022-01-12 22:16:12 +00:00
|
|
|
Optional<DateField> date_field_from_string(StringView calendar);
|
2022-01-04 17:22:25 +00:00
|
|
|
Optional<ListPatternType> list_pattern_type_from_string(StringView list_pattern_type);
|
2021-08-25 02:17:08 +00:00
|
|
|
|
2022-02-15 17:23:26 +00:00
|
|
|
Optional<Key> key_from_string(StringView key);
|
|
|
|
Optional<KeywordCalendar> keyword_ca_from_string(StringView ca);
|
2022-07-14 13:37:36 +00:00
|
|
|
Optional<KeywordCollation> keyword_co_from_string(StringView co);
|
|
|
|
Optional<KeywordHours> keyword_hc_from_string(StringView hc);
|
2022-02-15 17:23:26 +00:00
|
|
|
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);
|
2022-07-14 17:14:11 +00:00
|
|
|
Optional<StringView> get_preferred_keyword_value_for_locale(StringView locale, StringView key);
|
2022-02-15 17:23:26 +00:00
|
|
|
|
2022-01-13 18:10:24 +00:00
|
|
|
Optional<DisplayPattern> get_locale_display_patterns(StringView locale);
|
2022-12-04 18:02:33 +00:00
|
|
|
Optional<DeprecatedString> format_locale_for_display(StringView locale, LocaleID locale_id);
|
2022-01-13 18:10:24 +00:00
|
|
|
|
2021-08-26 12:17:01 +00:00
|
|
|
Optional<StringView> get_locale_language_mapping(StringView locale, StringView language);
|
2021-08-26 10:56:17 +00:00
|
|
|
Optional<StringView> get_locale_territory_mapping(StringView locale, StringView territory);
|
2021-08-26 12:29:39 +00:00
|
|
|
Optional<StringView> get_locale_script_mapping(StringView locale, StringView script);
|
2022-01-04 17:22:25 +00:00
|
|
|
Optional<StringView> get_locale_long_currency_mapping(StringView locale, StringView currency);
|
|
|
|
Optional<StringView> get_locale_short_currency_mapping(StringView locale, StringView currency);
|
|
|
|
Optional<StringView> get_locale_narrow_currency_mapping(StringView locale, StringView currency);
|
|
|
|
Optional<StringView> get_locale_numeric_currency_mapping(StringView locale, StringView currency);
|
2022-01-12 21:15:41 +00:00
|
|
|
Optional<StringView> get_locale_calendar_mapping(StringView locale, StringView calendar);
|
2022-01-12 22:16:12 +00:00
|
|
|
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);
|
2021-12-16 00:47:17 +00:00
|
|
|
|
2022-01-25 16:38:55 +00:00
|
|
|
Optional<ListPatterns> get_locale_list_patterns(StringView locale, StringView type, Style style);
|
2021-08-25 02:17:08 +00:00
|
|
|
|
2022-07-05 23:40:40 +00:00
|
|
|
Optional<CharacterOrder> character_order_from_string(StringView character_order);
|
|
|
|
StringView character_order_to_string(CharacterOrder character_order);
|
|
|
|
Optional<CharacterOrder> character_order_for_locale(StringView locale);
|
|
|
|
|
2021-08-30 18:56:23 +00:00
|
|
|
Optional<StringView> resolve_language_alias(StringView language);
|
|
|
|
Optional<StringView> resolve_territory_alias(StringView territory);
|
|
|
|
Optional<StringView> resolve_script_tag_alias(StringView script_tag);
|
|
|
|
Optional<StringView> resolve_variant_alias(StringView variant);
|
|
|
|
Optional<StringView> resolve_subdivision_alias(StringView subdivision);
|
2022-01-04 17:22:25 +00:00
|
|
|
void resolve_complex_language_aliases(LanguageID& language_id);
|
2021-08-30 18:56:23 +00:00
|
|
|
|
2021-09-02 22:21:42 +00:00
|
|
|
Optional<LanguageID> add_likely_subtags(LanguageID const& language_id);
|
2021-09-03 01:44:12 +00:00
|
|
|
Optional<LanguageID> remove_likely_subtags(LanguageID const& language_id);
|
2022-01-04 17:22:25 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Optional<DeprecatedString> resolve_most_likely_territory(LanguageID const& language_id);
|
|
|
|
DeprecatedString resolve_most_likely_territory_alias(LanguageID const& language_id, StringView territory_alias);
|
2021-08-31 13:40:24 +00:00
|
|
|
|
2021-08-25 02:15:38 +00:00
|
|
|
}
|