README.md
1# icu_locale [](https://crates.io/crates/icu_locale)
2
3<!-- cargo-rdme start -->
4
5Canonicalization of locale identifiers based on [`CLDR`] data.
6
7This module is published as its own crate ([`icu_locale`](https://docs.rs/icu_locale/latest/icu_locale/))
8and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project.
9
10It currently supports locale canonicalization based upon the canonicalization
11algorithm from [`UTS #35: Unicode LDML 3. LocaleId Canonicalization`],
12as well as the minimize and maximize likely subtags algorithms
13as described in [`UTS #35: Unicode LDML 3. Likely Subtags`].
14
15The maximize method potentially updates a passed in locale in place
16depending up the results of running the 'Add Likely Subtags' algorithm
17from [`UTS #35: Unicode LDML 3. Likely Subtags`].
18
19This minimize method returns a new Locale that is the result of running the
20'Remove Likely Subtags' algorithm from [`UTS #35: Unicode LDML 3. Likely Subtags`].
21
22## Examples
23
24```rust
25use icu::locale::Locale;
26use icu::locale::{LocaleCanonicalizer, TransformResult};
27
28let lc = LocaleCanonicalizer::new_extended();
29
30let mut locale: Locale = "ja-Latn-fonipa-hepburn-heploc"
31 .parse()
32 .expect("parse failed");
33assert_eq!(lc.canonicalize(&mut locale), TransformResult::Modified);
34assert_eq!(locale, "ja-Latn-alalc97-fonipa".parse::<Locale>().unwrap());
35```
36
37```rust
38use icu::locale::{locale, LocaleExpander, TransformResult};
39
40let lc = LocaleExpander::new_common();
41
42let mut locale = locale!("zh-CN");
43assert_eq!(lc.maximize(&mut locale.id), TransformResult::Modified);
44assert_eq!(locale, locale!("zh-Hans-CN"));
45
46let mut locale = locale!("zh-Hant-TW");
47assert_eq!(lc.maximize(&mut locale.id), TransformResult::Unmodified);
48assert_eq!(locale, locale!("zh-Hant-TW"));
49```
50
51```rust
52use icu::locale::{locale, LocaleExpander, TransformResult};
53use writeable::assert_writeable_eq;
54
55let lc = LocaleExpander::new_common();
56
57let mut locale = locale!("zh-Hans-CN");
58assert_eq!(lc.minimize(&mut locale.id), TransformResult::Modified);
59assert_eq!(locale, locale!("zh"));
60
61let mut locale = locale!("zh");
62assert_eq!(lc.minimize(&mut locale.id), TransformResult::Unmodified);
63assert_eq!(locale, locale!("zh"));
64```
65
66[`ICU4X`]: ../icu/index.html
67[`CLDR`]: http://cldr.unicode.org/
68[`UTS #35: Unicode LDML 3. Likely Subtags`]: https://www.unicode.org/reports/tr35/#Likely_Subtags.
69[`UTS #35: Unicode LDML 3. LocaleId Canonicalization`]: http://unicode.org/reports/tr35/#LocaleId_Canonicalization,
70
71<!-- cargo-rdme end -->
72
73## More Information
74
75For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x).
76