| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
// https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations |
| 6 |
#![cfg_attr(not(any(test, doc)), no_std)] |
| 7 |
#![cfg_attr( |
| 8 |
not(test), |
| 9 |
deny( |
| 10 |
clippy::indexing_slicing, |
| 11 |
clippy::unwrap_used, |
| 12 |
clippy::expect_used, |
| 13 |
clippy::panic, |
| 14 |
) |
| 15 |
)] |
| 16 |
#![warn(missing_docs)] |
| 17 |
|
| 18 |
//! Canonicalization of locale identifiers based on [`CLDR`] data. |
| 19 |
//! |
| 20 |
//! This module is published as its own crate ([`icu_locale`](https://docs.rs/icu_locale/latest/icu_locale/)) |
| 21 |
//! and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project. |
| 22 |
//! |
| 23 |
//! It currently supports locale canonicalization based upon the canonicalization |
| 24 |
//! algorithm from [`UTS #35: Unicode LDML 3. LocaleId Canonicalization`], |
| 25 |
//! as well as the minimize and maximize likely subtags algorithms |
| 26 |
//! as described in [`UTS #35: Unicode LDML 3. Likely Subtags`]. |
| 27 |
//! |
| 28 |
//! The maximize method potentially updates a passed in locale in place |
| 29 |
//! depending up the results of running the 'Add Likely Subtags' algorithm |
| 30 |
//! from [`UTS #35: Unicode LDML 3. Likely Subtags`]. |
| 31 |
//! |
| 32 |
//! This minimize method returns a new Locale that is the result of running the |
| 33 |
//! 'Remove Likely Subtags' algorithm from [`UTS #35: Unicode LDML 3. Likely Subtags`]. |
| 34 |
//! |
| 35 |
//! # Examples |
| 36 |
//! |
| 37 |
//! ``` |
| 38 |
//! use icu::locale::Locale; |
| 39 |
//! use icu::locale::{LocaleCanonicalizer, TransformResult}; |
| 40 |
//! |
| 41 |
//! let lc = LocaleCanonicalizer::new_extended(); |
| 42 |
//! |
| 43 |
//! let mut locale: Locale = "ja-Latn-fonipa-hepburn-heploc" |
| 44 |
//! .parse() |
| 45 |
//! .expect("parse failed"); |
| 46 |
//! assert_eq!(lc.canonicalize(&mut locale), TransformResult::Modified); |
| 47 |
//! assert_eq!(locale, "ja-Latn-alalc97-fonipa".parse::<Locale>().unwrap()); |
| 48 |
//! ``` |
| 49 |
//! |
| 50 |
//! ``` |
| 51 |
//! use icu::locale::{LocaleExpander, TransformResult, locale}; |
| 52 |
//! |
| 53 |
//! let lc = LocaleExpander::new_common(); |
| 54 |
//! |
| 55 |
//! let mut locale = locale!("zh-CN"); |
| 56 |
//! assert_eq!(lc.maximize(&mut locale.id), TransformResult::Modified); |
| 57 |
//! assert_eq!(locale, locale!("zh-Hans-CN")); |
| 58 |
//! |
| 59 |
//! let mut locale = locale!("zh-Hant-TW"); |
| 60 |
//! assert_eq!(lc.maximize(&mut locale.id), TransformResult::Unmodified); |
| 61 |
//! assert_eq!(locale, locale!("zh-Hant-TW")); |
| 62 |
//! ``` |
| 63 |
//! |
| 64 |
//! ``` |
| 65 |
//! use icu::locale::{LocaleExpander, TransformResult, locale}; |
| 66 |
//! use writeable::assert_writeable_eq; |
| 67 |
//! |
| 68 |
//! let lc = LocaleExpander::new_common(); |
| 69 |
//! |
| 70 |
//! let mut locale = locale!("zh-Hans-CN"); |
| 71 |
//! assert_eq!(lc.minimize(&mut locale.id), TransformResult::Modified); |
| 72 |
//! assert_eq!(locale, locale!("zh")); |
| 73 |
//! |
| 74 |
//! let mut locale = locale!("zh"); |
| 75 |
//! assert_eq!(lc.minimize(&mut locale.id), TransformResult::Unmodified); |
| 76 |
//! assert_eq!(locale, locale!("zh")); |
| 77 |
//! ``` |
| 78 |
//! |
| 79 |
//! [`ICU4X`]: ../icu/index.html |
| 80 |
//! [`CLDR`]: https://cldr.unicode.org/ |
| 81 |
//! [`UTS #35: Unicode LDML 3. Likely Subtags`]: https://www.unicode.org/reports/tr35/#Likely_Subtags. |
| 82 |
//! [`UTS #35: Unicode LDML 3. LocaleId Canonicalization`]: https://unicode.org/reports/tr35/#LocaleId_Canonicalization, |
| 83 |
|
| 84 |
extern crate alloc; |
| 85 |
|
| 86 |
mod canonicalizer; |
| 87 |
mod directionality; |
| 88 |
pub mod exemplar_chars; |
| 89 |
mod expander; |
| 90 |
/// Locale fallback algorithm re-exports. |
| 91 |
pub mod fallback { |
| 92 |
#[doc(inline)] |
| 93 |
pub use icu_locale_fallback::*; |
| 94 |
} |
| 95 |
#[cfg(feature = "unstable")] |
| 96 |
pub mod names; |
| 97 |
pub mod provider; |
| 98 |
#[cfg(feature = "unstable")] |
| 99 |
mod size_test_macro; |
| 100 |
|
| 101 |
pub use icu_locale_core::*; |
| 102 |
|
| 103 |
pub use canonicalizer::LocaleCanonicalizer; |
| 104 |
pub use directionality::{Direction, LocaleDirectionality}; |
| 105 |
pub use expander::LocaleExpander; |
| 106 |
#[doc(hidden)] // canonical location in module |
| 107 |
pub use fallback::LocaleFallbacker; |
| 108 |
|
| 109 |
/// Used to track the result of a transformation operation that potentially modifies its argument in place. |
| 110 |
#[derive(Debug, PartialEq)] |
| 111 |
#[allow(clippy::exhaustive_enums)] // this enum is stable |
| 112 |
pub enum TransformResult { |
| 113 |
/// The canonicalization operation modified the locale. |
| 114 |
Modified, |
| 115 |
/// The canonicalization operation did not modify the locale. |
| 116 |
Unmodified, |
| 117 |
} |