icu_locid_transform

Struct LocaleFallbacker

Source
pub struct LocaleFallbacker { /* private fields */ }
Expand description

Implements the algorithm defined in UTS #35: Locale Inheritance and Matching.

Note that this implementation performs some additional steps compared to the UTS #35 algorithm. See the design doc for a detailed description and #2243 to track alignment with UTS #35.

If running fallback in a loop, use DataLocale::is_und() to break from the loop.

ยงExamples

use icu::locid::locale;
use icu::locid_transform::fallback::LocaleFallbacker;

// Set up a LocaleFallbacker with data.
let fallbacker = LocaleFallbacker::new();

// Create a LocaleFallbackerIterator with a default configuration.
// By default, uses language priority with no additional extension keywords.
let mut fallback_iterator = fallbacker
    .for_config(Default::default())
    .fallback_for(locale!("hi-Latn-IN").into());

// Run the algorithm and check the results.
assert_eq!(fallback_iterator.get(), &locale!("hi-Latn-IN").into());
fallback_iterator.step();
assert_eq!(fallback_iterator.get(), &locale!("hi-Latn").into());
fallback_iterator.step();
assert_eq!(fallback_iterator.get(), &locale!("en-IN").into());
fallback_iterator.step();
assert_eq!(fallback_iterator.get(), &locale!("en-001").into());
fallback_iterator.step();
assert_eq!(fallback_iterator.get(), &locale!("en").into());
fallback_iterator.step();
assert_eq!(fallback_iterator.get(), &locale!("und").into());

Implementationsยง

Sourceยง

impl LocaleFallbacker

Source

pub const fn new<'a>() -> LocaleFallbackerBorrowed<'a>

Creates a LocaleFallbacker with compiled fallback data (likely subtags and parent locales).

โœจ Enabled with the compiled_data Cargo feature.

๐Ÿ“š Help choosing a constructor

Source

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

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

๐Ÿ“š Help choosing a constructor

Source

pub fn try_new_unstable<P>(provider: &P) -> Result<Self, DataError>

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 new_without_data() -> Self

Creates a LocaleFallbacker without fallback data. Using this constructor may result in surprising behavior, especially in multi-script languages.

Source

pub fn for_config( &self, config: LocaleFallbackConfig, ) -> LocaleFallbackerWithConfig<'_>

Associates a configuration with this fallbacker.

Source

pub fn as_borrowed(&self) -> LocaleFallbackerBorrowed<'_>

Creates a borrowed version of this fallbacker for performance.