LocaleMatcher class Locales
Matches supported locales to the desired locales.
The class caches information for supported locales so it only needs to be calculated once for multiple getBestMatch or isMatch operations.
The matcher uses the CLDR information (see: https://unicode.org/reports/tr35/#LanguageMatching) to determine which LanguageIds are "close" to each other.
If the distance is the same based on the CLDR information, it will favor supported locales which has a similar number of fields set as the desired locale.
If this is till the same it will favor supported locales which are closer to the "default" locale for a language.
Both LanguageIds and LocaleIds or a mix can be used.
Flutter usage
This class can be used to provide an improved localeListResolutionCallback
which can be set during construction of MaterialApp
:
...
localeListResolutionCallback: (desiredLocales, supportedLocales) {
if (desiredLocales == null || desiredLocales.isEmpty) return null;
var languageId = LocaleMatcher(supportedLocales
.map((l) => LanguageId.parse(l.toLanguageTag())))
.getBestMatch(
desiredLocales.map((l) => LanguageId.parse(l.toLanguageTag())))
.supportedLocale;
if (languageId == null || languageId.lang == null) return null;
return supportedLocales.firstWhere((e) =>
e.languageCode == languageId.lang &&
e.scriptCode == languageId.script &&
e.countryCode == languageId.region);
},
...
Constructors
-
LocaleMatcher(Iterable<
LanguageId> supportedLocales, {bool noDefaultLocale = false, LanguageId? defaultLocale, int? threshold, LanguageId? desiredForThreshold, LanguageId? supportedForThreshold}) -
Create a new LocaleMatcher with specified
supportedLocales
.factory
Properties
- defaultLocale → LanguageId?
-
Locale which will be tried first and returned if no matches occur.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
-
orderedMaximizedSupportedLocales
→ List<
LanguageId> -
The supported locales in the order that they will be matched,
with likely sub tags added (maximized form).
final
-
orderedSupportedLocales
→ List<
LanguageId> -
The supported locales in the order that they will be matched.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- threshold → int
-
The threshold for matching;
any matches with distance bigger then the threshold will be ignored.
final
Methods
-
getBestMatch(
Iterable< LanguageId> desiredLocales, {bool favorScript = false, bool favorEarlier = true, bool ignoreFallback = false}) → MatchResult -
Get the best match from the
desiredLocales
out of thesupportedLocales
. -
isMatch(
LanguageId desiredLocale, LanguageId supportedLocale, {bool favorScript = false, bool ignoreFallback = false}) → bool -
Check if the
desiredLocale
matches thesupportedLocale
. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
getBestCommonLocaleData(
Iterable< String> desiredLocales, Iterable<CommonLocaleData> supportedLocales, {bool noDefaultLocale = false}) → CommonLocaleData? -
Helper method to select the
desiredLocales
from a list ofsupportedLocales
s, where thesupportedLocales
are CommonLocaleData.