Interface LocaleMatcher
- All Known Subinterfaces:
Strings
Locale or List<Locale.LanguageRange> to an appropriate localized strings Locale.
Lokalized's implementation prefers exact and CLDR-canonical matches, then CLDR parent-locale fallback,
then script-aware likely-subtag matches. If multiple supported localized strings files still share the same language,
configured tiebreakers determine which locale wins. Unmatched, root, and undetermined requests resolve to the
configured fallback locale when using bestMatchFor(...). The strict matchFor(...) methods represent
the same state as an unmatched LocaleMatchResult instead of manufacturing a match.
Non-bare wildcard language ranges use RFC 4647 extended filtering only; they are not broadened through
CLDR, likely-subtag, or primary-language heuristics, and successful results report
LocaleMatchType.EXTENDED_RANGE. When multiple ranges match one supported locale, the most-specific exact,
canonical, or structural range determines its effective quality, including a q=0 exclusion.
- Author:
- Mark Allen
-
Field Summary
FieldsModifier and TypeFieldDescriptionMaximum number of parsed language ranges accepted by one matching operation: 32. -
Method Summary
Modifier and TypeMethodDescriptionbestMatchFor(@NonNull List<@NonNull Locale.LanguageRange> languageRanges) Given a list of language ranges (e.g. as parsed from anAccept-LanguageHTTP request header), determine the best-matching localized strings file's locale.bestMatchFor(@NonNull Locale locale) Given a locale, determine the best-matching localized strings file's locale.bestMatchForAcceptLanguage(@Nullable String acceptLanguage) Given a rawAccept-LanguageHTTP field value, determines the best-matching localized strings file's locale.matchFor(@NonNull List<@NonNull Locale.LanguageRange> languageRanges) Strictly negotiates language ranges without manufacturing a configured-fallback match.default @NonNull LocaleMatchResultStrictly negotiates a locale without manufacturing a configured-fallback match.
-
Field Details
-
MAXIMUM_LANGUAGE_RANGES
Maximum number of parsed language ranges accepted by one matching operation: 32.Locale.LanguageRange.parse(String)may add IANA-equivalent ranges, so this limit applies to the returned list, not the number of comma-separated ranges in the source header.- Since:
- 3.0.0
-
-
Method Details
-
matchFor
Strictly negotiates a locale without manufacturing a configured-fallback match.- Parameters:
locale- requested locale, not null- Returns:
- diagnostic match result, not null
- Throws:
IllegalArgumentException- if the locale is not well-formed- Since:
- 3.0.0
-
matchFor
Strictly negotiates language ranges without manufacturing a configured-fallback match.- Parameters:
languageRanges- requested language ranges, not null- Returns:
- diagnostic match result, not null
- Throws:
IllegalArgumentException- if more thanMAXIMUM_LANGUAGE_RANGESlanguage ranges are supplied- Since:
- 3.0.0
-
bestMatchFor
Given a locale, determine the best-matching localized strings file's locale.- Parameters:
locale- the locale for which to find the best match.- Returns:
- the best-matching locale, not null
- Throws:
IllegalArgumentException- if the locale is not well-formed
-
bestMatchFor
Given a list of language ranges (e.g. as parsed from anAccept-LanguageHTTP request header), determine the best-matching localized strings file's locale.- Parameters:
languageRanges- the ordered list of language ranges for which to find the best match.- Returns:
- the best-matching locale, not null
- Throws:
IllegalArgumentException- if more thanMAXIMUM_LANGUAGE_RANGESlanguage ranges are supplied
-
bestMatchForAcceptLanguage
Given a rawAccept-LanguageHTTP field value, determines the best-matching localized strings file's locale.This is a fail-soft convenience for request handling. A missing, blank, malformed, or longer than 4,096 UTF-16 code-unit value returns the configured fallback locale. The length limit is applied before parsing so parser work is bounded independently of the parsed-range limit. HTTP optional whitespace and empty list elements are normalized before parsing. The configured fallback is also returned if parsing produces more than
MAXIMUM_LANGUAGE_RANGESranges, which can happen whenLocale.LanguageRange.parse(String)adds IANA-equivalent ranges. A valid parsed list is passed through whole; preferences are never truncated. UsematchFor(List)orbestMatchFor(List)when language ranges have already been parsed and strict limit enforcement is desired.- Parameters:
acceptLanguage- raw, already-combinedAccept-Languagefield value, or null if absent- Returns:
- the best-matching locale, or the configured fallback for unusable input, not null
- Since:
- 3.0.0
-