Language Tag language-tag
Guides usage of base::i18n::LanguageTag in Chromium C++, Mojo, and Preferences.
Use it when parsing, canonicalizing, storing, matching, or refactoring BCP47 language tags and locale strings.
- Our test
- —
- not tested yet
- GitHub stars
- 26k
- chromium/chromium
- Added by
- 0
- members
- Updated
- Dec 6
- commit 8cf236c
- Content
- 1 file
- no scripts · 11 KB
- License
- BSD 3-Clause
- free to use
What our test found
Not tested yet. Our model tries every skill on a realistic request and scores the result, the most popular skills first. Until then, the safety checks on the right still apply.
name: language-tag description: >- Guides usage of base::i18n::LanguageTag in Chromium C++, Mojo, and Preferences. Use when parsing, canonicalizing, storing, matching, or refactoring BCP47 language tags and locale strings.
base::i18n::LanguageTag
base::i18n::LanguageTag (base/i18n/language_tag.h) is Chromium's immutable, type-safe wrapper for canonical BCP47 language tags (e.g., "en-US", "zh-Hant-TW", "es-419", "en-US-u-ca-gregory").
Properties and GN Dependencies
- Non-default-constructible:
LanguageTaghas no public default constructor (onlymojo::DefaultConstruct::Tagfor Mojo deserialization andbase::PassKey<blink::HashTraits<LanguageTag>>for Blink hash sentinels). Initialize variables/members explicitly or wrap instd::optional<LanguageTag>. - Value semantics: Copyable, movable,
constexpr-friendly, comparable with==and<=>(std::strong_orderingovertag_string()), streamable viaoperator<<, and hashable viaAbslHashValue. Works inbase::MakeFixedFlatSet,base::MakeFixedFlatMap, andabsl::flat_hash_map. - GN targets:
//base/i18n:language_tag: CoreLanguageTag,tag_converters,bcp47_extensions,language_tag_list_helper,language_tag_matcher,language_tag_value_converters,legacy_language_tag_helpers,locale_holder, andchinese_helpers. Uses ICU4X locale canonicalization (Rusticu_capi) without depending on ICU4C.//base:i18n: Full i18n target (public_depson//base/i18n:language_tagand//base/i18n/icubridge), addingIcuLocaleConverter,IcuBridge,GetDefaultIcuLocale, and Android locale helpers.
Creating and Parsing LanguageTag
1. Compile-Time Known Literals (GetKnownLanguageTag)
Use base::i18n::GetKnownLanguageTag(std::string_view) (base/i18n/language_tag.h) for compile-time constants. This consteval function validates BCP47 syntax, canonical casing (en lowercase, Latn titlecase, US uppercase, oxendict lowercase), and membership in Chromium's known subtags table (base/i18n/internal/bcp47_known_subtags.h). Extensions (u-, x-) are not supported here.
#include "base/i18n/language_tag.h"
constexpr base::i18n::LanguageTag kDefaultTag =
base::i18n::GetKnownLanguageTag("und");
constexpr base::i18n::LanguageTag kLongKnownTag =
base::i18n::GetKnownLanguageTag("en-Latn-GB-oxendict");2. Runtime String Parsing (GetLanguageTagFromString)
Use base::i18n::GetLanguageTagFromString(std::string_view) (base/i18n/tag_converters.h) rather than calling LanguageTagConverter::GetInstance().FromString(...) directly. It parses and canonicalizes via ICU4X, returning std::optional<LanguageTag>:
- Normalizes casing and separators (
"EN-us"->"en-US","en_US"->"en-US"). - Canonicalizes deprecated tags (
"iw"->"he","cmn"->"zh","tl"->"fil","deu"/"ger"->"de", Windows"zh-chs"->"zh-CN"). Legacy"sh"is intentionally preserved without canonicalization. - Converts legacy ICU keywords and strips POSIX encodings (
"en_US.UTF8@currency=USD"->"en-US-u-cu-usd","ca_ES@valencia"->"ca-ES-u-va-valencia"). - Returns
std::nulloptfor strings shorter than 2 chars, malformed tags, or standalone private-use tags ("x-private";"und-x-private"and"en-x-test"are valid).
#include "base/i18n/tag_converters.h"
std::optional<base::i18n::LanguageTag> tag =
base::i18n::GetLanguageTagFromString(raw_locale);3. Comma-Separated Strings and String Lists
Use base/i18n/language_tag_list_helper.h instead of manually splitting and parsing locale lists (such as accept-languages strings):
GetLanguageTagListFromString(std::string_view[, filter]): Splits on',', trims whitespace, parses each element viaGetLanguageTagFromString, and skips empty or malformed items while preserving order and duplicates.GetLanguageTagListFromStringList(base::span<const std::string>[, filter]): Parses each element viaGetLanguageTagFromString, skipping malformed items.LanguageTagListToString(base::span<const LanguageTag>): Joins tags with','and no whitespace.
Inspecting Subtags, Parent Chains, and Extensions
Subtags and Hierarchy (base/i18n/language_tag.h)
tag_string()->std::string_view(LIFETIME_BOUND): Canonical BCP47 tag.language_subtag()->std::string_view(LIFETIME_BOUND): Base language.WithLanguageSubtagOnly()->LanguageTag: Tag with onlylanguage_subtag().script_subtag()->std::string_view(LIFETIME_BOUND): Script or"".region_subtag()->std::string_view(LIFETIME_BOUND): Region or"".variant_subtags()->std::vector<std::string_view>(LIFETIME_BOUND): Sorted variants.GetParentTag()->std::optional<LanguageTag>(constexpr): Strips the most specific component in order: private-use (x-) -> all non-private extensions -> last variant -> region -> script. Returnsstd::nulloptwhen only the base language remains.GetLineage()->std::vector<LanguageTag>(constexpr): Returns the full parent chain starting with*this(e.g.,"sr-Latn-RS"->["sr-Latn-RS", "sr-Latn", "sr"]).base/i18n/chinese_helpers.h:IsChinese(tag),IsSimplifiedChinese(tag), andIsTraditionalChinese(tag).
Extensions and Private-Use Subtags (base/i18n/bcp47_extensions.h)
Retrieve extensions with GetExtension(...) and produce updated tags (immutably) with WithExtension(...) or WithExtensionRemoved(char key):
- Unicode (
u-):tag.GetExtension(bcp47_extensions::unicode())->std::optional<UnicodeExtension>(has_attribute,AddAttribute,GetKeywordValue,SetKeyword,keywords,GetKeywordKeys). - Private-use (
x-):tag.GetExtension(bcp47_extensions::priv())->std::optional<PrivateUseSubtags>(AddSubtag,SubtagsString). - Other singletons:
tag.GetExtension(bcp47_extensions::ext<'a'>())->std::optional<Extension>(singleton,AddSubtag,SubtagsString).
#include "base/i18n/bcp47_extensions.h"
if (auto u_ext = tag.GetExtension(base::i18n::bcp47_extensions::unicode())) {
u_ext->SetKeyword("ca", "gregory");
tag = tag.WithExtension(*u_ext);
}
base::i18n::LanguageTag no_unicode = tag.WithExtensionRemoved('u');Matching, Thread-Safe Holders, and Chromium Integrations
Language Matching (base/i18n/language_tag_matcher.h)
LanguageTagMatcher precomputes an ICU4X fallback graph (including script and macro-region affinity, e.g., "zh-HK" -> "zh-TW", "es-MX" -> "es-419").
- Construct once and reuse (
LanguageTagMatcher::Createis expensive): LanguageTagMatcher::Create(supported_tags)withMatch(preferred_tag)->std::optional<LanguageTag>andHasExactMatch(preferred_tag).LanguageTagMatcherWithDefault::Create(default_tag, supported_tags)withMatchOrDefault(preferred_tag)->LanguageTag.- For Chromium's built-in locale lists, reuse the cached singletons in
ui/base/l10n/chromium_language_matcher.h:GetAcceptLanguageMatcher(),GetAcceptLanguageTags(),GetPlatformLanguageMatcher(), andGetPlatformLanguageTags().
Thread-Safe Holders and //base:i18n / //ui/base/l10n APIs
base/i18n/locale_holder.h:ThreadSafeLocaleHolder: Lock-protectedGetLocale(),SetLocale(), andRegisterOnLocaleChangedCallback().SequenceCheckedLocaleHolder: Sequence-checkedSetLocale()with lock-protectedGetLocale()readable from any thread.base/i18n/icubridge/default_icu_locale.h:GetDefaultIcuLocale()andRegisterOnDefaultLocaleUpdatedCallback(). Always useGetDefaultIcuLocale()instead oficu::Locale::getDefault(), which must not be used outside ofbase/i18n; if anicu::Localeis needed, convert the returnedLanguageTagwithIcuLocaleConverter::GetInstance().FromLanguageTag(). (SetDefaultIcuLocaleis pass-key restricted viaDefaultIcuLocaleSetterKey.)base/i18n/icubridge/icu_bridge.h:IcuBridge::GetInstance().date_time_formatter(locale)acceptsconst LanguageTag&.base/i18n/icu4c_tag_converter.h:IcuLocaleConverter::GetInstance()providesFromLanguageTag(tag)andToLanguageTag(icu_locale)for legacyicu::Localeboundaries.base/i18n/android_locale.h:GetAndroidDefaultLocale()returnsLanguageTag.ui/base/l10n/l10n_util.h: Prefer theLanguageTagoverloads when available (GetDisplayNameForLocale,GetAcceptLanguageTagsForLocale,IsAcceptLanguageDisplayable,KeepAcceptedLanguages,GetPlatformLocalesForTesting).
Legacy String Interop
LanguageTag::ToLegacyICUFormat()returns underscore format ("en_US","de_DE@calendar=gregorian;collation=phonebook"), ignoring non-Unicode extensions. Avoid calling it for comparisons or internal APIs; keepLanguageTagobjects instead.base/i18n/legacy_language_tag_helpers.h:GetLanguageSubtagUsingLanguageTag(std::string_view).
Mojo (mojo_base.mojom.LanguageTag)
mojo_base.mojom.LanguageTag (mojo/public/mojom/base/language_tag.mojom) is typemapped (default_constructible = false) to ::base::i18n::LanguageTag in both standard Chromium and Blink C++ Mojo bindings:
import "mojo/public/mojom/base/language_tag.mojom";
struct LocaleConfig {
mojo_base.mojom.LanguageTag language;
};- On deserialization (
mojo/public/cpp/base/language_tag_mojom_traits.cc),tag_stringis parsed viaLanguageTagConverter::FromString; empty or malformed tags fail validation and drop the message. - In Blink,
mojo/public/mojom/base/BUILD.gnautomatically pulls inthird_party/blink/renderer/platform/wtf/base_hash_traits.hsoLanguageTagworks as ablink::HashMapkey. For directbase::i18n::LanguageTagusage in Blink, verify the directory is allowlisted inthird_party/blink/tools/blinkpy/presubmit/audit_non_blink_usage.py.
Preferences and base::Value
- Registration (
components/prefs/pref_registry_simple.h): ``cpp registry->RegisterLanguageTagPref( prefs::kPreferredLanguage, base::i18n::GetKnownLanguageTag("en"));`` - Read/Write (
components/prefs/pref_service.h): pref_service->GetLanguageTag(path)(falls back toGetKnownLanguageTag("und")if the stored value is missing/invalid).pref_service->SetLanguageTag(path, tag).- PrefMember (
components/prefs/pref_member_language_tag.h— defined in its own header, separate frompref_member.h, to keeppref_member.hcompile costs low): ```cpp #include "components/prefs/pref_member_language_tag.h"
LanguageTagPrefMember preferred_language_;
- **`base::Value` converters** (`base/i18n/language_tag_value_converters.h`):
`LanguageTagToValue(tag)` and `ValueToLanguageTag(value)`.
`ValueToLanguageTag` parses string values via `GetLanguageTagFromString()`
(canonicalizing inputs such as `"en_US"` -> `"en-US"` and `"iw"` -> `"he"`)
and returns `std::nullopt` for null, non-string, or malformed values.
## Testing Utilities
- `base/i18n/test/locales_for_test.h`: `GetLocalesForTest()` returns a test list
of `LanguageTag`s.
- `base/i18n/test/scoped_icu_locale.h`:
`ScopedDefaultIcuLocale(const LanguageTag&)` overrides and restores
`GetDefaultIcuLocale()` (do not use `icu::Locale::getDefault()` or
`icu::Locale::setDefault()` directly).
- `ui/base/l10n/test/scoped_application_locale.h` and
`ui/base/l10n/test/scoped_resource_bundle_locale.h`: Scoped locale overrides
accepting `const base::i18n::LanguageTag&`.