| 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 |
//! Options to define fallback behaviour. |
| 6 |
//! |
| 7 |
//! These options are consumed by the `LocaleFallbacker` in the `icu_locales` crate |
| 8 |
//! (or the `icu::locales` module), but are defined here because they are used by `DataMarkerInfo`. |
| 9 |
|
| 10 |
/// Hint for which subtag to prioritize during fallback. |
| 11 |
/// |
| 12 |
/// For example, `"en-US"` might fall back to either `"en"` or `"und-US"` depending |
| 13 |
/// on this enum. |
| 14 |
#[derive(Debug, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)] |
| 15 |
#[non_exhaustive] |
| 16 |
pub enum LocaleFallbackPriority { |
| 17 |
/// Prioritize the language. This is the default behavior. |
| 18 |
/// |
| 19 |
/// For example, `"en-US"` should go to `"en"` and then `"und"`. |
| 20 |
Language, |
| 21 |
/// Prioritize the script. |
| 22 |
/// |
| 23 |
/// For example, `"en-US"` should go to `"en"` and then `"und-Latn"` and then `"und"`. |
| 24 |
Script, |
| 25 |
/// Prioritize the region. |
| 26 |
/// |
| 27 |
/// For example, `"en-US"` should go to `"und-US"` and then `"und"`. |
| 28 |
/// |
| 29 |
/// This should be used for [data that is region-specific](https://github.com/unicode-org/cldr/blob/main/common/supplemental/rgScope.xml). |
| 30 |
Region, |
| 31 |
} |
| 32 |
|
| 33 |
impl LocaleFallbackPriority { |
| 34 |
/// Const-friendly version of [`Default::default`]. |
| 35 |
pub const fn default() -> Self { |
| 36 |
Self::Language |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
impl Default for LocaleFallbackPriority { |
| 41 |
fn default() -> Self { |
| 42 |
Self::default() |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/// Configuration settings for a particular fallback operation. |
| 47 |
#[derive(Debug, Clone, PartialEq, Eq, Copy)] |
| 48 |
#[non_exhaustive] |
| 49 |
pub struct LocaleFallbackConfig { |
| 50 |
/// Strategy for choosing which subtags to drop during locale fallback. |
| 51 |
/// |
| 52 |
/// # Examples |
| 53 |
/// |
| 54 |
/// Retain the language and script subtags until the final step: |
| 55 |
/// |
| 56 |
/// ``` |
| 57 |
/// use icu::locale::LocaleFallbacker; |
| 58 |
/// use icu::locale::data_locale; |
| 59 |
/// use icu::locale::fallback::LocaleFallbackConfig; |
| 60 |
/// use icu::locale::fallback::LocaleFallbackPriority; |
| 61 |
/// |
| 62 |
/// // Set up the fallback iterator. |
| 63 |
/// let fallbacker = LocaleFallbacker::new(); |
| 64 |
/// let mut config = LocaleFallbackConfig::default(); |
| 65 |
/// config.priority = LocaleFallbackPriority::Language; |
| 66 |
/// let mut fallback_iterator = fallbacker |
| 67 |
/// .for_config(config) |
| 68 |
/// .fallback_for(data_locale!("ca-ES-valencia")); |
| 69 |
/// |
| 70 |
/// // Run the algorithm and check the results. |
| 71 |
/// assert_eq!(fallback_iterator.get(), &data_locale!("ca-ES-valencia")); |
| 72 |
/// fallback_iterator.step(); |
| 73 |
/// assert_eq!(fallback_iterator.get(), &data_locale!("ca-ES")); |
| 74 |
/// fallback_iterator.step(); |
| 75 |
/// assert_eq!(fallback_iterator.get(), &data_locale!("ca-valencia")); |
| 76 |
/// fallback_iterator.step(); |
| 77 |
/// assert_eq!(fallback_iterator.get(), &data_locale!("ca")); |
| 78 |
/// fallback_iterator.step(); |
| 79 |
/// assert_eq!(fallback_iterator.get(), &data_locale!("und")); |
| 80 |
/// ``` |
| 81 |
/// |
| 82 |
/// Retain the region subtag until the final step: |
| 83 |
/// |
| 84 |
/// ``` |
| 85 |
/// use icu::locale::LocaleFallbacker; |
| 86 |
/// use icu::locale::data_locale; |
| 87 |
/// use icu::locale::fallback::LocaleFallbackConfig; |
| 88 |
/// use icu::locale::fallback::LocaleFallbackPriority; |
| 89 |
/// |
| 90 |
/// // Set up the fallback iterator. |
| 91 |
/// let fallbacker = LocaleFallbacker::new(); |
| 92 |
/// let mut config = LocaleFallbackConfig::default(); |
| 93 |
/// config.priority = LocaleFallbackPriority::Region; |
| 94 |
/// let mut fallback_iterator = fallbacker |
| 95 |
/// .for_config(config) |
| 96 |
/// .fallback_for(data_locale!("ca-ES-valencia")); |
| 97 |
/// |
| 98 |
/// // Run the algorithm and check the results. |
| 99 |
/// assert_eq!(fallback_iterator.get(), &data_locale!("ca-ES-valencia")); |
| 100 |
/// fallback_iterator.step(); |
| 101 |
/// assert_eq!(fallback_iterator.get(), &data_locale!("ca-ES")); |
| 102 |
/// fallback_iterator.step(); |
| 103 |
/// assert_eq!(fallback_iterator.get(), &data_locale!("und-ES-valencia")); |
| 104 |
/// fallback_iterator.step(); |
| 105 |
/// assert_eq!(fallback_iterator.get(), &data_locale!("und-ES")); |
| 106 |
/// fallback_iterator.step(); |
| 107 |
/// assert_eq!(fallback_iterator.get(), &data_locale!("und")); |
| 108 |
/// ``` |
| 109 |
pub priority: LocaleFallbackPriority, |
| 110 |
} |
| 111 |
|
| 112 |
impl LocaleFallbackConfig { |
| 113 |
/// Const version of [`Default::default`]. |
| 114 |
pub const fn default() -> Self { |
| 115 |
Self { |
| 116 |
priority: LocaleFallbackPriority::default(), |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
impl Default for LocaleFallbackConfig { |
| 122 |
fn default() -> Self { |
| 123 |
Self::default() |
| 124 |
} |
| 125 |
} |