Struct language_matcher::LanguageMatcher
source · pub struct LanguageMatcher { /* private fields */ }
Expand description
This is a language matcher. The distance of two languages are calculated by the algorithm of CLDR. The value of distance is multiplied by 10, because we need to consider the paradigm locales.
Examples
use icu_locid::langid;
use language_matcher::LanguageMatcher;
let matcher = LanguageMatcher::new();
assert_eq!(matcher.distance(langid!("zh-CN"), langid!("zh-Hans")), 0);
assert_eq!(matcher.distance(langid!("zh-HK"), langid!("zh-MO")), 40);
assert_eq!(matcher.distance(langid!("en-US"), langid!("en-GB")), 50);
assert_eq!(matcher.distance(langid!("en-US"), langid!("en-CA")), 39);
With the distance, you can choose the nearst language from a set of languages:
use icu_locid::langid;
use language_matcher::LanguageMatcher;
let matcher = LanguageMatcher::new();
let accepts = [
langid!("en"),
langid!("ja"),
langid!("zh-Hans"),
langid!("zh-Hant"),
];
assert_eq!(matcher.matches(langid!("zh-CN"), &accepts),Some((&langid!("zh-Hans"), 0)));
Implementations§
source§impl LanguageMatcher
impl LanguageMatcher
sourcepub fn new() -> Self
pub fn new() -> Self
Creates an instance of LanguageMatcher
.
sourcepub fn matches<L: AsRef<LanguageIdentifier>>(
&self,
desired: LanguageIdentifier,
supported: impl IntoIterator<Item = L>
) -> Option<(L, u16)>
pub fn matches<L: AsRef<LanguageIdentifier>>( &self, desired: LanguageIdentifier, supported: impl IntoIterator<Item = L> ) -> Option<(L, u16)>
Choose the nearst language of desired language from the supported language collection. Returns the chosen language and the distance.
None
will be returned if no language gives the distance less than 1000.
That usually means no language matches the desired one.
sourcepub fn distance(
&self,
desired: LanguageIdentifier,
supported: LanguageIdentifier
) -> u16
pub fn distance( &self, desired: LanguageIdentifier, supported: LanguageIdentifier ) -> u16
Calculate the distance of the two language. Some rule in CLDR is one way. Be careful about the parameters order.
The return value is multiplied by 10, and if only one is paradigm locale, the value is substructed by 1.