// This file is part of ICU4X. For terms of use, please see the file // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). //! Tools for locale fallback, enabling arbitrary input locales to be mapped into the nearest //! locale with data. use crate::provider::*; use icu_locale_core::subtags::*; use icu_provider::prelude::*; #[doc(inline)] pub use icu_provider::fallback::{LocaleFallbackConfig, LocaleFallbackPriority}; mod algorithms; /// 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]( /// https://github.com/unicode-org/icu4x/issues/2243) to track alignment with *UTS #35*. /// /// If running fallback in a loop, use [`DataLocale::is_default()`] to break from the loop. /// /// # Examples /// /// ``` /// use icu::locale::fallback::LocaleFallbacker; /// use icu::locale::locale; /// /// // Set up a LocaleFallbacker with data. /// let fallbacker = LocaleFallbacker::new(); /// /// // Create a LocaleFallbackerIterator with a default configuration. /// // By default, uses language priority. /// 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()); /// ``` /// /// [UTS #35: Locale Inheritance and Matching]: https://www.unicode.org/reports/tr35/#Locale_Inheritance /// [the design doc]: https://docs.google.com/document/d/1Mp7EUyl-sFh_HZYgyeVwj88vJGpCBIWxzlCwGgLCDwM/edit #[doc(hidden)] // canonical location in super #[derive(Debug, Clone, PartialEq)] pub struct LocaleFallbacker { likely_subtags: DataPayload, parents: DataPayload, } /// Borrowed version of [`LocaleFallbacker`]. #[derive(Debug, Clone, Copy, PartialEq)] pub struct LocaleFallbackerBorrowed<'a> { likely_subtags: &'a LikelySubtagsForLanguage<'a>, parents: &'a Parents<'a>, } /// A [`LocaleFallbackerBorrowed`] with an associated [`LocaleFallbackConfig`]. #[derive(Debug, Clone, Copy, PartialEq)] pub struct LocaleFallbackerWithConfig<'a> { likely_subtags: &'a LikelySubtagsForLanguage<'a>, parents: &'a Parents<'a>, config: LocaleFallbackConfig, } /// Inner iteration type. Does not own the item under fallback. #[derive(Debug)] struct LocaleFallbackIteratorInner<'a> { likely_subtags: &'a LikelySubtagsForLanguage<'a>, parents: &'a Parents<'a>, config: LocaleFallbackConfig, backup_subdivision: Option, backup_variant: Option, backup_region: Option, max_script: Option