Struct icu_locid_transform::LocaleExpander

source ·
pub struct LocaleExpander { /* private fields */ }
Expand description

Implements the Add Likely Subtags and Remove Likely Subtags algorithms as defined in UTS #35: Likely Subtags.

§Examples

Add likely subtags:

use icu::locid::locale;
use icu::locid_transform::{LocaleExpander, TransformResult};

let lc = LocaleExpander::new();

let mut locale = locale!("zh-CN");
assert_eq!(lc.maximize(&mut locale), TransformResult::Modified);
assert_eq!(locale, locale!("zh-Hans-CN"));

let mut locale = locale!("zh-Hant-TW");
assert_eq!(lc.maximize(&mut locale), TransformResult::Unmodified);
assert_eq!(locale, locale!("zh-Hant-TW"));

Remove likely subtags:

use icu::locid::locale;
use icu::locid_transform::{LocaleExpander, TransformResult};

let lc = LocaleExpander::new();

let mut locale = locale!("zh-Hans-CN");
assert_eq!(lc.minimize(&mut locale), TransformResult::Modified);
assert_eq!(locale, locale!("zh"));

let mut locale = locale!("zh");
assert_eq!(lc.minimize(&mut locale), TransformResult::Unmodified);
assert_eq!(locale, locale!("zh"));

Normally, only CLDR locales with Basic or higher coverage are included. To include more locales for maximization, use try_new_extended:

use icu::locid::locale;
use icu::locid_transform::{LocaleExpander, TransformResult};

let lc = LocaleExpander::new_extended();

let mut locale = locale!("atj");
assert_eq!(lc.maximize(&mut locale), TransformResult::Modified);
assert_eq!(locale, locale!("atj-Latn-CA"));

Implementations§

source§

impl LocaleExpander

source

pub const fn new() -> Self

Creates a LocaleExpander with compiled data for commonly-used locales (locales with Basic or higher CLDR coverage).

Use this constructor if you want limited likely subtags for data-oriented use cases.

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

source

pub const fn new_extended() -> Self

Creates a LocaleExpander with compiled data for all locales.

Use this constructor if you want to include data for all locales, including ones that may not have data for other services (i.e. CLDR coverage below Basic).

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

source

pub fn try_new_extended_unstable<P>( provider: &P ) -> Result<LocaleExpander, LocaleTransformError>

A version of Self::new_extended that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
source

pub fn try_new_extended_with_any_provider( provider: &(impl AnyProvider + ?Sized) ) -> Result<Self, LocaleTransformError>

A version of Self::new_extended that uses custom data provided by an AnyProvider.

📚 Help choosing a constructor

source

pub fn try_new_extended_with_buffer_provider( provider: &(impl BufferProvider + ?Sized) ) -> Result<Self, LocaleTransformError>

A version of Self::new_extended that uses custom data provided by a BufferProvider.

Enabled with the serde feature.

📚 Help choosing a constructor

source

pub fn try_new_with_any_provider( provider: &(impl AnyProvider + ?Sized) ) -> Result<LocaleExpander, LocaleTransformError>

A version of Self::new that uses custom data provided by an AnyProvider.

📚 Help choosing a constructor

source

pub fn try_new_with_buffer_provider( provider: &(impl BufferProvider + ?Sized) ) -> Result<LocaleExpander, LocaleTransformError>

A version of Self::new that uses custom data provided by a BufferProvider.

Enabled with the serde feature.

📚 Help choosing a constructor

source

pub fn try_new_unstable<P>( provider: &P ) -> Result<LocaleExpander, LocaleTransformError>

A version of Self::new that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
source

pub fn maximize<T: AsMut<LanguageIdentifier>>( &self, langid: T ) -> TransformResult

The maximize method potentially updates a passed in locale in place depending up the results of running the ‘Add Likely Subtags’ algorithm from https://www.unicode.org/reports/tr35/#Likely_Subtags.

If the result of running the algorithm would result in a new locale, the locale argument is updated in place to match the result, and the method returns TransformResult::Modified. Otherwise, the method returns TransformResult::Unmodified and the locale argument is unchanged.

This function does not guarantee that any particular set of subtags will be present in the resulting locale.

§Examples
use icu::locid::locale;
use icu::locid_transform::{LocaleExpander, TransformResult};

let lc = LocaleExpander::new();

let mut locale = locale!("zh-CN");
assert_eq!(lc.maximize(&mut locale), TransformResult::Modified);
assert_eq!(locale, locale!("zh-Hans-CN"));

let mut locale = locale!("zh-Hant-TW");
assert_eq!(lc.maximize(&mut locale), TransformResult::Unmodified);
assert_eq!(locale, locale!("zh-Hant-TW"));

If there is no data for a particular language, the result is not modified. Note that LocaleExpander::new_extended supports more languages.

use icu::locid::locale;
use icu::locid_transform::{LocaleExpander, TransformResult};

let lc = LocaleExpander::new();

// No subtags data for ccp in the default set:
let mut locale = locale!("ccp");
assert_eq!(lc.maximize(&mut locale), TransformResult::Unmodified);
assert_eq!(locale, locale!("ccp"));

// The extended set supports it:
let lc = LocaleExpander::new_extended();
let mut locale = locale!("ccp");
assert_eq!(lc.maximize(&mut locale), TransformResult::Modified);
assert_eq!(locale, locale!("ccp-Cakm-BD"));

// But even the extended set does not support all language subtags:
let mut locale = locale!("mul");
assert_eq!(lc.maximize(&mut locale), TransformResult::Unmodified);
assert_eq!(locale, locale!("mul"));
source

pub fn minimize<T: AsMut<LanguageIdentifier>>( &self, langid: T ) -> TransformResult

This returns a new Locale that is the result of running the ‘Remove Likely Subtags’ algorithm from https://www.unicode.org/reports/tr35/#Likely_Subtags.

If the result of running the algorithm would result in a new locale, the locale argument is updated in place to match the result, and the method returns TransformResult::Modified. Otherwise, the method returns TransformResult::Unmodified and the locale argument is unchanged.

§Examples
use icu::locid::locale;
use icu::locid_transform::{LocaleExpander, TransformResult};

let lc = LocaleExpander::new();

let mut locale = locale!("zh-Hans-CN");
assert_eq!(lc.minimize(&mut locale), TransformResult::Modified);
assert_eq!(locale, locale!("zh"));

let mut locale = locale!("zh");
assert_eq!(lc.minimize(&mut locale), TransformResult::Unmodified);
assert_eq!(locale, locale!("zh"));
source

pub fn minimize_favor_script<T: AsMut<LanguageIdentifier>>( &self, langid: T ) -> TransformResult

This returns a new Locale that is the result of running the ‘Remove Likely Subtags, favoring script’ algorithm from https://www.unicode.org/reports/tr35/#Likely_Subtags.

If the result of running the algorithm would result in a new locale, the locale argument is updated in place to match the result, and the method returns TransformResult::Modified. Otherwise, the method returns TransformResult::Unmodified and the locale argument is unchanged.

§Examples
use icu::locid::locale;
use icu::locid_transform::{LocaleExpander, TransformResult};

let lc = LocaleExpander::new();

let mut locale = locale!("zh_TW");
assert_eq!(
    lc.minimize_favor_script(&mut locale),
    TransformResult::Modified
);
assert_eq!(locale, locale!("zh_Hant"));

Trait Implementations§

source§

impl Clone for LocaleExpander

source§

fn clone(&self) -> LocaleExpander

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for LocaleExpander

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for LocaleExpander

source§

fn default() -> Self

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

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

source§

impl<T> MaybeSendSync for T