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

LocaleExpander supports the minimize and maximize likely subtags algorithms as described in UTS #35: Unicode LDML 3. Likely Subtags.

The maximize method potentially updates a passed in locale in place depending up the results of running the ‘Add Likely Subtags’ algorithm from UTS #35: Unicode LDML 3. Likely Subtags.

This minimize method returns a new Locale that is the result of running the ‘Remove Likely Subtags’ algorithm from UTS #35: Unicode LDML 3. Likely Subtags.

Examples

Add likely subtags:

use icu_locid::locale;
use icu_locid_transform::{LocaleExpander, TransformResult};

let lc = LocaleExpander::try_new_unstable(&icu_testdata::unstable())
    .expect("create failed");

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::try_new_unstable(&icu_testdata::unstable())
    .expect("create failed");

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::try_new_extended_unstable(&icu_testdata::unstable())
        .expect("create failed");

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 fn try_new_unstable<P>( provider: &P ) -> Result<LocaleExpander, LocaleTransformError>where P: DataProvider<LikelySubtagsForLanguageV1Marker> + DataProvider<LikelySubtagsForScriptRegionV1Marker> + ?Sized,

Creates a LocaleExpander with data for CLDR locales with Basic or higher coverage.

Use this contructor if you are using likely subtags for data lookup.

📚 Help choosing a constructor

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

pub fn try_new_extended_unstable<P>( provider: &P ) -> Result<LocaleExpander, LocaleTransformError>where P: DataProvider<LikelySubtagsForLanguageV1Marker> + DataProvider<LikelySubtagsForScriptRegionV1Marker> + DataProvider<LikelySubtagsExtendedV1Marker> + ?Sized,

Creates a LocaleExpander with data for all locales.

Use this constructor if you are using likely subtags for comprehensive support of all languages and regions, including ones that may not have CLDR data.

📚 Help choosing a constructor

⚠️ The bounds on this function 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>

Creates a new instance using an AnyProvider.

For details on the behavior of this function, see: Self::try_new_extended_unstable

📚 Help choosing a constructor

source

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

Enabled with the "serde" feature.

Creates a new instance using a BufferProvider.

For details on the behavior of this function, see: Self::try_new_extended_unstable

📚 Help choosing a constructor

source

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

Creates a new instance using an AnyProvider.

For details on the behavior of this function, see: Self::try_new_unstable

📚 Help choosing a constructor

source

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

Enabled with the "serde" feature.

Creates a new instance using a BufferProvider.

For details on the behavior of this function, see: Self::try_new_unstable

📚 Help choosing a constructor

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.

Examples
use icu_locid::locale;
use icu_locid_transform::{LocaleExpander, TransformResult};

let lc = LocaleExpander::try_new_unstable(&icu_testdata::unstable())
    .expect("create failed");

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"));
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::try_new_unstable(&icu_testdata::unstable())
    .expect("creation failed");

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"));

Trait Implementations§

source§

impl Debug for LocaleExpander

source§

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

Formats the value using the given formatter. 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 T