Struct icu::locale::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::locale::locale;
use icu::locale::{LocaleExpander, TransformResult};
let lc = LocaleExpander::new();
let mut locale = locale!("zh-CN");
assert_eq!(lc.maximize(&mut locale.id), TransformResult::Modified);
assert_eq!(locale, locale!("zh-Hans-CN"));
let mut locale = locale!("zh-Hant-TW");
assert_eq!(lc.maximize(&mut locale.id), TransformResult::Unmodified);
assert_eq!(locale, locale!("zh-Hant-TW"));
Remove likely subtags:
use icu::locale::{locale, LocaleExpander, TransformResult};
let lc = LocaleExpander::new();
let mut locale = locale!("zh-Hans-CN");
assert_eq!(lc.minimize(&mut locale.id), TransformResult::Modified);
assert_eq!(locale, locale!("zh"));
let mut locale = locale!("zh");
assert_eq!(lc.minimize(&mut locale.id), 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::locale::{locale, LocaleExpander, TransformResult};
let lc = LocaleExpander::new_extended();
let mut locale = locale!("atj");
assert_eq!(lc.maximize(&mut locale.id), TransformResult::Modified);
assert_eq!(locale, locale!("atj-Latn-CA"));
Implementations§
source§impl LocaleExpander
impl LocaleExpander
sourcepub const fn new() -> LocaleExpander
pub const fn new() -> LocaleExpander
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.
sourcepub fn try_new_with_any_provider(
provider: &(impl AnyProvider + ?Sized),
) -> Result<LocaleExpander, DataError>
pub fn try_new_with_any_provider( provider: &(impl AnyProvider + ?Sized), ) -> Result<LocaleExpander, DataError>
A version of [Self :: new
] that uses custom data provided by an AnyProvider
.
sourcepub fn try_new_with_buffer_provider(
provider: &(impl BufferProvider + ?Sized),
) -> Result<LocaleExpander, DataError>
pub fn try_new_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), ) -> Result<LocaleExpander, DataError>
A version of [Self :: new
] that uses custom data provided by a BufferProvider
.
✨ Enabled with the serde
feature.
sourcepub fn try_new_unstable<P>(provider: &P) -> Result<LocaleExpander, DataError>
pub fn try_new_unstable<P>(provider: &P) -> Result<LocaleExpander, DataError>
A version of Self::new
that uses custom data provided by a DataProvider
.
sourcepub const fn new_extended() -> LocaleExpander
pub const fn new_extended() -> LocaleExpander
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.
sourcepub fn try_new_extended_with_any_provider(
provider: &(impl AnyProvider + ?Sized),
) -> Result<LocaleExpander, DataError>
pub fn try_new_extended_with_any_provider( provider: &(impl AnyProvider + ?Sized), ) -> Result<LocaleExpander, DataError>
A version of [Self :: new_extended
] that uses custom data provided by an AnyProvider
.
sourcepub fn try_new_extended_with_buffer_provider(
provider: &(impl BufferProvider + ?Sized),
) -> Result<LocaleExpander, DataError>
pub fn try_new_extended_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), ) -> Result<LocaleExpander, DataError>
A version of [Self :: new_extended
] that uses custom data provided by a BufferProvider
.
✨ Enabled with the serde
feature.
sourcepub fn try_new_extended_unstable<P>(
provider: &P,
) -> Result<LocaleExpander, DataError>
pub fn try_new_extended_unstable<P>( provider: &P, ) -> Result<LocaleExpander, DataError>
A version of Self::new_extended
that uses custom data provided by a DataProvider
.
sourcepub fn maximize(&self, langid: &mut LanguageIdentifier) -> TransformResult
pub fn maximize(&self, langid: &mut LanguageIdentifier) -> 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::locale::{locale, LocaleExpander, TransformResult};
let lc = LocaleExpander::new();
let mut locale = locale!("zh-CN");
assert_eq!(lc.maximize(&mut locale.id), TransformResult::Modified);
assert_eq!(locale, locale!("zh-Hans-CN"));
let mut locale = locale!("zh-Hant-TW");
assert_eq!(lc.maximize(&mut locale.id), 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::locale::{locale, 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.id), 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.id), 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.id), TransformResult::Unmodified);
assert_eq!(locale, locale!("mul"));
sourcepub fn minimize(&self, langid: &mut LanguageIdentifier) -> TransformResult
pub fn minimize(&self, langid: &mut LanguageIdentifier) -> 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::locale::{locale, LocaleExpander, TransformResult};
let lc = LocaleExpander::new();
let mut locale = locale!("zh-Hans-CN");
assert_eq!(lc.minimize(&mut locale.id), TransformResult::Modified);
assert_eq!(locale, locale!("zh"));
let mut locale = locale!("zh");
assert_eq!(lc.minimize(&mut locale.id), TransformResult::Unmodified);
assert_eq!(locale, locale!("zh"));
sourcepub fn minimize_favor_script(
&self,
langid: &mut LanguageIdentifier,
) -> TransformResult
pub fn minimize_favor_script( &self, langid: &mut LanguageIdentifier, ) -> 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::locale::{locale, LocaleExpander, TransformResult};
let lc = LocaleExpander::new();
let mut locale = locale!("zh_TW");
assert_eq!(
lc.minimize_favor_script(&mut locale.id),
TransformResult::Modified
);
assert_eq!(locale, locale!("zh_Hant"));
Trait Implementations§
source§impl AsRef<LocaleExpander> for LocaleExpander
impl AsRef<LocaleExpander> for LocaleExpander
source§fn as_ref(&self) -> &LocaleExpander
fn as_ref(&self) -> &LocaleExpander
source§impl Clone for LocaleExpander
impl Clone for LocaleExpander
source§fn clone(&self) -> LocaleExpander
fn clone(&self) -> LocaleExpander
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for LocaleExpander
impl Debug for LocaleExpander
source§impl Default for LocaleExpander
impl Default for LocaleExpander
source§fn default() -> LocaleExpander
fn default() -> LocaleExpander
Auto Trait Implementations§
impl Freeze for LocaleExpander
impl RefUnwindSafe for LocaleExpander
impl Send for LocaleExpander
impl Sync for LocaleExpander
impl Unpin for LocaleExpander
impl UnwindSafe for LocaleExpander
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more