Cldr.Locale.Match (Cldr v2.44.0)
View SourceImplements the CLDR language matching algorithm.
Summary
Functions
Find the desired locale that is the best suported match.
Return a match distance between a desired locale and a supported locale.
Functions
Find the desired locale that is the best suported match.
Arguments
desiredis any valid locale name or list of locale names returned byCldr.known_locale_names/1or a string or atom locale name.optionsis a keyword list of options.
Options
:backendis any module that includesuse Cldrand therefore is aCldrbackend module. The default isCldr.default_backend!/0.:supportedis a list of locale names that are supported by the application. The default isCldr.known_locale_names/1.:thresholdfilters the returned list to those locales that score below this limit. The default is 50.
Returns
- A possibly empty list of
{supported_locale, numeric_score}tuples sorted in ascending numeric score order. The head of the list is considered the best match fordesiredinsupported.
Examples
iex> Cldr.Locale.Match.best_match "zh-HK",
...> supported: ["zh", "zh-Hans", "zh-Hant", "en", "fr", "en-Hant"]
{:ok, "zh-Hant", 5}
iex> supported = Cldr.known_gettext_locale_names()
["bg_BG", "en", "en_GB", "es", "it"]
iex> Cldr.Locale.Match.best_match("en-GB", supported: supported)
{:ok, "en_GB", 0}
iex> Cldr.Locale.Match.best_match("zh-HK", supported: supported)
{:error, {Cldr.NoMatchingLocale, "No match for desired locales \"zh-HK\""}}
Return a match distance between a desired locale and a supported locale.
Arguments
desiredis any valid locale returned byCldr.known_locale_names/1or a string or atom locale name.supportedis any valid locale returned byCldr.known_locale_names/1or a string or atom locale name.backendis any module that includesuse Cldrand therefore is aCldrbackend module. The default isCldr.default_backend!/0.
Returns
- A numeric score indicating how well the supported locale can represent the desired locale. A smaller number is better with a value under 10 being a good fit and a number over 50 being a poor fit.
Example
iex> Cldr.Locale.Match.match_distance("en", "en")
0
iex> Cldr.Locale.Match.match_distance("en-AU", "en")
5
iex> Cldr.Locale.Match.match_distance("en-AU", "en-GB")
3
iex> Cldr.Locale.Match.match_distance("zh-HK", "zh-Hant")
5
iex> Cldr.Locale.Match.match_distance("en", "zh-Hans")
134