Locale constructor
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.
Implementation
factory Locale(String language, [String? country, String? variant]) => _Locale(language, country, variant);