Locale class abstract
A representation of a locale, consisting of a language code and optional country and variant codes.
Useful for internationalization (i18n) and localization (l10n),
supporting language tags like "en"
, "en-US"
, or "fr-FR-Paris"
.
The locale follows the pattern: language[-country[-variant]]
.
Usage
Create a locale with just a language:
final locale = Locale('en');
print(locale.getLanguageTag()); // Output: "en"
Create a locale with language and country:
final locale = Locale('en', 'US');
print(locale.getLanguageTag()); // Output: "en-US"
Create a locale with language, country, and variant:
final locale = Locale('fr', 'FR', 'Paris');
print(locale.getLanguageTag()); // Output: "fr-FR-Paris"
Parse a locale from a string:
final locale = Locale.parse('es-MX');
print(locale.getLanguage()); // Output: "es"
print(locale.getCountry()); // Output: "MX"
Validation
The class automatically validates and normalizes input:
- Language codes are converted to lowercase and must be 2-3 characters
- Country codes are converted to uppercase and must be exactly 2 characters
- Variants must contain only word characters and hyphens
Invalid inputs will throw InvalidFormatException.
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
compareTo(
Locale other) → int - Compare locales for sorting.
-
copyWith(
{String? language, String? country, String? variant}) → Locale - Copy this locale with optional replacements.
-
getCountry(
) → String? - Returns the country code of this locale, or null if not specified.
-
getLanguage(
) → String - Returns the language code of this locale.
-
getLanguageTag(
) → String -
Returns the full language tag (
language[-country[-variant]]
). -
getNormalizedCountry(
) → String? - Returns normalized country (uppercase), or null if not specified.
-
getNormalizedLanguage(
) → String - Returns normalized language (lowercase).
-
getNormalizedVariant(
) → String? - Returns normalized variant (uppercase), or null if not specified.
-
getVariant(
) → String? - Returns the variant of this locale, or null if not specified.
-
hasCountry(
) → bool - Returns true if the locale has a country code.
-
hasVariant(
) → bool - Returns true if the locale has a variant.
-
isDefault(
) → bool - Returns true if this is the default locale.
-
matches(
Locale other, {bool ignoreVariant = true}) → bool - Check if this locale matches another (optionally ignoring variant).
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toJson(
) → Map< String, String?> - Convert to map (useful for JSON serialization).
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- DEFAULT_LOCALE ↔ Locale
-
The default locale for the system or application (
en-US
).getter/setter pair