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

source

pub fn new() -> Self

Creates an instance of LanguageMatcher.

source

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.

source

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.

Trait Implementations§

source§

impl Default for LanguageMatcher

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ErasedDestructor for Twhere T: 'static,

source§

impl<T> MaybeSendSync for Twhere T: Send + Sync,