| 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 |
use super::{ |
| 6 |
DisplayNamesPreferences, LanguageIdentifierDisplayNameOptions, RegionDisplayName, |
| 7 |
ScriptDisplayName, VariantDisplayName, load_one, |
| 8 |
}; |
| 9 |
use crate::provider::names::{ |
| 10 |
LocaleNamesEssentialsV1, LocaleNamesLanguageLongHeavyV1, LocaleNamesLanguageLongLightV1, |
| 11 |
LocaleNamesLanguageMediumHeavyV1, LocaleNamesLanguageMediumLightV1, |
| 12 |
LocaleNamesLanguageMediumTinyV1, LocaleNamesLanguageMenuMediumHeavyV1, |
| 13 |
LocaleNamesLanguageMenuMediumLightV1, LocaleNamesLanguageShortHeavyV1, |
| 14 |
LocaleNamesLanguageShortLightV1, LocaleNamesRegionMediumLightV1, LocaleNamesRegionMediumTinyV1, |
| 15 |
LocaleNamesRegionShortLightV1, LocaleNamesRegionShortTinyV1, LocaleNamesScriptMediumHeavyV1, |
| 16 |
LocaleNamesScriptMediumLightV1, LocaleNamesScriptMediumTinyV1, LocaleNamesScriptShortHeavyV1, |
| 17 |
LocaleNamesVariantMediumHeavyV1, MenuNamePartsULE, |
| 18 |
}; |
| 19 |
use crate::size_test_macro::size_test; |
| 20 |
use alloc::vec::Vec; |
| 21 |
use icu_locale_core::LanguageIdentifier; |
| 22 |
use icu_locale_core::subtags::{Language, Region, Script, Variant}; |
| 23 |
use icu_pattern::{DoublePlaceholderPattern, PatternItem, TryWrap}; |
| 24 |
use icu_provider::DataPayloadOr; |
| 25 |
use icu_provider::marker::ErasedMarker; |
| 26 |
use icu_provider::prelude::*; |
| 27 |
use tinystr::{TinyAsciiStr, tinystr}; |
| 28 |
use writeable::LengthHint; |
| 29 |
use writeable::{PartsWrite, TryWriteable, adapters::LossyWrap}; |
| 30 |
use zerovec::VarZeroCow; |
| 31 |
|
| 32 |
/// An error returned when a display name was not found in data and has fallen back to the raw BCP-47 subtag code. |
| 33 |
#[derive(displaydoc::Display, Debug, Copy, Clone, PartialEq, Eq, Default)] |
| 34 |
#[allow(clippy::exhaustive_structs)] |
| 35 |
pub struct LanguageIdentifierNameFallbackError; |
| 36 |
|
| 37 |
/// A data struct that is either [`MenuNameParts`] or a string |
| 38 |
#[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)] |
| 39 |
#[allow(clippy::exhaustive_enums)] // provider-unstable |
| 40 |
enum MenuNamePartsOrString<'a> { |
| 41 |
/// A data struct that is [`MenuNameParts`] |
| 42 |
MenuNameParts(VarZeroCow<'a, MenuNamePartsULE>), |
| 43 |
/// A data struct that is a string |
| 44 |
String(VarZeroCow<'a, str>), |
| 45 |
} |
| 46 |
|
| 47 |
type MenuNamePartsMarker = ErasedMarker<VarZeroCow<'static, MenuNamePartsULE>>; |
| 48 |
|
| 49 |
type StringMarker = ErasedMarker<VarZeroCow<'static, str>>; |
| 50 |
|
| 51 |
size_test!( |
| 52 |
LanguageIdentifierDisplayName, |
| 53 |
language_identifier_display_name_owned_size, |
| 54 |
192 |
| 55 |
); |
| 56 |
|
| 57 |
macro_rules! table_row { |
| 58 |
(try_new_tiny) => { |
| 59 |
"| [`try_new_tiny`](Self::try_new_tiny) | \"English (United States)\" | ❌ | ❌ |" |
| 60 |
}; |
| 61 |
(try_new_light) => { |
| 62 |
"| [`try_new_light`](Self::try_new_light) | \"American English\" | \"Chinese\" | ❌ |" |
| 63 |
}; |
| 64 |
(try_new_short_light) => { |
| 65 |
"| [`try_new_short_light`](Self::try_new_short_light) | \"US English\" | \"Chinese\" | ❌ |" |
| 66 |
}; |
| 67 |
(try_new_long_light) => { |
| 68 |
"| [`try_new_long_light`](Self::try_new_long_light) | \"American English\" | \"Mandarin Chinese\" | ❌ |" |
| 69 |
}; |
| 70 |
(try_new_menu_light) => { |
| 71 |
"| [`try_new_menu_light`](Self::try_new_menu_light) | \"English (United States)\" | \"Chinese, Mandarin\" | ❌ |" |
| 72 |
}; |
| 73 |
(try_new_short_menu_light) => { |
| 74 |
"| [`try_new_short_menu_light`](Self::try_new_short_menu_light) | \"English (US)\" | \"Chinese, Mandarin\" | ❌ |" |
| 75 |
}; |
| 76 |
(try_new_heavy) => { |
| 77 |
"| [`try_new_heavy`](Self::try_new_heavy) | \"American English\" | \"Chinese\" | \"Azerbaijani\" |" |
| 78 |
}; |
| 79 |
(try_new_short_heavy) => { |
| 80 |
"| [`try_new_short_heavy`](Self::try_new_short_heavy) | \"US English\" | \"Chinese\" | \"Azeri\" |" |
| 81 |
}; |
| 82 |
(try_new_long_heavy) => { |
| 83 |
"| [`try_new_long_heavy`](Self::try_new_long_heavy) | \"American English\" | \"Mandarin Chinese\" | \"Azerbaijani\" |" |
| 84 |
}; |
| 85 |
(try_new_menu_heavy) => { |
| 86 |
"| [`try_new_menu_heavy`](Self::try_new_menu_heavy) | \"English (United States)\" | \"Chinese, Mandarin\" | \"Azerbaijani\" |" |
| 87 |
}; |
| 88 |
(try_new_short_menu_heavy) => { |
| 89 |
"| [`try_new_short_menu_heavy`](Self::try_new_short_menu_heavy) | \"English (US)\" | \"Chinese, Mandarin\" | \"Azeri\" |" |
| 90 |
}; |
| 91 |
} |
| 92 |
|
| 93 |
/// A localized display name for a language identifier, owned version. |
| 94 |
/// |
| 95 |
/// The formatter falls back to the BCP-47 subtag when localized display names are missing |
| 96 |
/// from the data provider. Fallback can be detected using [`TryWriteable`]. |
| 97 |
/// |
| 98 |
/// # Constructor Behavior |
| 99 |
/// |
| 100 |
/// There are several constructors, each of which links different data and serve |
| 101 |
/// different use cases. The behavior is illustrated in the table below. |
| 102 |
/// |
| 103 |
/// | Constructor | `en-US` | `zh` | `az` | |
| 104 |
/// | :--- | :--- | :--- | :--- | |
| 105 |
#[doc = concat!(table_row!(try_new_tiny), "\n")] |
| 106 |
#[doc = concat!(table_row!(try_new_light), "\n")] |
| 107 |
#[doc = concat!(table_row!(try_new_short_light), "\n")] |
| 108 |
#[doc = concat!(table_row!(try_new_long_light), "\n")] |
| 109 |
#[doc = concat!(table_row!(try_new_menu_light), "\n")] |
| 110 |
#[doc = concat!(table_row!(try_new_short_menu_light), "\n")] |
| 111 |
#[doc = concat!(table_row!(try_new_heavy), "\n")] |
| 112 |
#[doc = concat!(table_row!(try_new_short_heavy), "\n")] |
| 113 |
#[doc = concat!(table_row!(try_new_long_heavy), "\n")] |
| 114 |
#[doc = concat!(table_row!(try_new_menu_heavy), "\n")] |
| 115 |
#[doc = concat!(table_row!(try_new_short_menu_heavy), "\n")] |
| 116 |
/// |
| 117 |
/// > Note: :x: means that the name includes a BCP-47 subtag fallback. |
| 118 |
/// |
| 119 |
/// # Examples |
| 120 |
/// |
| 121 |
/// ``` |
| 122 |
/// use icu::locale::names::{ |
| 123 |
/// DisplayNamesPreferences, LanguageIdentifierDisplayName, |
| 124 |
/// LanguageIdentifierDisplayNameOptions, |
| 125 |
/// }; |
| 126 |
/// use icu::locale::{langid, locale}; |
| 127 |
/// use writeable::assert_try_writeable_eq; |
| 128 |
/// |
| 129 |
/// let prefs = DisplayNamesPreferences::from(locale!("en")); |
| 130 |
/// let options = LanguageIdentifierDisplayNameOptions::default(); |
| 131 |
/// let display_name = LanguageIdentifierDisplayName::try_new_light( |
| 132 |
/// prefs, |
| 133 |
/// langid!("fr-CA"), |
| 134 |
/// options, |
| 135 |
/// ) |
| 136 |
/// .expect("Data should load successfully"); |
| 137 |
/// |
| 138 |
/// assert_try_writeable_eq!( |
| 139 |
/// display_name.as_borrowed(), |
| 140 |
/// "Canadian French", |
| 141 |
/// Ok(()) |
| 142 |
/// ); |
| 143 |
/// ``` |
| 144 |
/// |
| 145 |
/// When a subtag is unknown: |
| 146 |
/// |
| 147 |
/// ``` |
| 148 |
/// use icu::locale::names::{ |
| 149 |
/// DisplayNamesPreferences, LanguageIdentifierDisplayName, LanguageIdentifierDisplayNameOptions, |
| 150 |
/// LanguageIdentifierNameFallbackError, |
| 151 |
/// }; |
| 152 |
/// use icu::locale::{locale, langid}; |
| 153 |
/// use writeable::{Part, TryWriteable, assert_try_writeable_parts_eq}; |
| 154 |
/// |
| 155 |
/// let prefs = DisplayNamesPreferences::from(locale!("en")); |
| 156 |
/// let options = LanguageIdentifierDisplayNameOptions::default(); |
| 157 |
/// |
| 158 |
/// // "it-Qabc-150" has known language "it" ("Italian") and known region "150" ("Europe"), |
| 159 |
/// // but unknown script "Qabc". |
| 160 |
/// let lang_id = langid!("it-Qabc-150"); |
| 161 |
/// let display_name = LanguageIdentifierDisplayName::try_new_light( |
| 162 |
/// prefs, |
| 163 |
/// lang_id, |
| 164 |
/// options, |
| 165 |
/// ) |
| 166 |
/// .expect("Data should load successfully"); |
| 167 |
/// |
| 168 |
/// let borrowed = display_name.as_borrowed(); |
| 169 |
/// |
| 170 |
/// // The fallback string is identified with a [`Part::ERROR`](writeable::Part::Error): |
| 171 |
/// assert_try_writeable_parts_eq!( |
| 172 |
/// borrowed, |
| 173 |
/// "Italian (Qabc, Europe)", |
| 174 |
/// Err(LanguageIdentifierNameFallbackError), |
| 175 |
/// [(9, 13, Part::ERROR)] // the span of Qabc |
| 176 |
/// ); |
| 177 |
/// |
| 178 |
/// // To format in lossy mode (ignoring fallback errors), use [`Writeable`] or [`Display`]: |
| 179 |
/// use writeable::assert_writeable_eq; |
| 180 |
/// assert_writeable_eq!(borrowed, "Italian (Qabc, Europe)"); |
| 181 |
/// ``` |
| 182 |
#[doc = language_identifier_display_name_owned_size!()] |
| 183 |
#[derive(Debug)] |
| 184 |
pub struct LanguageIdentifierDisplayName { |
| 185 |
/// Either the language display name or the subtag as fallback |
| 186 |
language_payload: DataPayloadOr<ErasedMarker<MenuNamePartsOrString<'static>>, Language>, |
| 187 |
/// All other fields (shared between Standard and Menu) |
| 188 |
qualifiers: QualifiersOwned, |
| 189 |
} |
| 190 |
|
| 191 |
type VariantPayloads = DataPayloadOr< |
| 192 |
ErasedMarker<VarZeroCow<'static, str>>, |
| 193 |
Result<Vec<DataPayloadOr<ErasedMarker<VarZeroCow<'static, str>>, Variant>>, Variant>, |
| 194 |
>; |
| 195 |
|
| 196 |
#[derive(Debug)] |
| 197 |
struct QualifiersOwned { |
| 198 |
/// Either the script display name, the subtag as fallback, or None if absent |
| 199 |
script_payload: DataPayloadOr<ErasedMarker<VarZeroCow<'static, str>>, Option<Script>>, |
| 200 |
/// Either the region display name, the subtag as fallback, or None if absent |
| 201 |
region_payload: DataPayloadOr<ErasedMarker<VarZeroCow<'static, str>>, Option<Region>>, |
| 202 |
/// Either a single variant display name, the subtag as fallback, or |
| 203 |
/// a vector of variant display names or subtags as fallback. |
| 204 |
/// The vector may be empty. |
| 205 |
variant_payloads: VariantPayloads, |
| 206 |
essentials_payload: DataPayload<LocaleNamesEssentialsV1>, |
| 207 |
} |
| 208 |
|
| 209 |
#[inline] |
| 210 |
fn make_attributes_for_subtag(subtag: &Language) -> &DataMarkerAttributes { |
| 211 |
// Valid Language subtags conform to DataMarkerAttributes syntax. |
| 212 |
DataMarkerAttributes::from_str_or_panic(subtag.as_str()) |
| 213 |
} |
| 214 |
|
| 215 |
#[inline] |
| 216 |
fn make_attributes_for_langid( |
| 217 |
language: Language, |
| 218 |
script: Option<Script>, |
| 219 |
region: Option<Region>, |
| 220 |
buffer: &mut TinyAsciiStr<16>, |
| 221 |
) -> &DataMarkerAttributes { |
| 222 |
const HYPHEN: TinyAsciiStr<1> = tinystr!(1, "-"); |
| 223 |
let lang_str = language.to_tinystr(); |
| 224 |
*buffer = match (script, region) { |
| 225 |
(Some(script), Some(region)) => { |
| 226 |
let script_str = script.to_tinystr(); |
| 227 |
let region_str = region.to_tinystr(); |
| 228 |
lang_str |
| 229 |
.concat::<1, 16>(HYPHEN) |
| 230 |
.concat::<4, 16>(script_str) |
| 231 |
.concat::<1, 16>(HYPHEN) |
| 232 |
.concat::<3, 16>(region_str) |
| 233 |
} |
| 234 |
(Some(script), None) => { |
| 235 |
let script_str = script.to_tinystr(); |
| 236 |
lang_str.concat::<1, 16>(HYPHEN).concat::<4, 16>(script_str) |
| 237 |
} |
| 238 |
(None, Some(region)) => { |
| 239 |
let region_str = region.to_tinystr(); |
| 240 |
lang_str.concat::<1, 16>(HYPHEN).concat::<3, 16>(region_str) |
| 241 |
} |
| 242 |
(None, None) => lang_str.resize::<16>(), |
| 243 |
}; |
| 244 |
// This is infallible (will not panic) because validated `Language`, `Script`, |
| 245 |
// `Region`, and hyphens are guaranteed to conform to `DataMarkerAttributes` syntax. |
| 246 |
DataMarkerAttributes::from_str_or_panic(buffer) |
| 247 |
} |
| 248 |
|
| 249 |
#[inline] |
| 250 |
fn make_locale(prefs: DisplayNamesPreferences) -> DataLocale { |
| 251 |
// All language markers use the same locale |
| 252 |
LocaleNamesLanguageMediumLightV1::make_locale(prefs.locale_preferences) |
| 253 |
} |
| 254 |
|
| 255 |
/// Loads the name for a language dialect, which includes script and region subtags. |
| 256 |
/// |
| 257 |
/// We try to load names for combinations of subtags: |
| 258 |
/// |
| 259 |
/// - Language + Script + Region (e.g., "zh-Hant-HK") |
| 260 |
/// - Language + Script (e.g., "zh-Hant") |
| 261 |
/// - Language + Region (e.g., "en-GB") |
| 262 |
/// |
| 263 |
/// We then "consume" the corresponding subtags from the input `LanguageIdentifier` |
| 264 |
/// so they are not repeated in the qualifiers. |
| 265 |
macro_rules! try_load_dialect_name { |
| 266 |
($provider:expr, $locale:expr, $subject:expr, [ $first_marker:ident $(, $rest_marker:ident)* $(,)? ]) => {{ |
| 267 |
let mut result = None; |
| 268 |
for (language, script, region) in [ |
| 269 |
($subject.language, Some($subject.script), Some($subject.region)), |
| 270 |
($subject.language, Some($subject.script), None), |
| 271 |
($subject.language, None, Some($subject.region)), |
| 272 |
] { |
| 273 |
let script_val = match script { |
| 274 |
Some(Some(s)) => Some(s), |
| 275 |
Some(None) => continue, |
| 276 |
None => None, |
| 277 |
}; |
| 278 |
let region_val = match region { |
| 279 |
Some(Some(r)) => Some(r), |
| 280 |
Some(None) => continue, |
| 281 |
None => None, |
| 282 |
}; |
| 283 |
let mut buffer = TinyAsciiStr::EMPTY; |
| 284 |
let attrs = make_attributes_for_langid( |
| 285 |
language, |
| 286 |
script_val, |
| 287 |
region_val, |
| 288 |
&mut buffer, |
| 289 |
); |
| 290 |
let loaded = load_one::<$first_marker, StringMarker, _>($provider, $locale, attrs)? |
| 291 |
$( |
| 292 |
.map_or_else( |
| 293 |
|| load_one::<$rest_marker, _, _>($provider, $locale, attrs), |
| 294 |
|p| Ok::<_, DataError>(Some(p)), |
| 295 |
)? |
| 296 |
)*; |
| 297 |
if let Some(payload) = loaded { |
| 298 |
if script_val.is_some() { |
| 299 |
$subject.script = None; |
| 300 |
} |
| 301 |
if region_val.is_some() { |
| 302 |
$subject.region = None; |
| 303 |
} |
| 304 |
result = Some(payload.map_project(|p, _| MenuNamePartsOrString::String(p))); |
| 305 |
break; |
| 306 |
} |
| 307 |
} |
| 308 |
Ok::<_, DataError>(result) |
| 309 |
}}; |
| 310 |
} |
| 311 |
|
| 312 |
impl LanguageIdentifierDisplayName { |
| 313 |
icu_provider::gen_buffer_data_constructors!( |
| 314 |
(prefs: DisplayNamesPreferences, subject: LanguageIdentifier, options: LanguageIdentifierDisplayNameOptions) -> result: Result<Self, DataError>, |
| 315 |
/// Loads a language display name in a formatting locale using compiled data. |
| 316 |
/// |
| 317 |
/// The `light` constructor links data for all common languages. |
| 318 |
/// See the [class docs](Self) for information on which constructor to use. |
| 319 |
/// |
| 320 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 321 |
/// |
| 322 |
/// # Examples |
| 323 |
/// |
| 324 |
/// ``` |
| 325 |
/// use icu::locale::names::{ |
| 326 |
/// DisplayNamesPreferences, LanguageIdentifierDisplayName, |
| 327 |
/// LanguageIdentifierDisplayNameOptions, LanguageIdentifierNameFallbackError, |
| 328 |
/// }; |
| 329 |
/// use icu::locale::{langid, locale}; |
| 330 |
/// use writeable::assert_try_writeable_eq; |
| 331 |
/// |
| 332 |
/// let prefs = DisplayNamesPreferences::from(locale!("de")); |
| 333 |
/// let options = LanguageIdentifierDisplayNameOptions::default(); |
| 334 |
/// |
| 335 |
/// // Light dataset formats common languages and subtags (e.g. region qualification): |
| 336 |
/// let display_name = LanguageIdentifierDisplayName::try_new_light( |
| 337 |
/// prefs, |
| 338 |
/// langid!("fr-CA"), |
| 339 |
/// options, |
| 340 |
/// ) |
| 341 |
/// .expect("Data should load successfully"); |
| 342 |
/// assert_try_writeable_eq!(display_name.as_borrowed(), "Französisch (Kanada)", Ok(())); |
| 343 |
/// |
| 344 |
/// // Light data does not include rare languages like Klingon ("tlh"), returning a fallback error: |
| 345 |
/// let tlh = LanguageIdentifierDisplayName::try_new_light(prefs, langid!("tlh"), options) |
| 346 |
/// .expect("Data should load successfully"); |
| 347 |
/// assert_try_writeable_eq!(tlh.as_borrowed(), "tlh", Err(LanguageIdentifierNameFallbackError)); |
| 348 |
/// ``` |
| 349 |
functions: [ |
| 350 |
try_new_light, |
| 351 |
try_new_light_with_buffer_provider, |
| 352 |
try_new_light_unstable, |
| 353 |
Self |
| 354 |
] |
| 355 |
); |
| 356 |
|
| 357 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_light)] |
| 358 |
pub fn try_new_light_unstable<D>( |
| 359 |
provider: &D, |
| 360 |
prefs: DisplayNamesPreferences, |
| 361 |
mut subject: LanguageIdentifier, |
| 362 |
options: LanguageIdentifierDisplayNameOptions, |
| 363 |
) -> Result<Self, DataError> |
| 364 |
where |
| 365 |
D: ?Sized |
| 366 |
+ DataProvider<LocaleNamesLanguageMediumLightV1> |
| 367 |
+ DataProvider<LocaleNamesLanguageMediumTinyV1> |
| 368 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 369 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 370 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 371 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 372 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 373 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 374 |
{ |
| 375 |
let locale = make_locale(prefs); |
| 376 |
let load_dialect = options.should_load_dialect(); |
| 377 |
let mut language_payload = None; |
| 378 |
if load_dialect { |
| 379 |
language_payload = try_load_dialect_name!( |
| 380 |
provider, |
| 381 |
&locale, |
| 382 |
subject, |
| 383 |
[ |
| 384 |
LocaleNamesLanguageMediumLightV1, |
| 385 |
LocaleNamesLanguageMediumTinyV1 |
| 386 |
] |
| 387 |
)?; |
| 388 |
} |
| 389 |
if language_payload.is_none() { |
| 390 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 391 |
language_payload = |
| 392 |
load_one::<LocaleNamesLanguageMediumLightV1, _, _>(provider, &locale, attrs)? |
| 393 |
.map_or_else( |
| 394 |
|| { |
| 395 |
load_one::<LocaleNamesLanguageMediumTinyV1, _, _>( |
| 396 |
provider, &locale, attrs, |
| 397 |
) |
| 398 |
}, |
| 399 |
|p| Ok(Some(p)), |
| 400 |
)? |
| 401 |
.map(|payload: DataPayload<StringMarker>| { |
| 402 |
payload.map_project(|p, _| MenuNamePartsOrString::String(p)) |
| 403 |
}); |
| 404 |
} |
| 405 |
let language_payload = match language_payload { |
| 406 |
Some(payload) => DataPayloadOr::from_payload(payload), |
| 407 |
None => DataPayloadOr::from_other(subject.language), |
| 408 |
}; |
| 409 |
|
| 410 |
let qualifiers = QualifiersOwned::try_new_light_unstable(provider, prefs, subject)?; |
| 411 |
Ok(Self { |
| 412 |
language_payload, |
| 413 |
qualifiers, |
| 414 |
}) |
| 415 |
} |
| 416 |
|
| 417 |
icu_provider::gen_buffer_data_constructors!( |
| 418 |
(prefs: DisplayNamesPreferences, subject: LanguageIdentifier, options: LanguageIdentifierDisplayNameOptions) -> result: Result<Self, DataError>, |
| 419 |
/// Loads a language display name in a formatting locale using compiled data. |
| 420 |
/// |
| 421 |
/// The `tiny` constructor links an extremely limited amount of data, with a focus on |
| 422 |
/// languages associated with the formatting locale. |
| 423 |
/// See the [class docs](Self) for more information on which constructor to use. |
| 424 |
/// |
| 425 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 426 |
/// |
| 427 |
/// # Examples |
| 428 |
/// |
| 429 |
/// ``` |
| 430 |
/// use icu::locale::names::{ |
| 431 |
/// LanguageIdentifierDisplayName, LanguageIdentifierNameFallbackError, |
| 432 |
/// }; |
| 433 |
/// use icu::locale::{langid, locale}; |
| 434 |
/// use writeable::assert_try_writeable_eq; |
| 435 |
/// |
| 436 |
/// // French contains its own translation in French... |
| 437 |
/// let fr_fr = LanguageIdentifierDisplayName::try_new_tiny( |
| 438 |
/// locale!("fr").into(), |
| 439 |
/// langid!("fr"), |
| 440 |
/// Default::default() |
| 441 |
/// ) |
| 442 |
/// .unwrap(); |
| 443 |
/// assert_try_writeable_eq!( |
| 444 |
/// fr_fr.as_borrowed(), |
| 445 |
/// "français" |
| 446 |
/// ); |
| 447 |
/// |
| 448 |
/// // ...but not a translation for German. |
| 449 |
/// let fr_de = LanguageIdentifierDisplayName::try_new_tiny( |
| 450 |
/// locale!("fr").into(), |
| 451 |
/// langid!("de"), |
| 452 |
/// Default::default() |
| 453 |
/// ) |
| 454 |
/// .unwrap(); |
| 455 |
/// assert_try_writeable_eq!( |
| 456 |
/// fr_de.as_borrowed(), |
| 457 |
/// "de", |
| 458 |
/// Err(LanguageIdentifierNameFallbackError) |
| 459 |
/// ); |
| 460 |
/// ``` |
| 461 |
functions: [ |
| 462 |
try_new_tiny, |
| 463 |
try_new_tiny_with_buffer_provider, |
| 464 |
try_new_tiny_unstable, |
| 465 |
Self |
| 466 |
] |
| 467 |
); |
| 468 |
|
| 469 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_tiny)] |
| 470 |
pub fn try_new_tiny_unstable<D>( |
| 471 |
provider: &D, |
| 472 |
prefs: DisplayNamesPreferences, |
| 473 |
mut subject: LanguageIdentifier, |
| 474 |
options: LanguageIdentifierDisplayNameOptions, |
| 475 |
) -> Result<Self, DataError> |
| 476 |
where |
| 477 |
D: ?Sized |
| 478 |
+ DataProvider<LocaleNamesLanguageMediumTinyV1> |
| 479 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 480 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 481 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 482 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 483 |
{ |
| 484 |
let locale = make_locale(prefs); |
| 485 |
// Step 1: Load language name |
| 486 |
let load_dialect = options.should_load_dialect(); |
| 487 |
let mut language_payload = None; |
| 488 |
// Only try dialect if requested (or default) |
| 489 |
if load_dialect { |
| 490 |
language_payload = try_load_dialect_name!( |
| 491 |
provider, |
| 492 |
&locale, |
| 493 |
subject, |
| 494 |
[LocaleNamesLanguageMediumTinyV1] |
| 495 |
)?; |
| 496 |
} |
| 497 |
if language_payload.is_none() { |
| 498 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 499 |
language_payload = load_one::<LocaleNamesLanguageMediumTinyV1, _, _>( |
| 500 |
provider, &locale, attrs, |
| 501 |
)? |
| 502 |
.map(|payload: DataPayload<StringMarker>| { |
| 503 |
payload.map_project(|p, _| MenuNamePartsOrString::String(p)) |
| 504 |
}); |
| 505 |
} |
| 506 |
let language_payload = match language_payload { |
| 507 |
Some(payload) => DataPayloadOr::from_payload(payload), |
| 508 |
None => DataPayloadOr::from_other(subject.language), |
| 509 |
}; |
| 510 |
|
| 511 |
// Load the remaining data |
| 512 |
let qualifiers = QualifiersOwned::try_new_tiny_unstable(provider, prefs, subject)?; |
| 513 |
Ok(Self { |
| 514 |
language_payload, |
| 515 |
qualifiers, |
| 516 |
}) |
| 517 |
} |
| 518 |
|
| 519 |
icu_provider::gen_buffer_data_constructors!( |
| 520 |
(prefs: DisplayNamesPreferences, subject: LanguageIdentifier, options: LanguageIdentifierDisplayNameOptions) -> result: Result<Self, DataError>, |
| 521 |
/// Loads a short language display name in a formatting locale using compiled data. |
| 522 |
/// |
| 523 |
/// Falls back to default (medium) length if a short name is not available. |
| 524 |
/// |
| 525 |
/// The `light` constructor links data for all common languages. |
| 526 |
/// See the [class docs](Self) for information on which constructor to use. |
| 527 |
/// |
| 528 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 529 |
/// |
| 530 |
/// # Examples |
| 531 |
/// |
| 532 |
/// ``` |
| 533 |
/// use icu::locale::names::{ |
| 534 |
/// DisplayNamesPreferences, LanguageIdentifierDisplayName, |
| 535 |
/// LanguageIdentifierDisplayNameOptions, LanguageIdentifierNameFallbackError, |
| 536 |
/// }; |
| 537 |
/// use icu::locale::{langid, locale}; |
| 538 |
/// use writeable::assert_try_writeable_eq; |
| 539 |
/// |
| 540 |
/// let prefs = DisplayNamesPreferences::from(locale!("en")); |
| 541 |
/// let options = LanguageIdentifierDisplayNameOptions::default(); |
| 542 |
/// |
| 543 |
/// // Short length format uses shorter subtag/qualifier names when available (e.g. "US English"): |
| 544 |
/// let display_name = LanguageIdentifierDisplayName::try_new_short_light( |
| 545 |
/// prefs, |
| 546 |
/// langid!("en-US"), |
| 547 |
/// options, |
| 548 |
/// ) |
| 549 |
/// .expect("Data should load successfully"); |
| 550 |
/// assert_try_writeable_eq!(display_name.as_borrowed(), "US English", Ok(())); |
| 551 |
/// |
| 552 |
/// // Light data does not include rare languages like Klingon ("tlh"), returning a fallback error: |
| 553 |
/// let tlh = LanguageIdentifierDisplayName::try_new_short_light(prefs, langid!("tlh"), options) |
| 554 |
/// .expect("Data should load successfully"); |
| 555 |
/// assert_try_writeable_eq!(tlh.as_borrowed(), "tlh", Err(LanguageIdentifierNameFallbackError)); |
| 556 |
/// ``` |
| 557 |
functions: [ |
| 558 |
try_new_short_light, |
| 559 |
try_new_short_light_with_buffer_provider, |
| 560 |
try_new_short_light_unstable, |
| 561 |
Self |
| 562 |
] |
| 563 |
); |
| 564 |
|
| 565 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_short_light)] |
| 566 |
pub fn try_new_short_light_unstable<D>( |
| 567 |
provider: &D, |
| 568 |
prefs: DisplayNamesPreferences, |
| 569 |
mut subject: LanguageIdentifier, |
| 570 |
options: LanguageIdentifierDisplayNameOptions, |
| 571 |
) -> Result<Self, DataError> |
| 572 |
where |
| 573 |
D: ?Sized |
| 574 |
+ DataProvider<LocaleNamesLanguageShortLightV1> |
| 575 |
+ DataProvider<LocaleNamesLanguageMediumLightV1> |
| 576 |
+ DataProvider<LocaleNamesLanguageMediumTinyV1> |
| 577 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 578 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 579 |
+ DataProvider<LocaleNamesRegionShortLightV1> |
| 580 |
+ DataProvider<LocaleNamesRegionShortTinyV1> |
| 581 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 582 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 583 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 584 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 585 |
{ |
| 586 |
let locale = make_locale(prefs); |
| 587 |
let load_dialect = options.should_load_dialect(); |
| 588 |
let mut language_payload = None; |
| 589 |
if load_dialect { |
| 590 |
language_payload = try_load_dialect_name!( |
| 591 |
provider, |
| 592 |
&locale, |
| 593 |
subject, |
| 594 |
[ |
| 595 |
LocaleNamesLanguageShortLightV1, |
| 596 |
LocaleNamesLanguageMediumLightV1, |
| 597 |
LocaleNamesLanguageMediumTinyV1 |
| 598 |
] |
| 599 |
)?; |
| 600 |
} |
| 601 |
if language_payload.is_none() { |
| 602 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 603 |
language_payload = |
| 604 |
load_one::<LocaleNamesLanguageShortLightV1, _, _>(provider, &locale, attrs)? |
| 605 |
.map_or_else( |
| 606 |
|| { |
| 607 |
load_one::<LocaleNamesLanguageMediumLightV1, _, _>( |
| 608 |
provider, &locale, attrs, |
| 609 |
) |
| 610 |
}, |
| 611 |
|p| Ok(Some(p)), |
| 612 |
)? |
| 613 |
.map_or_else( |
| 614 |
|| { |
| 615 |
load_one::<LocaleNamesLanguageMediumTinyV1, _, _>( |
| 616 |
provider, &locale, attrs, |
| 617 |
) |
| 618 |
}, |
| 619 |
|p| Ok(Some(p)), |
| 620 |
)? |
| 621 |
.map(|payload: DataPayload<StringMarker>| { |
| 622 |
payload.map_project(|p, _| MenuNamePartsOrString::String(p)) |
| 623 |
}); |
| 624 |
} |
| 625 |
let language_payload = match language_payload { |
| 626 |
Some(payload) => DataPayloadOr::from_payload(payload), |
| 627 |
None => DataPayloadOr::from_other(subject.language), |
| 628 |
}; |
| 629 |
|
| 630 |
let qualifiers = QualifiersOwned::try_new_short_light_unstable(provider, prefs, subject)?; |
| 631 |
Ok(Self { |
| 632 |
language_payload, |
| 633 |
qualifiers, |
| 634 |
}) |
| 635 |
} |
| 636 |
|
| 637 |
icu_provider::gen_buffer_data_constructors!( |
| 638 |
(prefs: DisplayNamesPreferences, subject: LanguageIdentifier, options: LanguageIdentifierDisplayNameOptions) -> result: Result<Self, DataError>, |
| 639 |
/// Loads a long language display name in a formatting locale using compiled data. |
| 640 |
/// |
| 641 |
/// Falls back to default (medium) length if a long name is not available. |
| 642 |
/// |
| 643 |
/// The `light` constructor links data for all common languages. |
| 644 |
/// See the [class docs](Self) for information on which constructor to use. |
| 645 |
/// |
| 646 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 647 |
/// |
| 648 |
/// # Examples |
| 649 |
/// |
| 650 |
/// ``` |
| 651 |
/// use icu::locale::names::{ |
| 652 |
/// DisplayNamesPreferences, LanguageIdentifierDisplayName, |
| 653 |
/// LanguageIdentifierDisplayNameOptions, LanguageIdentifierNameFallbackError, |
| 654 |
/// }; |
| 655 |
/// use icu::locale::{langid, locale}; |
| 656 |
/// use writeable::assert_try_writeable_eq; |
| 657 |
/// |
| 658 |
/// let prefs = DisplayNamesPreferences::from(locale!("en")); |
| 659 |
/// let options = LanguageIdentifierDisplayNameOptions::default(); |
| 660 |
/// |
| 661 |
/// // Long length format uses longer subtag names when available (e.g. "Mandarin Chinese"): |
| 662 |
/// let display_name = LanguageIdentifierDisplayName::try_new_long_light( |
| 663 |
/// prefs, |
| 664 |
/// langid!("zh"), |
| 665 |
/// options, |
| 666 |
/// ) |
| 667 |
/// .expect("Data should load successfully"); |
| 668 |
/// assert_try_writeable_eq!(display_name.as_borrowed(), "Mandarin Chinese", Ok(())); |
| 669 |
/// |
| 670 |
/// // Light data does not include rare languages like Klingon ("tlh"), returning a fallback error: |
| 671 |
/// let tlh = LanguageIdentifierDisplayName::try_new_long_light(prefs, langid!("tlh"), options) |
| 672 |
/// .expect("Data should load successfully"); |
| 673 |
/// assert_try_writeable_eq!(tlh.as_borrowed(), "tlh", Err(LanguageIdentifierNameFallbackError)); |
| 674 |
/// ``` |
| 675 |
functions: [ |
| 676 |
try_new_long_light, |
| 677 |
try_new_long_light_with_buffer_provider, |
| 678 |
try_new_long_light_unstable, |
| 679 |
Self |
| 680 |
] |
| 681 |
); |
| 682 |
|
| 683 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_long_light)] |
| 684 |
pub fn try_new_long_light_unstable<D>( |
| 685 |
provider: &D, |
| 686 |
prefs: DisplayNamesPreferences, |
| 687 |
mut subject: LanguageIdentifier, |
| 688 |
options: LanguageIdentifierDisplayNameOptions, |
| 689 |
) -> Result<Self, DataError> |
| 690 |
where |
| 691 |
D: ?Sized |
| 692 |
+ DataProvider<LocaleNamesLanguageLongLightV1> |
| 693 |
+ DataProvider<LocaleNamesLanguageMediumLightV1> |
| 694 |
+ DataProvider<LocaleNamesLanguageMediumTinyV1> |
| 695 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 696 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 697 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 698 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 699 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 700 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 701 |
{ |
| 702 |
let locale = make_locale(prefs); |
| 703 |
let load_dialect = options.should_load_dialect(); |
| 704 |
let mut language_payload = None; |
| 705 |
if load_dialect { |
| 706 |
language_payload = try_load_dialect_name!( |
| 707 |
provider, |
| 708 |
&locale, |
| 709 |
subject, |
| 710 |
[ |
| 711 |
LocaleNamesLanguageLongLightV1, |
| 712 |
LocaleNamesLanguageMediumLightV1, |
| 713 |
LocaleNamesLanguageMediumTinyV1 |
| 714 |
] |
| 715 |
)?; |
| 716 |
} |
| 717 |
if language_payload.is_none() { |
| 718 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 719 |
language_payload = |
| 720 |
load_one::<LocaleNamesLanguageLongLightV1, _, _>(provider, &locale, attrs)? |
| 721 |
.map_or_else( |
| 722 |
|| { |
| 723 |
load_one::<LocaleNamesLanguageMediumLightV1, _, _>( |
| 724 |
provider, &locale, attrs, |
| 725 |
) |
| 726 |
}, |
| 727 |
|p| Ok(Some(p)), |
| 728 |
)? |
| 729 |
.map_or_else( |
| 730 |
|| { |
| 731 |
load_one::<LocaleNamesLanguageMediumTinyV1, _, _>( |
| 732 |
provider, &locale, attrs, |
| 733 |
) |
| 734 |
}, |
| 735 |
|p| Ok(Some(p)), |
| 736 |
)? |
| 737 |
.map(|payload: DataPayload<StringMarker>| { |
| 738 |
payload.map_project(|p, _| MenuNamePartsOrString::String(p)) |
| 739 |
}); |
| 740 |
} |
| 741 |
let language_payload = match language_payload { |
| 742 |
Some(payload) => DataPayloadOr::from_payload(payload), |
| 743 |
None => DataPayloadOr::from_other(subject.language), |
| 744 |
}; |
| 745 |
|
| 746 |
let qualifiers = QualifiersOwned::try_new_light_unstable(provider, prefs, subject)?; |
| 747 |
Ok(Self { |
| 748 |
language_payload, |
| 749 |
qualifiers, |
| 750 |
}) |
| 751 |
} |
| 752 |
|
| 753 |
icu_provider::gen_buffer_data_constructors!( |
| 754 |
(prefs: DisplayNamesPreferences, subject: LanguageIdentifier, options: LanguageIdentifierDisplayNameOptions) -> result: Result<Self, DataError>, |
| 755 |
/// Loads a menu-style language display name in a formatting locale using compiled data. |
| 756 |
/// |
| 757 |
/// The `light` constructor links data for all common languages. |
| 758 |
/// See the [class docs](Self) for information on which constructor to use. |
| 759 |
/// |
| 760 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 761 |
/// |
| 762 |
/// # Examples |
| 763 |
/// |
| 764 |
/// ``` |
| 765 |
/// use icu::locale::names::{ |
| 766 |
/// DisplayNamesPreferences, LanguageIdentifierDisplayName, |
| 767 |
/// LanguageIdentifierDisplayNameOptions, LanguageIdentifierNameFallbackError, |
| 768 |
/// }; |
| 769 |
/// use icu::locale::{langid, locale}; |
| 770 |
/// use writeable::assert_try_writeable_eq; |
| 771 |
/// |
| 772 |
/// let prefs = DisplayNamesPreferences::from(locale!("en")); |
| 773 |
/// let options = LanguageIdentifierDisplayNameOptions::default(); |
| 774 |
/// |
| 775 |
/// // Menu-style format places primary language name first for dropdown sorting (e.g. "Chinese, Mandarin"): |
| 776 |
/// let display_name = LanguageIdentifierDisplayName::try_new_menu_light( |
| 777 |
/// prefs, |
| 778 |
/// langid!("zh"), |
| 779 |
/// options, |
| 780 |
/// ) |
| 781 |
/// .expect("Data should load successfully"); |
| 782 |
/// assert_try_writeable_eq!(display_name.as_borrowed(), "Chinese, Mandarin", Ok(())); |
| 783 |
/// |
| 784 |
/// // Light data does not include rare languages like Klingon ("tlh"), returning a fallback error: |
| 785 |
/// let tlh = LanguageIdentifierDisplayName::try_new_menu_light(prefs, langid!("tlh"), options) |
| 786 |
/// .expect("Data should load successfully"); |
| 787 |
/// assert_try_writeable_eq!(tlh.as_borrowed(), "tlh", Err(LanguageIdentifierNameFallbackError)); |
| 788 |
/// ``` |
| 789 |
functions: [ |
| 790 |
try_new_menu_light, |
| 791 |
try_new_menu_light_with_buffer_provider, |
| 792 |
try_new_menu_light_unstable, |
| 793 |
Self |
| 794 |
] |
| 795 |
); |
| 796 |
|
| 797 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_menu_light)] |
| 798 |
pub fn try_new_menu_light_unstable<D>( |
| 799 |
provider: &D, |
| 800 |
prefs: DisplayNamesPreferences, |
| 801 |
subject: LanguageIdentifier, |
| 802 |
_options: LanguageIdentifierDisplayNameOptions, |
| 803 |
) -> Result<Self, DataError> |
| 804 |
where |
| 805 |
D: ?Sized |
| 806 |
+ DataProvider<LocaleNamesLanguageMenuMediumLightV1> |
| 807 |
+ DataProvider<LocaleNamesLanguageMediumLightV1> |
| 808 |
+ DataProvider<LocaleNamesLanguageMediumTinyV1> |
| 809 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 810 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 811 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 812 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 813 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 814 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 815 |
{ |
| 816 |
let locale = make_locale(prefs); |
| 817 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 818 |
let mut language_payload = load_one::<LocaleNamesLanguageMenuMediumLightV1, _, _>( |
| 819 |
provider, &locale, attrs, |
| 820 |
)? |
| 821 |
.map(|payload: DataPayload<MenuNamePartsMarker>| { |
| 822 |
payload.map_project(|p, _| MenuNamePartsOrString::MenuNameParts(p)) |
| 823 |
}); |
| 824 |
if language_payload.is_none() { |
| 825 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 826 |
language_payload = |
| 827 |
load_one::<LocaleNamesLanguageMediumLightV1, _, _>(provider, &locale, attrs)? |
| 828 |
.map_or_else( |
| 829 |
|| { |
| 830 |
load_one::<LocaleNamesLanguageMediumTinyV1, _, _>( |
| 831 |
provider, &locale, attrs, |
| 832 |
) |
| 833 |
}, |
| 834 |
|p| Ok(Some(p)), |
| 835 |
)? |
| 836 |
.map(|payload: DataPayload<StringMarker>| { |
| 837 |
payload.map_project(|p, _| MenuNamePartsOrString::String(p)) |
| 838 |
}); |
| 839 |
} |
| 840 |
let language_payload = match language_payload { |
| 841 |
Some(payload) => DataPayloadOr::from_payload(payload), |
| 842 |
None => DataPayloadOr::from_other(subject.language), |
| 843 |
}; |
| 844 |
|
| 845 |
let qualifiers = QualifiersOwned::try_new_light_unstable(provider, prefs, subject)?; |
| 846 |
Ok(Self { |
| 847 |
language_payload, |
| 848 |
qualifiers, |
| 849 |
}) |
| 850 |
} |
| 851 |
|
| 852 |
icu_provider::gen_buffer_data_constructors!( |
| 853 |
(prefs: DisplayNamesPreferences, subject: LanguageIdentifier, options: LanguageIdentifierDisplayNameOptions) -> result: Result<Self, DataError>, |
| 854 |
/// Loads a short menu-style language display name in a formatting locale using compiled data. |
| 855 |
/// |
| 856 |
/// Falls back to default (medium) length if a short name is not available. |
| 857 |
/// |
| 858 |
/// The `light` constructor links data for all common languages. |
| 859 |
/// See the [class docs](Self) for information on which constructor to use. |
| 860 |
/// |
| 861 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 862 |
/// |
| 863 |
/// # Examples |
| 864 |
/// |
| 865 |
/// ``` |
| 866 |
/// use icu::locale::names::{ |
| 867 |
/// DisplayNamesPreferences, LanguageIdentifierDisplayName, |
| 868 |
/// LanguageIdentifierDisplayNameOptions, LanguageIdentifierNameFallbackError, |
| 869 |
/// }; |
| 870 |
/// use icu::locale::{langid, locale}; |
| 871 |
/// use writeable::assert_try_writeable_eq; |
| 872 |
/// |
| 873 |
/// let prefs = DisplayNamesPreferences::from(locale!("en")); |
| 874 |
/// let options = LanguageIdentifierDisplayNameOptions::default(); |
| 875 |
/// |
| 876 |
/// // Short menu format uses abbreviated region name in menu layout (e.g. "English (US)"): |
| 877 |
/// let display_name = LanguageIdentifierDisplayName::try_new_short_menu_light( |
| 878 |
/// prefs, |
| 879 |
/// langid!("en-US"), |
| 880 |
/// options, |
| 881 |
/// ) |
| 882 |
/// .expect("Data should load successfully"); |
| 883 |
/// assert_try_writeable_eq!(display_name.as_borrowed(), "English (US)", Ok(())); |
| 884 |
/// |
| 885 |
/// // Light data does not include rare languages like Klingon ("tlh"), returning a fallback error: |
| 886 |
/// let tlh = LanguageIdentifierDisplayName::try_new_short_menu_light(prefs, langid!("tlh"), options) |
| 887 |
/// .expect("Data should load successfully"); |
| 888 |
/// assert_try_writeable_eq!(tlh.as_borrowed(), "tlh", Err(LanguageIdentifierNameFallbackError)); |
| 889 |
/// ``` |
| 890 |
functions: [ |
| 891 |
try_new_short_menu_light, |
| 892 |
try_new_short_menu_light_with_buffer_provider, |
| 893 |
try_new_short_menu_light_unstable, |
| 894 |
Self |
| 895 |
] |
| 896 |
); |
| 897 |
|
| 898 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_short_menu_light)] |
| 899 |
pub fn try_new_short_menu_light_unstable<D>( |
| 900 |
provider: &D, |
| 901 |
prefs: DisplayNamesPreferences, |
| 902 |
subject: LanguageIdentifier, |
| 903 |
_options: LanguageIdentifierDisplayNameOptions, |
| 904 |
) -> Result<Self, DataError> |
| 905 |
where |
| 906 |
D: ?Sized |
| 907 |
+ DataProvider<LocaleNamesLanguageMenuMediumLightV1> |
| 908 |
+ DataProvider<LocaleNamesLanguageShortLightV1> |
| 909 |
+ DataProvider<LocaleNamesLanguageMediumLightV1> |
| 910 |
+ DataProvider<LocaleNamesLanguageMediumTinyV1> |
| 911 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 912 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 913 |
+ DataProvider<LocaleNamesRegionShortLightV1> |
| 914 |
+ DataProvider<LocaleNamesRegionShortTinyV1> |
| 915 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 916 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 917 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 918 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 919 |
{ |
| 920 |
let locale = make_locale(prefs); |
| 921 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 922 |
let mut language_payload = load_one::<LocaleNamesLanguageMenuMediumLightV1, _, _>( |
| 923 |
provider, &locale, attrs, |
| 924 |
)? |
| 925 |
.map(|payload: DataPayload<MenuNamePartsMarker>| { |
| 926 |
payload.map_project(|p, _| MenuNamePartsOrString::MenuNameParts(p)) |
| 927 |
}); |
| 928 |
if language_payload.is_none() { |
| 929 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 930 |
language_payload = |
| 931 |
load_one::<LocaleNamesLanguageShortLightV1, _, _>(provider, &locale, attrs)? |
| 932 |
.map_or_else( |
| 933 |
|| { |
| 934 |
load_one::<LocaleNamesLanguageMediumLightV1, _, _>( |
| 935 |
provider, &locale, attrs, |
| 936 |
) |
| 937 |
}, |
| 938 |
|p| Ok(Some(p)), |
| 939 |
)? |
| 940 |
.map_or_else( |
| 941 |
|| { |
| 942 |
load_one::<LocaleNamesLanguageMediumTinyV1, _, _>( |
| 943 |
provider, &locale, attrs, |
| 944 |
) |
| 945 |
}, |
| 946 |
|p| Ok(Some(p)), |
| 947 |
)? |
| 948 |
.map(|payload: DataPayload<StringMarker>| { |
| 949 |
payload.map_project(|p, _| MenuNamePartsOrString::String(p)) |
| 950 |
}); |
| 951 |
} |
| 952 |
let language_payload = match language_payload { |
| 953 |
Some(payload) => DataPayloadOr::from_payload(payload), |
| 954 |
None => DataPayloadOr::from_other(subject.language), |
| 955 |
}; |
| 956 |
|
| 957 |
let qualifiers = QualifiersOwned::try_new_short_light_unstable(provider, prefs, subject)?; |
| 958 |
Ok(Self { |
| 959 |
language_payload, |
| 960 |
qualifiers, |
| 961 |
}) |
| 962 |
} |
| 963 |
|
| 964 |
icu_provider::gen_buffer_data_constructors!( |
| 965 |
(prefs: DisplayNamesPreferences, subject: LanguageIdentifier, options: LanguageIdentifierDisplayNameOptions) -> result: Result<Self, DataError>, |
| 966 |
/// Loads a language display name in a formatting locale using compiled data. |
| 967 |
/// |
| 968 |
/// The `heavy` constructor includes additional data coverage for subtags that are |
| 969 |
/// less commonly formatted in the target locale. |
| 970 |
/// See the [class docs](Self) for information on which constructor to use. |
| 971 |
/// |
| 972 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 973 |
/// |
| 974 |
/// # Examples |
| 975 |
/// |
| 976 |
/// ``` |
| 977 |
/// use icu::locale::names::{ |
| 978 |
/// DisplayNamesPreferences, LanguageIdentifierDisplayName, LanguageIdentifierDisplayNameOptions, |
| 979 |
/// }; |
| 980 |
/// use icu::locale::{langid, locale}; |
| 981 |
/// use writeable::assert_try_writeable_eq; |
| 982 |
/// |
| 983 |
/// let prefs = DisplayNamesPreferences::from(locale!("nl")); |
| 984 |
/// let options = LanguageIdentifierDisplayNameOptions::default(); |
| 985 |
/// |
| 986 |
/// // Heavy dataset includes data coverage for rare languages like Klingon ("tlh"): |
| 987 |
/// let display_name = LanguageIdentifierDisplayName::try_new_heavy( |
| 988 |
/// prefs, |
| 989 |
/// langid!("tlh"), |
| 990 |
/// options, |
| 991 |
/// ) |
| 992 |
/// .expect("Data should load successfully"); |
| 993 |
/// assert_try_writeable_eq!(display_name.as_borrowed(), "Klingon", Ok(())); |
| 994 |
/// ``` |
| 995 |
functions: [ |
| 996 |
try_new_heavy, |
| 997 |
try_new_heavy_with_buffer_provider, |
| 998 |
try_new_heavy_unstable, |
| 999 |
Self |
| 1000 |
] |
| 1001 |
); |
| 1002 |
|
| 1003 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_heavy)] |
| 1004 |
pub fn try_new_heavy_unstable<D>( |
| 1005 |
provider: &D, |
| 1006 |
prefs: DisplayNamesPreferences, |
| 1007 |
mut subject: LanguageIdentifier, |
| 1008 |
options: LanguageIdentifierDisplayNameOptions, |
| 1009 |
) -> Result<Self, DataError> |
| 1010 |
where |
| 1011 |
D: ?Sized |
| 1012 |
+ DataProvider<LocaleNamesLanguageMediumHeavyV1> |
| 1013 |
+ DataProvider<LocaleNamesLanguageMediumLightV1> |
| 1014 |
+ DataProvider<LocaleNamesLanguageMediumTinyV1> |
| 1015 |
+ DataProvider<LocaleNamesScriptMediumHeavyV1> |
| 1016 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 1017 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 1018 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 1019 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 1020 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 1021 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 1022 |
{ |
| 1023 |
let locale = make_locale(prefs); |
| 1024 |
// Step 1: Load language name |
| 1025 |
let load_dialect = options.should_load_dialect(); |
| 1026 |
let mut language_payload = None; |
| 1027 |
// Only try dialect if requested (or default) |
| 1028 |
if load_dialect { |
| 1029 |
language_payload = try_load_dialect_name!( |
| 1030 |
provider, |
| 1031 |
&locale, |
| 1032 |
subject, |
| 1033 |
[ |
| 1034 |
LocaleNamesLanguageMediumHeavyV1, |
| 1035 |
LocaleNamesLanguageMediumLightV1, |
| 1036 |
LocaleNamesLanguageMediumTinyV1 |
| 1037 |
] |
| 1038 |
)?; |
| 1039 |
} |
| 1040 |
if language_payload.is_none() { |
| 1041 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 1042 |
language_payload = |
| 1043 |
load_one::<LocaleNamesLanguageMediumHeavyV1, _, _>(provider, &locale, attrs)? |
| 1044 |
.map_or_else( |
| 1045 |
|| { |
| 1046 |
load_one::<LocaleNamesLanguageMediumLightV1, _, _>( |
| 1047 |
provider, &locale, attrs, |
| 1048 |
) |
| 1049 |
}, |
| 1050 |
|p| Ok(Some(p)), |
| 1051 |
)? |
| 1052 |
.map_or_else( |
| 1053 |
|| { |
| 1054 |
load_one::<LocaleNamesLanguageMediumTinyV1, _, _>( |
| 1055 |
provider, &locale, attrs, |
| 1056 |
) |
| 1057 |
}, |
| 1058 |
|p| Ok(Some(p)), |
| 1059 |
)? |
| 1060 |
.map(|payload: DataPayload<StringMarker>| { |
| 1061 |
payload.map_project(|p, _| MenuNamePartsOrString::String(p)) |
| 1062 |
}); |
| 1063 |
} |
| 1064 |
let language_payload = match language_payload { |
| 1065 |
Some(payload) => DataPayloadOr::from_payload(payload), |
| 1066 |
None => DataPayloadOr::from_other(subject.language), |
| 1067 |
}; |
| 1068 |
|
| 1069 |
// Load the remaining data |
| 1070 |
let qualifiers = QualifiersOwned::try_new_heavy_unstable(provider, prefs, subject)?; |
| 1071 |
Ok(Self { |
| 1072 |
language_payload, |
| 1073 |
qualifiers, |
| 1074 |
}) |
| 1075 |
} |
| 1076 |
|
| 1077 |
icu_provider::gen_buffer_data_constructors!( |
| 1078 |
(prefs: DisplayNamesPreferences, subject: LanguageIdentifier, options: LanguageIdentifierDisplayNameOptions) -> result: Result<Self, DataError>, |
| 1079 |
/// Loads a short language display name in a formatting locale using compiled data. |
| 1080 |
/// |
| 1081 |
/// Falls back to default (medium) length if a short name is not available. |
| 1082 |
/// |
| 1083 |
/// The `heavy` constructor includes additional data coverage for subtags that are |
| 1084 |
/// less commonly formatted in the target locale. |
| 1085 |
/// See the [class docs](Self) for information on which constructor to use. |
| 1086 |
/// |
| 1087 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 1088 |
/// |
| 1089 |
/// # Examples |
| 1090 |
/// |
| 1091 |
/// ``` |
| 1092 |
/// use icu::locale::names::{ |
| 1093 |
/// DisplayNamesPreferences, LanguageIdentifierDisplayName, LanguageIdentifierDisplayNameOptions, |
| 1094 |
/// }; |
| 1095 |
/// use icu::locale::{langid, locale}; |
| 1096 |
/// use writeable::assert_try_writeable_eq; |
| 1097 |
/// |
| 1098 |
/// let prefs = DisplayNamesPreferences::from(locale!("nl")); |
| 1099 |
/// let options = LanguageIdentifierDisplayNameOptions::default(); |
| 1100 |
/// |
| 1101 |
/// // Short heavy formats short names with full data coverage for rare subtags: |
| 1102 |
/// let display_name = LanguageIdentifierDisplayName::try_new_short_heavy( |
| 1103 |
/// prefs, |
| 1104 |
/// langid!("tlh-001"), |
| 1105 |
/// options, |
| 1106 |
/// ) |
| 1107 |
/// .expect("Data should load successfully"); |
| 1108 |
/// assert_try_writeable_eq!(display_name.as_borrowed(), "Klingon (wereld)", Ok(())); |
| 1109 |
/// ``` |
| 1110 |
functions: [ |
| 1111 |
try_new_short_heavy, |
| 1112 |
try_new_short_heavy_with_buffer_provider, |
| 1113 |
try_new_short_heavy_unstable, |
| 1114 |
Self |
| 1115 |
] |
| 1116 |
); |
| 1117 |
|
| 1118 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_short_heavy)] |
| 1119 |
pub fn try_new_short_heavy_unstable<D>( |
| 1120 |
provider: &D, |
| 1121 |
prefs: DisplayNamesPreferences, |
| 1122 |
mut subject: LanguageIdentifier, |
| 1123 |
options: LanguageIdentifierDisplayNameOptions, |
| 1124 |
) -> Result<Self, DataError> |
| 1125 |
where |
| 1126 |
D: ?Sized |
| 1127 |
+ DataProvider<LocaleNamesLanguageShortHeavyV1> |
| 1128 |
+ DataProvider<LocaleNamesLanguageShortLightV1> |
| 1129 |
+ DataProvider<LocaleNamesLanguageMediumHeavyV1> |
| 1130 |
+ DataProvider<LocaleNamesLanguageMediumLightV1> |
| 1131 |
+ DataProvider<LocaleNamesLanguageMediumTinyV1> |
| 1132 |
+ DataProvider<LocaleNamesScriptShortHeavyV1> |
| 1133 |
+ DataProvider<LocaleNamesScriptMediumHeavyV1> |
| 1134 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 1135 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 1136 |
+ DataProvider<LocaleNamesRegionShortLightV1> |
| 1137 |
+ DataProvider<LocaleNamesRegionShortTinyV1> |
| 1138 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 1139 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 1140 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 1141 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 1142 |
{ |
| 1143 |
let locale = make_locale(prefs); |
| 1144 |
// Step 1: Load language name |
| 1145 |
let load_dialect = options.should_load_dialect(); |
| 1146 |
let mut language_payload = None; |
| 1147 |
// Only try dialect if requested (or default) |
| 1148 |
if load_dialect { |
| 1149 |
language_payload = try_load_dialect_name!( |
| 1150 |
provider, |
| 1151 |
&locale, |
| 1152 |
subject, |
| 1153 |
[ |
| 1154 |
LocaleNamesLanguageShortHeavyV1, |
| 1155 |
LocaleNamesLanguageShortLightV1, |
| 1156 |
LocaleNamesLanguageMediumHeavyV1, |
| 1157 |
LocaleNamesLanguageMediumLightV1, |
| 1158 |
LocaleNamesLanguageMediumTinyV1 |
| 1159 |
] |
| 1160 |
)?; |
| 1161 |
} |
| 1162 |
if language_payload.is_none() { |
| 1163 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 1164 |
language_payload = |
| 1165 |
load_one::<LocaleNamesLanguageShortHeavyV1, _, _>(provider, &locale, attrs)? |
| 1166 |
.map_or_else( |
| 1167 |
|| { |
| 1168 |
load_one::<LocaleNamesLanguageShortLightV1, _, _>( |
| 1169 |
provider, &locale, attrs, |
| 1170 |
) |
| 1171 |
}, |
| 1172 |
|p| Ok(Some(p)), |
| 1173 |
)? |
| 1174 |
.map_or_else( |
| 1175 |
|| { |
| 1176 |
load_one::<LocaleNamesLanguageMediumHeavyV1, _, _>( |
| 1177 |
provider, &locale, attrs, |
| 1178 |
) |
| 1179 |
}, |
| 1180 |
|p| Ok(Some(p)), |
| 1181 |
)? |
| 1182 |
.map_or_else( |
| 1183 |
|| { |
| 1184 |
load_one::<LocaleNamesLanguageMediumLightV1, _, _>( |
| 1185 |
provider, &locale, attrs, |
| 1186 |
) |
| 1187 |
}, |
| 1188 |
|p| Ok(Some(p)), |
| 1189 |
)? |
| 1190 |
.map_or_else( |
| 1191 |
|| { |
| 1192 |
load_one::<LocaleNamesLanguageMediumTinyV1, _, _>( |
| 1193 |
provider, &locale, attrs, |
| 1194 |
) |
| 1195 |
}, |
| 1196 |
|p| Ok(Some(p)), |
| 1197 |
)? |
| 1198 |
.map(|payload: DataPayload<StringMarker>| { |
| 1199 |
payload.map_project(|p, _| MenuNamePartsOrString::String(p)) |
| 1200 |
}); |
| 1201 |
} |
| 1202 |
let language_payload = match language_payload { |
| 1203 |
Some(payload) => DataPayloadOr::from_payload(payload), |
| 1204 |
None => DataPayloadOr::from_other(subject.language), |
| 1205 |
}; |
| 1206 |
|
| 1207 |
// Load the remaining data |
| 1208 |
let qualifiers = QualifiersOwned::try_new_short_heavy_unstable(provider, prefs, subject)?; |
| 1209 |
Ok(Self { |
| 1210 |
language_payload, |
| 1211 |
qualifiers, |
| 1212 |
}) |
| 1213 |
} |
| 1214 |
|
| 1215 |
icu_provider::gen_buffer_data_constructors!( |
| 1216 |
(prefs: DisplayNamesPreferences, subject: LanguageIdentifier, options: LanguageIdentifierDisplayNameOptions) -> result: Result<Self, DataError>, |
| 1217 |
/// Loads a long language display name in a formatting locale using compiled data. |
| 1218 |
/// |
| 1219 |
/// Falls back to default (medium) length if a long name is not available. |
| 1220 |
/// |
| 1221 |
/// The `heavy` constructor includes additional data coverage for subtags that are |
| 1222 |
/// less commonly formatted in the target locale. |
| 1223 |
/// See the [class docs](Self) for information on which constructor to use. |
| 1224 |
/// |
| 1225 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 1226 |
/// |
| 1227 |
/// # Examples |
| 1228 |
/// |
| 1229 |
/// ``` |
| 1230 |
/// use icu::locale::names::{ |
| 1231 |
/// DisplayNamesPreferences, LanguageIdentifierDisplayName, LanguageIdentifierDisplayNameOptions, |
| 1232 |
/// }; |
| 1233 |
/// use icu::locale::{langid, locale}; |
| 1234 |
/// use writeable::assert_try_writeable_eq; |
| 1235 |
/// |
| 1236 |
/// let prefs = DisplayNamesPreferences::from(locale!("nl")); |
| 1237 |
/// let options = LanguageIdentifierDisplayNameOptions::default(); |
| 1238 |
/// |
| 1239 |
/// // Long heavy formats long names with full data coverage for rare subtags: |
| 1240 |
/// let display_name = LanguageIdentifierDisplayName::try_new_long_heavy( |
| 1241 |
/// prefs, |
| 1242 |
/// langid!("tlh-001"), |
| 1243 |
/// options, |
| 1244 |
/// ) |
| 1245 |
/// .expect("Data should load successfully"); |
| 1246 |
/// assert_try_writeable_eq!(display_name.as_borrowed(), "Klingon (wereld)", Ok(())); |
| 1247 |
/// ``` |
| 1248 |
functions: [ |
| 1249 |
try_new_long_heavy, |
| 1250 |
try_new_long_heavy_with_buffer_provider, |
| 1251 |
try_new_long_heavy_unstable, |
| 1252 |
Self |
| 1253 |
] |
| 1254 |
); |
| 1255 |
|
| 1256 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_long_heavy)] |
| 1257 |
pub fn try_new_long_heavy_unstable<D>( |
| 1258 |
provider: &D, |
| 1259 |
prefs: DisplayNamesPreferences, |
| 1260 |
mut subject: LanguageIdentifier, |
| 1261 |
options: LanguageIdentifierDisplayNameOptions, |
| 1262 |
) -> Result<Self, DataError> |
| 1263 |
where |
| 1264 |
D: ?Sized |
| 1265 |
+ DataProvider<LocaleNamesLanguageLongHeavyV1> |
| 1266 |
+ DataProvider<LocaleNamesLanguageLongLightV1> |
| 1267 |
+ DataProvider<LocaleNamesLanguageMediumHeavyV1> |
| 1268 |
+ DataProvider<LocaleNamesLanguageMediumLightV1> |
| 1269 |
+ DataProvider<LocaleNamesLanguageMediumTinyV1> |
| 1270 |
+ DataProvider<LocaleNamesScriptMediumHeavyV1> |
| 1271 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 1272 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 1273 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 1274 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 1275 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 1276 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 1277 |
{ |
| 1278 |
let locale = make_locale(prefs); |
| 1279 |
let load_dialect = options.should_load_dialect(); |
| 1280 |
let mut language_payload = None; |
| 1281 |
if load_dialect { |
| 1282 |
language_payload = try_load_dialect_name!( |
| 1283 |
provider, |
| 1284 |
&locale, |
| 1285 |
subject, |
| 1286 |
[ |
| 1287 |
LocaleNamesLanguageLongHeavyV1, |
| 1288 |
LocaleNamesLanguageLongLightV1, |
| 1289 |
LocaleNamesLanguageMediumHeavyV1, |
| 1290 |
LocaleNamesLanguageMediumLightV1, |
| 1291 |
LocaleNamesLanguageMediumTinyV1 |
| 1292 |
] |
| 1293 |
)?; |
| 1294 |
} |
| 1295 |
if language_payload.is_none() { |
| 1296 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 1297 |
language_payload = |
| 1298 |
load_one::<LocaleNamesLanguageLongHeavyV1, _, _>(provider, &locale, attrs)? |
| 1299 |
.map_or_else( |
| 1300 |
|| { |
| 1301 |
load_one::<LocaleNamesLanguageLongLightV1, _, _>( |
| 1302 |
provider, &locale, attrs, |
| 1303 |
) |
| 1304 |
}, |
| 1305 |
|p| Ok(Some(p)), |
| 1306 |
)? |
| 1307 |
.map_or_else( |
| 1308 |
|| { |
| 1309 |
load_one::<LocaleNamesLanguageMediumHeavyV1, _, _>( |
| 1310 |
provider, &locale, attrs, |
| 1311 |
) |
| 1312 |
}, |
| 1313 |
|p| Ok(Some(p)), |
| 1314 |
)? |
| 1315 |
.map_or_else( |
| 1316 |
|| { |
| 1317 |
load_one::<LocaleNamesLanguageMediumLightV1, _, _>( |
| 1318 |
provider, &locale, attrs, |
| 1319 |
) |
| 1320 |
}, |
| 1321 |
|p| Ok(Some(p)), |
| 1322 |
)? |
| 1323 |
.map_or_else( |
| 1324 |
|| { |
| 1325 |
load_one::<LocaleNamesLanguageMediumTinyV1, _, _>( |
| 1326 |
provider, &locale, attrs, |
| 1327 |
) |
| 1328 |
}, |
| 1329 |
|p| Ok(Some(p)), |
| 1330 |
)? |
| 1331 |
.map(|payload: DataPayload<StringMarker>| { |
| 1332 |
payload.map_project(|p, _| MenuNamePartsOrString::String(p)) |
| 1333 |
}); |
| 1334 |
} |
| 1335 |
let language_payload = match language_payload { |
| 1336 |
Some(payload) => DataPayloadOr::from_payload(payload), |
| 1337 |
None => DataPayloadOr::from_other(subject.language), |
| 1338 |
}; |
| 1339 |
|
| 1340 |
let qualifiers = QualifiersOwned::try_new_heavy_unstable(provider, prefs, subject)?; |
| 1341 |
Ok(Self { |
| 1342 |
language_payload, |
| 1343 |
qualifiers, |
| 1344 |
}) |
| 1345 |
} |
| 1346 |
|
| 1347 |
icu_provider::gen_buffer_data_constructors!( |
| 1348 |
(prefs: DisplayNamesPreferences, subject: LanguageIdentifier, options: LanguageIdentifierDisplayNameOptions) -> result: Result<Self, DataError>, |
| 1349 |
/// Loads a menu-style language display name in a formatting locale using compiled data. |
| 1350 |
/// |
| 1351 |
/// The `heavy` constructor includes additional data coverage for subtags that are |
| 1352 |
/// less commonly formatted in the target locale. |
| 1353 |
/// See the [class docs](Self) for information on which constructor to use. |
| 1354 |
/// |
| 1355 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 1356 |
/// |
| 1357 |
/// # Examples |
| 1358 |
/// |
| 1359 |
/// ``` |
| 1360 |
/// use icu::locale::names::{ |
| 1361 |
/// DisplayNamesPreferences, LanguageIdentifierDisplayName, LanguageIdentifierDisplayNameOptions, |
| 1362 |
/// }; |
| 1363 |
/// use icu::locale::{langid, locale}; |
| 1364 |
/// use writeable::assert_try_writeable_eq; |
| 1365 |
/// |
| 1366 |
/// let prefs = DisplayNamesPreferences::from(locale!("nl")); |
| 1367 |
/// let options = LanguageIdentifierDisplayNameOptions::default(); |
| 1368 |
/// |
| 1369 |
/// // Menu heavy formats menu-style names with full data coverage for rare subtags: |
| 1370 |
/// let display_name = LanguageIdentifierDisplayName::try_new_menu_heavy( |
| 1371 |
/// prefs, |
| 1372 |
/// langid!("tlh-001"), |
| 1373 |
/// options, |
| 1374 |
/// ) |
| 1375 |
/// .expect("Data should load successfully"); |
| 1376 |
/// assert_try_writeable_eq!(display_name.as_borrowed(), "Klingon (wereld)", Ok(())); |
| 1377 |
/// ``` |
| 1378 |
functions: [ |
| 1379 |
try_new_menu_heavy, |
| 1380 |
try_new_menu_heavy_with_buffer_provider, |
| 1381 |
try_new_menu_heavy_unstable, |
| 1382 |
Self |
| 1383 |
] |
| 1384 |
); |
| 1385 |
|
| 1386 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_menu_heavy)] |
| 1387 |
pub fn try_new_menu_heavy_unstable<D>( |
| 1388 |
provider: &D, |
| 1389 |
prefs: DisplayNamesPreferences, |
| 1390 |
subject: LanguageIdentifier, |
| 1391 |
_options: LanguageIdentifierDisplayNameOptions, |
| 1392 |
) -> Result<Self, DataError> |
| 1393 |
where |
| 1394 |
D: ?Sized |
| 1395 |
+ DataProvider<LocaleNamesLanguageMenuMediumHeavyV1> |
| 1396 |
+ DataProvider<LocaleNamesLanguageMenuMediumLightV1> |
| 1397 |
+ DataProvider<LocaleNamesLanguageMediumHeavyV1> |
| 1398 |
+ DataProvider<LocaleNamesLanguageMediumLightV1> |
| 1399 |
+ DataProvider<LocaleNamesLanguageMediumTinyV1> |
| 1400 |
+ DataProvider<LocaleNamesScriptMediumHeavyV1> |
| 1401 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 1402 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 1403 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 1404 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 1405 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 1406 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 1407 |
{ |
| 1408 |
let locale = make_locale(prefs); |
| 1409 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 1410 |
let mut language_payload = |
| 1411 |
load_one::<LocaleNamesLanguageMenuMediumHeavyV1, _, _>(provider, &locale, attrs)? |
| 1412 |
.map_or_else( |
| 1413 |
|| { |
| 1414 |
load_one::<LocaleNamesLanguageMenuMediumLightV1, _, _>( |
| 1415 |
provider, &locale, attrs, |
| 1416 |
) |
| 1417 |
}, |
| 1418 |
|p| Ok(Some(p)), |
| 1419 |
)? |
| 1420 |
.map(|payload: DataPayload<MenuNamePartsMarker>| { |
| 1421 |
payload.map_project(|p, _| MenuNamePartsOrString::MenuNameParts(p)) |
| 1422 |
}); |
| 1423 |
if language_payload.is_none() { |
| 1424 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 1425 |
language_payload = |
| 1426 |
load_one::<LocaleNamesLanguageMediumHeavyV1, _, _>(provider, &locale, attrs)? |
| 1427 |
.map_or_else( |
| 1428 |
|| { |
| 1429 |
load_one::<LocaleNamesLanguageMediumLightV1, _, _>( |
| 1430 |
provider, &locale, attrs, |
| 1431 |
) |
| 1432 |
}, |
| 1433 |
|p| Ok(Some(p)), |
| 1434 |
)? |
| 1435 |
.map_or_else( |
| 1436 |
|| { |
| 1437 |
load_one::<LocaleNamesLanguageMediumTinyV1, _, _>( |
| 1438 |
provider, &locale, attrs, |
| 1439 |
) |
| 1440 |
}, |
| 1441 |
|p| Ok(Some(p)), |
| 1442 |
)? |
| 1443 |
.map(|payload: DataPayload<StringMarker>| { |
| 1444 |
payload.map_project(|p, _| MenuNamePartsOrString::String(p)) |
| 1445 |
}); |
| 1446 |
} |
| 1447 |
let language_payload = match language_payload { |
| 1448 |
Some(payload) => DataPayloadOr::from_payload(payload), |
| 1449 |
None => DataPayloadOr::from_other(subject.language), |
| 1450 |
}; |
| 1451 |
|
| 1452 |
let qualifiers = QualifiersOwned::try_new_heavy_unstable(provider, prefs, subject)?; |
| 1453 |
Ok(Self { |
| 1454 |
language_payload, |
| 1455 |
qualifiers, |
| 1456 |
}) |
| 1457 |
} |
| 1458 |
|
| 1459 |
icu_provider::gen_buffer_data_constructors!( |
| 1460 |
(prefs: DisplayNamesPreferences, subject: LanguageIdentifier, options: LanguageIdentifierDisplayNameOptions) -> result: Result<Self, DataError>, |
| 1461 |
/// Loads a short menu-style language display name in a formatting locale using compiled data. |
| 1462 |
/// |
| 1463 |
/// Falls back to default (medium) length if a short name is not available. |
| 1464 |
/// |
| 1465 |
/// The `heavy` constructor includes additional data coverage for subtags that are |
| 1466 |
/// less commonly formatted in the target locale. |
| 1467 |
/// See the [class docs](Self) for information on which constructor to use. |
| 1468 |
/// |
| 1469 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 1470 |
/// |
| 1471 |
/// # Examples |
| 1472 |
/// |
| 1473 |
/// ``` |
| 1474 |
/// use icu::locale::names::{ |
| 1475 |
/// DisplayNamesPreferences, LanguageIdentifierDisplayName, LanguageIdentifierDisplayNameOptions, |
| 1476 |
/// }; |
| 1477 |
/// use icu::locale::{langid, locale}; |
| 1478 |
/// use writeable::assert_try_writeable_eq; |
| 1479 |
/// |
| 1480 |
/// let prefs = DisplayNamesPreferences::from(locale!("nl")); |
| 1481 |
/// let options = LanguageIdentifierDisplayNameOptions::default(); |
| 1482 |
/// |
| 1483 |
/// // Short menu heavy formats short menu-style names with full data coverage for rare subtags: |
| 1484 |
/// let display_name = LanguageIdentifierDisplayName::try_new_short_menu_heavy( |
| 1485 |
/// prefs, |
| 1486 |
/// langid!("tlh-001"), |
| 1487 |
/// options, |
| 1488 |
/// ) |
| 1489 |
/// .expect("Data should load successfully"); |
| 1490 |
/// assert_try_writeable_eq!(display_name.as_borrowed(), "Klingon (wereld)", Ok(())); |
| 1491 |
/// ``` |
| 1492 |
functions: [ |
| 1493 |
try_new_short_menu_heavy, |
| 1494 |
try_new_short_menu_heavy_with_buffer_provider, |
| 1495 |
try_new_short_menu_heavy_unstable, |
| 1496 |
Self |
| 1497 |
] |
| 1498 |
); |
| 1499 |
|
| 1500 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new_short_menu_heavy)] |
| 1501 |
pub fn try_new_short_menu_heavy_unstable<D>( |
| 1502 |
provider: &D, |
| 1503 |
prefs: DisplayNamesPreferences, |
| 1504 |
subject: LanguageIdentifier, |
| 1505 |
_options: LanguageIdentifierDisplayNameOptions, |
| 1506 |
) -> Result<Self, DataError> |
| 1507 |
where |
| 1508 |
D: ?Sized |
| 1509 |
+ DataProvider<LocaleNamesLanguageMenuMediumHeavyV1> |
| 1510 |
+ DataProvider<LocaleNamesLanguageMenuMediumLightV1> |
| 1511 |
+ DataProvider<LocaleNamesLanguageShortHeavyV1> |
| 1512 |
+ DataProvider<LocaleNamesLanguageShortLightV1> |
| 1513 |
+ DataProvider<LocaleNamesLanguageMediumHeavyV1> |
| 1514 |
+ DataProvider<LocaleNamesLanguageMediumLightV1> |
| 1515 |
+ DataProvider<LocaleNamesLanguageMediumTinyV1> |
| 1516 |
+ DataProvider<LocaleNamesScriptShortHeavyV1> |
| 1517 |
+ DataProvider<LocaleNamesScriptMediumHeavyV1> |
| 1518 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 1519 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 1520 |
+ DataProvider<LocaleNamesRegionShortLightV1> |
| 1521 |
+ DataProvider<LocaleNamesRegionShortTinyV1> |
| 1522 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 1523 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 1524 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 1525 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 1526 |
{ |
| 1527 |
let locale = make_locale(prefs); |
| 1528 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 1529 |
let mut language_payload = |
| 1530 |
load_one::<LocaleNamesLanguageMenuMediumHeavyV1, _, _>(provider, &locale, attrs)? |
| 1531 |
.map_or_else( |
| 1532 |
|| { |
| 1533 |
load_one::<LocaleNamesLanguageMenuMediumLightV1, _, _>( |
| 1534 |
provider, &locale, attrs, |
| 1535 |
) |
| 1536 |
}, |
| 1537 |
|p| Ok(Some(p)), |
| 1538 |
)? |
| 1539 |
.map(|payload: DataPayload<MenuNamePartsMarker>| { |
| 1540 |
payload.map_project(|p, _| MenuNamePartsOrString::MenuNameParts(p)) |
| 1541 |
}); |
| 1542 |
if language_payload.is_none() { |
| 1543 |
let attrs = make_attributes_for_subtag(&subject.language); |
| 1544 |
language_payload = |
| 1545 |
load_one::<LocaleNamesLanguageShortHeavyV1, _, _>(provider, &locale, attrs)? |
| 1546 |
.map_or_else( |
| 1547 |
|| { |
| 1548 |
load_one::<LocaleNamesLanguageShortLightV1, _, _>( |
| 1549 |
provider, &locale, attrs, |
| 1550 |
) |
| 1551 |
}, |
| 1552 |
|p| Ok(Some(p)), |
| 1553 |
)? |
| 1554 |
.map_or_else( |
| 1555 |
|| { |
| 1556 |
load_one::<LocaleNamesLanguageMediumHeavyV1, _, _>( |
| 1557 |
provider, &locale, attrs, |
| 1558 |
) |
| 1559 |
}, |
| 1560 |
|p| Ok(Some(p)), |
| 1561 |
)? |
| 1562 |
.map_or_else( |
| 1563 |
|| { |
| 1564 |
load_one::<LocaleNamesLanguageMediumLightV1, _, _>( |
| 1565 |
provider, &locale, attrs, |
| 1566 |
) |
| 1567 |
}, |
| 1568 |
|p| Ok(Some(p)), |
| 1569 |
)? |
| 1570 |
.map_or_else( |
| 1571 |
|| { |
| 1572 |
load_one::<LocaleNamesLanguageMediumTinyV1, _, _>( |
| 1573 |
provider, &locale, attrs, |
| 1574 |
) |
| 1575 |
}, |
| 1576 |
|p| Ok(Some(p)), |
| 1577 |
)? |
| 1578 |
.map(|payload: DataPayload<StringMarker>| { |
| 1579 |
payload.map_project(|p, _| MenuNamePartsOrString::String(p)) |
| 1580 |
}); |
| 1581 |
} |
| 1582 |
let language_payload = match language_payload { |
| 1583 |
Some(payload) => DataPayloadOr::from_payload(payload), |
| 1584 |
None => DataPayloadOr::from_other(subject.language), |
| 1585 |
}; |
| 1586 |
|
| 1587 |
let qualifiers = QualifiersOwned::try_new_short_heavy_unstable(provider, prefs, subject)?; |
| 1588 |
Ok(Self { |
| 1589 |
language_payload, |
| 1590 |
qualifiers, |
| 1591 |
}) |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
impl QualifiersOwned { |
| 1596 |
fn try_new_internal_unstable<D, FS, FR, FV>( |
| 1597 |
provider: &D, |
| 1598 |
prefs: DisplayNamesPreferences, |
| 1599 |
subject: LanguageIdentifier, |
| 1600 |
load_script: FS, |
| 1601 |
load_region: FR, |
| 1602 |
load_variant: FV, |
| 1603 |
) -> Result<Self, DataError> |
| 1604 |
where |
| 1605 |
D: ?Sized + DataProvider<LocaleNamesEssentialsV1>, |
| 1606 |
FS: Fn(&D, DisplayNamesPreferences, Script) -> Result<ScriptDisplayName, DataError>, |
| 1607 |
FR: Fn(&D, DisplayNamesPreferences, Region) -> Result<RegionDisplayName, DataError>, |
| 1608 |
FV: Fn(&D, DisplayNamesPreferences, Variant) -> Result<VariantDisplayName, DataError>, |
| 1609 |
{ |
| 1610 |
// Step 2: Load script name (if present in subject) |
| 1611 |
let script_payload = if let Some(script) = subject.script { |
| 1612 |
match load_script(provider, prefs, script).allow_identifier_not_found()? { |
| 1613 |
Some(obj) => obj.payload.cast().map_other(Some), |
| 1614 |
None => DataPayloadOr::from_other(Some(script)), |
| 1615 |
} |
| 1616 |
} else { |
| 1617 |
DataPayloadOr::from_other(None) |
| 1618 |
}; |
| 1619 |
|
| 1620 |
// Step 3: Load region name (if present in subject) |
| 1621 |
let region_payload = if let Some(region) = subject.region { |
| 1622 |
match load_region(provider, prefs, region).allow_identifier_not_found()? { |
| 1623 |
Some(obj) => obj.payload.cast().map_other(Some), |
| 1624 |
None => DataPayloadOr::from_other(Some(region)), |
| 1625 |
} |
| 1626 |
} else { |
| 1627 |
DataPayloadOr::from_other(None) |
| 1628 |
}; |
| 1629 |
|
| 1630 |
// Step 4: Load variant names (if present in subject) |
| 1631 |
let load_variant_helper = |variant: Variant| -> Result< |
| 1632 |
DataPayloadOr<ErasedMarker<VarZeroCow<'static, str>>, Variant>, |
| 1633 |
DataError, |
| 1634 |
> { |
| 1635 |
match load_variant(provider, prefs, variant).allow_identifier_not_found()? { |
| 1636 |
Some(obj) => Ok(obj.payload.cast()), |
| 1637 |
None => Ok(DataPayloadOr::from_other(variant)), |
| 1638 |
} |
| 1639 |
}; |
| 1640 |
|
| 1641 |
let mut variant_results = subject |
| 1642 |
.variants |
| 1643 |
.iter() |
| 1644 |
.map(|variant| load_variant_helper(*variant)); |
| 1645 |
|
| 1646 |
let variant_payloads = if let Some(first) = variant_results.next() { |
| 1647 |
if let Some(second) = variant_results.next() { |
| 1648 |
// 2 or more variants |
| 1649 |
let payload_vec = [first, second] |
| 1650 |
.into_iter() |
| 1651 |
.chain(variant_results) |
| 1652 |
.collect::<Result<Vec<_>, _>>()?; |
| 1653 |
DataPayloadOr::from_other(Ok(payload_vec)) |
| 1654 |
} else { |
| 1655 |
// 1 variant |
| 1656 |
match first?.into_inner() { |
| 1657 |
Ok(payload) => DataPayloadOr::from_payload(payload), |
| 1658 |
Err(fallback_code) => DataPayloadOr::from_other(Err(fallback_code)), |
| 1659 |
} |
| 1660 |
} |
| 1661 |
} else { |
| 1662 |
// 0 variants |
| 1663 |
DataPayloadOr::from_other(Ok(Vec::new())) |
| 1664 |
}; |
| 1665 |
|
| 1666 |
// Step 5: Load essentials |
| 1667 |
let essentials_payload = provider |
| 1668 |
.load(DataRequest { |
| 1669 |
id: DataIdentifierBorrowed::for_locale(&LocaleNamesEssentialsV1::make_locale( |
| 1670 |
prefs.locale_preferences, |
| 1671 |
)), |
| 1672 |
..Default::default() |
| 1673 |
})? |
| 1674 |
.payload; |
| 1675 |
|
| 1676 |
Ok(Self { |
| 1677 |
script_payload, |
| 1678 |
region_payload, |
| 1679 |
variant_payloads, |
| 1680 |
essentials_payload, |
| 1681 |
}) |
| 1682 |
} |
| 1683 |
|
| 1684 |
fn try_new_light_unstable<D>( |
| 1685 |
provider: &D, |
| 1686 |
prefs: DisplayNamesPreferences, |
| 1687 |
subject: LanguageIdentifier, |
| 1688 |
) -> Result<Self, DataError> |
| 1689 |
where |
| 1690 |
D: ?Sized |
| 1691 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 1692 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 1693 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 1694 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 1695 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 1696 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 1697 |
{ |
| 1698 |
Self::try_new_internal_unstable( |
| 1699 |
provider, |
| 1700 |
prefs, |
| 1701 |
subject, |
| 1702 |
ScriptDisplayName::try_new_light_unstable, |
| 1703 |
RegionDisplayName::try_new_light_unstable, |
| 1704 |
VariantDisplayName::try_new_heavy_unstable, |
| 1705 |
) |
| 1706 |
} |
| 1707 |
|
| 1708 |
fn try_new_tiny_unstable<D>( |
| 1709 |
provider: &D, |
| 1710 |
prefs: DisplayNamesPreferences, |
| 1711 |
subject: LanguageIdentifier, |
| 1712 |
) -> Result<Self, DataError> |
| 1713 |
where |
| 1714 |
D: ?Sized |
| 1715 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 1716 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 1717 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 1718 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 1719 |
{ |
| 1720 |
Self::try_new_internal_unstable( |
| 1721 |
provider, |
| 1722 |
prefs, |
| 1723 |
subject, |
| 1724 |
ScriptDisplayName::try_new_tiny_unstable, |
| 1725 |
RegionDisplayName::try_new_tiny_unstable, |
| 1726 |
VariantDisplayName::try_new_heavy_unstable, |
| 1727 |
) |
| 1728 |
} |
| 1729 |
|
| 1730 |
fn try_new_short_light_unstable<D>( |
| 1731 |
provider: &D, |
| 1732 |
prefs: DisplayNamesPreferences, |
| 1733 |
subject: LanguageIdentifier, |
| 1734 |
) -> Result<Self, DataError> |
| 1735 |
where |
| 1736 |
D: ?Sized |
| 1737 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 1738 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 1739 |
+ DataProvider<LocaleNamesRegionShortLightV1> |
| 1740 |
+ DataProvider<LocaleNamesRegionShortTinyV1> |
| 1741 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 1742 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 1743 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 1744 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 1745 |
{ |
| 1746 |
Self::try_new_internal_unstable( |
| 1747 |
provider, |
| 1748 |
prefs, |
| 1749 |
subject, |
| 1750 |
ScriptDisplayName::try_new_light_unstable, |
| 1751 |
RegionDisplayName::try_new_short_light_unstable, |
| 1752 |
VariantDisplayName::try_new_heavy_unstable, |
| 1753 |
) |
| 1754 |
} |
| 1755 |
|
| 1756 |
fn try_new_heavy_unstable<D>( |
| 1757 |
provider: &D, |
| 1758 |
prefs: DisplayNamesPreferences, |
| 1759 |
subject: LanguageIdentifier, |
| 1760 |
) -> Result<Self, DataError> |
| 1761 |
where |
| 1762 |
D: ?Sized |
| 1763 |
+ DataProvider<LocaleNamesScriptMediumHeavyV1> |
| 1764 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 1765 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 1766 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 1767 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 1768 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 1769 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 1770 |
{ |
| 1771 |
Self::try_new_internal_unstable( |
| 1772 |
provider, |
| 1773 |
prefs, |
| 1774 |
subject, |
| 1775 |
ScriptDisplayName::try_new_heavy_unstable, |
| 1776 |
RegionDisplayName::try_new_light_unstable, |
| 1777 |
VariantDisplayName::try_new_heavy_unstable, |
| 1778 |
) |
| 1779 |
} |
| 1780 |
|
| 1781 |
fn try_new_short_heavy_unstable<D>( |
| 1782 |
provider: &D, |
| 1783 |
prefs: DisplayNamesPreferences, |
| 1784 |
subject: LanguageIdentifier, |
| 1785 |
) -> Result<Self, DataError> |
| 1786 |
where |
| 1787 |
D: ?Sized |
| 1788 |
+ DataProvider<LocaleNamesScriptShortHeavyV1> |
| 1789 |
+ DataProvider<LocaleNamesScriptMediumHeavyV1> |
| 1790 |
+ DataProvider<LocaleNamesScriptMediumLightV1> |
| 1791 |
+ DataProvider<LocaleNamesScriptMediumTinyV1> |
| 1792 |
+ DataProvider<LocaleNamesRegionShortLightV1> |
| 1793 |
+ DataProvider<LocaleNamesRegionShortTinyV1> |
| 1794 |
+ DataProvider<LocaleNamesRegionMediumLightV1> |
| 1795 |
+ DataProvider<LocaleNamesRegionMediumTinyV1> |
| 1796 |
+ DataProvider<LocaleNamesVariantMediumHeavyV1> |
| 1797 |
+ DataProvider<LocaleNamesEssentialsV1>, |
| 1798 |
{ |
| 1799 |
Self::try_new_internal_unstable( |
| 1800 |
provider, |
| 1801 |
prefs, |
| 1802 |
subject, |
| 1803 |
ScriptDisplayName::try_new_short_heavy_unstable, |
| 1804 |
RegionDisplayName::try_new_short_light_unstable, |
| 1805 |
VariantDisplayName::try_new_heavy_unstable, |
| 1806 |
) |
| 1807 |
} |
| 1808 |
} |
| 1809 |
|
| 1810 |
impl LanguageIdentifierDisplayName { |
| 1811 |
/// Returns a borrowed version of this display name |
| 1812 |
/// suitable for writing out to a string. |
| 1813 |
pub fn as_borrowed(&self) -> LanguageIdentifierDisplayNameBorrowed<'_> { |
| 1814 |
let mut qualifiers = self.qualifiers.as_borrowed(); |
| 1815 |
let base_name = match self.language_payload.get() { |
| 1816 |
Ok(MenuNamePartsOrString::String(string)) => NameOrFallback(Ok(string.as_ref())), |
| 1817 |
Ok(MenuNamePartsOrString::MenuNameParts(parts)) => { |
| 1818 |
if !parts.extension().is_empty() { |
| 1819 |
qualifiers.menu_extension = Some(parts.extension()); |
| 1820 |
} |
| 1821 |
NameOrFallback(Ok(parts.core())) |
| 1822 |
} |
| 1823 |
Err(lang) => NameOrFallback(Err(lang.as_str())), |
| 1824 |
}; |
| 1825 |
|
| 1826 |
LanguageIdentifierDisplayNameBorrowed(LossyWrap(LanguageIdentifierDisplayNameInner { |
| 1827 |
base_name, |
| 1828 |
qualifiers, |
| 1829 |
})) |
| 1830 |
} |
| 1831 |
} |
| 1832 |
|
| 1833 |
impl QualifiersOwned { |
| 1834 |
pub fn as_borrowed(&self) -> QualifiersBorrowed<'_> { |
| 1835 |
let script = match self.script_payload.get() { |
| 1836 |
Ok(p) => Some(NameOrFallback(Ok(p.as_ref()))), |
| 1837 |
Err(Some(script)) => Some(NameOrFallback(Err(script.as_str()))), |
| 1838 |
Err(None) => None, |
| 1839 |
}; |
| 1840 |
|
| 1841 |
let region = match self.region_payload.get() { |
| 1842 |
Ok(p) => Some(NameOrFallback(Ok(p.as_ref()))), |
| 1843 |
Err(Some(region)) => Some(NameOrFallback(Err(region.as_str()))), |
| 1844 |
Err(None) => None, |
| 1845 |
}; |
| 1846 |
|
| 1847 |
let variants = match self.variant_payloads.get() { |
| 1848 |
Ok(variant_name) => BorrowedVariants::One(NameOrFallback(Ok(variant_name))), |
| 1849 |
Err(Ok(vec)) => BorrowedVariants::Slice(vec.as_slice()), |
| 1850 |
Err(Err(variant)) => BorrowedVariants::One(NameOrFallback(Err(variant.as_str()))), |
| 1851 |
}; |
| 1852 |
|
| 1853 |
QualifiersBorrowed { |
| 1854 |
menu_extension: None, |
| 1855 |
script, |
| 1856 |
region, |
| 1857 |
variants, |
| 1858 |
glue: &self.essentials_payload.get().locale_pattern, |
| 1859 |
separator: &self.essentials_payload.get().locale_separator, |
| 1860 |
} |
| 1861 |
} |
| 1862 |
} |
| 1863 |
|
| 1864 |
/// Borrowed variants representation to avoid heap allocation. |
| 1865 |
/// |
| 1866 |
/// Note: if a compiled-data-only constructor is added in the future, |
| 1867 |
/// this will need a new variant for a vec of borrowed variant names. |
| 1868 |
#[derive(Debug, Clone, Copy)] |
| 1869 |
enum BorrowedVariants<'a> { |
| 1870 |
One(NameOrFallback<'a>), |
| 1871 |
Slice(&'a [DataPayloadOr<ErasedMarker<VarZeroCow<'static, str>>, Variant>]), |
| 1872 |
} |
| 1873 |
|
| 1874 |
impl BorrowedVariants<'_> { |
| 1875 |
#[inline] |
| 1876 |
fn is_empty(&self) -> bool { |
| 1877 |
matches!(self, Self::Slice([])) |
| 1878 |
} |
| 1879 |
} |
| 1880 |
|
| 1881 |
/// A localized display name for a language identifier. |
| 1882 |
/// |
| 1883 |
/// See [`LanguageIdentifierDisplayName`]. |
| 1884 |
#[derive(Debug, Clone, Copy)] |
| 1885 |
pub struct LanguageIdentifierDisplayNameBorrowed<'a>( |
| 1886 |
pub(crate) LossyWrap<LanguageIdentifierDisplayNameInner<'a>>, |
| 1887 |
); |
| 1888 |
|
| 1889 |
/// A struct implementing [`TryWriteable`] that returns a [`LanguageIdentifierNameFallbackError`] |
| 1890 |
#[derive(Debug, Clone, Copy)] |
| 1891 |
struct NameOrFallback<'a>(Result<&'a str, &'a str>); |
| 1892 |
|
| 1893 |
writeable::impl_try_writeable_delegate!( |
| 1894 |
NameOrFallback<'a>, |
| 1895 |
|&self| &self.0, |
| 1896 |
Error = LanguageIdentifierNameFallbackError, |
| 1897 |
|_fallback_str| LanguageIdentifierNameFallbackError, |
| 1898 |
where 'a |
| 1899 |
); |
| 1900 |
|
| 1901 |
#[derive(Debug, Clone, Copy)] |
| 1902 |
pub(crate) struct LanguageIdentifierDisplayNameInner<'a> { |
| 1903 |
base_name: NameOrFallback<'a>, |
| 1904 |
qualifiers: QualifiersBorrowed<'a>, |
| 1905 |
} |
| 1906 |
|
| 1907 |
writeable::impl_try_writeable_delegate!( |
| 1908 |
LanguageIdentifierDisplayNameBorrowed<'_>, |
| 1909 |
|&self| &self.0.0, |
| 1910 |
Error = LanguageIdentifierNameFallbackError |
| 1911 |
); |
| 1912 |
|
| 1913 |
writeable::impl_writeable_delegate!(LanguageIdentifierDisplayNameBorrowed<'_>, |&self| &self.0); |
| 1914 |
|
| 1915 |
writeable::impl_display_with_writeable!(LanguageIdentifierDisplayNameBorrowed<'_>); |
| 1916 |
|
| 1917 |
#[derive(Debug, Copy, Clone)] |
| 1918 |
struct QualifiersBorrowed<'a> { |
| 1919 |
menu_extension: Option<&'a str>, |
| 1920 |
script: Option<NameOrFallback<'a>>, |
| 1921 |
region: Option<NameOrFallback<'a>>, |
| 1922 |
variants: BorrowedVariants<'a>, |
| 1923 |
glue: &'a DoublePlaceholderPattern, |
| 1924 |
separator: &'a DoublePlaceholderPattern, |
| 1925 |
} |
| 1926 |
|
| 1927 |
impl<'a> QualifiersBorrowed<'a> { |
| 1928 |
fn separator_str(&self) -> &'a str { |
| 1929 |
let mut separator_str = ", "; |
| 1930 |
for item in self.separator.iter() { |
| 1931 |
if let PatternItem::Literal(s) = item { |
| 1932 |
separator_str = s; |
| 1933 |
break; |
| 1934 |
} |
| 1935 |
} |
| 1936 |
separator_str |
| 1937 |
} |
| 1938 |
|
| 1939 |
fn is_empty(&self) -> bool { |
| 1940 |
self.menu_extension.is_none() |
| 1941 |
&& self.script.is_none() |
| 1942 |
&& self.region.is_none() |
| 1943 |
&& self.variants.is_empty() |
| 1944 |
} |
| 1945 |
} |
| 1946 |
|
| 1947 |
impl<'a> TryWriteable for QualifiersBorrowed<'a> { |
| 1948 |
type Error = LanguageIdentifierNameFallbackError; |
| 1949 |
|
| 1950 |
fn try_write_to_parts<S: PartsWrite + ?Sized>( |
| 1951 |
&self, |
| 1952 |
sink: &mut S, |
| 1953 |
) -> Result<Result<(), Self::Error>, core::fmt::Error> { |
| 1954 |
// TODO: See whether we can share this code with the list component. |
| 1955 |
let mut first = true; |
| 1956 |
let separator_str = self.separator_str(); |
| 1957 |
|
| 1958 |
if let Some(menu_extension) = self.menu_extension { |
| 1959 |
sink.write_str(menu_extension)?; |
| 1960 |
first = false; |
| 1961 |
} |
| 1962 |
|
| 1963 |
let mut write_item = |sink: &mut S, |
| 1964 |
res: NameOrFallback| |
| 1965 |
-> Result< |
| 1966 |
Result<(), LanguageIdentifierNameFallbackError>, |
| 1967 |
core::fmt::Error, |
| 1968 |
> { |
| 1969 |
if !first { |
| 1970 |
sink.write_str(separator_str)?; |
| 1971 |
} |
| 1972 |
first = false; |
| 1973 |
res.try_write_to_parts(sink) |
| 1974 |
}; |
| 1975 |
|
| 1976 |
let mut result = Ok(()); |
| 1977 |
if let Some(script) = self.script { |
| 1978 |
result = result.and(write_item(sink, script)?); |
| 1979 |
} |
| 1980 |
if let Some(region) = self.region { |
| 1981 |
result = result.and(write_item(sink, region)?); |
| 1982 |
} |
| 1983 |
match self.variants { |
| 1984 |
BorrowedVariants::One(variant) => { |
| 1985 |
result = result.and(write_item(sink, variant)?); |
| 1986 |
} |
| 1987 |
BorrowedVariants::Slice(slice) => { |
| 1988 |
for item in slice.iter() { |
| 1989 |
let res = match item.get() { |
| 1990 |
Ok(p) => NameOrFallback(Ok(p.as_ref())), |
| 1991 |
Err(var) => NameOrFallback(Err(var.as_str())), |
| 1992 |
}; |
| 1993 |
result = result.and(write_item(sink, res)?); |
| 1994 |
} |
| 1995 |
} |
| 1996 |
} |
| 1997 |
|
| 1998 |
Ok(result) |
| 1999 |
} |
| 2000 |
|
| 2001 |
fn writeable_length_hint(&self) -> LengthHint { |
| 2002 |
let mut length_hint = LengthHint::exact(0); |
| 2003 |
let mut num_items = 0; |
| 2004 |
if let Some(menu_extension) = self.menu_extension { |
| 2005 |
length_hint += writeable::Writeable::writeable_length_hint(&menu_extension); |
| 2006 |
num_items += 1; |
| 2007 |
} |
| 2008 |
if let Some(script) = self.script { |
| 2009 |
length_hint += script.writeable_length_hint(); |
| 2010 |
num_items += 1; |
| 2011 |
} |
| 2012 |
if let Some(region) = self.region { |
| 2013 |
length_hint += region.writeable_length_hint(); |
| 2014 |
num_items += 1; |
| 2015 |
} |
| 2016 |
match self.variants { |
| 2017 |
BorrowedVariants::One(variant) => { |
| 2018 |
length_hint += variant.writeable_length_hint(); |
| 2019 |
num_items += 1; |
| 2020 |
} |
| 2021 |
BorrowedVariants::Slice(slice) => { |
| 2022 |
for item in slice.iter() { |
| 2023 |
length_hint += match item.get() { |
| 2024 |
Ok(p) => writeable::Writeable::writeable_length_hint(&**p), |
| 2025 |
Err(var) => writeable::Writeable::writeable_length_hint(var.as_str()), |
| 2026 |
}; |
| 2027 |
num_items += 1; |
| 2028 |
} |
| 2029 |
} |
| 2030 |
} |
| 2031 |
length_hint += LengthHint::exact(self.separator_str().len() * (num_items - 1)); |
| 2032 |
length_hint |
| 2033 |
} |
| 2034 |
} |
| 2035 |
|
| 2036 |
impl<'a> TryWriteable for LanguageIdentifierDisplayNameInner<'a> { |
| 2037 |
type Error = LanguageIdentifierNameFallbackError; |
| 2038 |
|
| 2039 |
fn try_write_to_parts<S: PartsWrite + ?Sized>( |
| 2040 |
&self, |
| 2041 |
sink: &mut S, |
| 2042 |
) -> Result<Result<(), Self::Error>, core::fmt::Error> { |
| 2043 |
if self.qualifiers.is_empty() { |
| 2044 |
self.base_name.try_write_to_parts(sink) |
| 2045 |
} else { |
| 2046 |
let result = self |
| 2047 |
.qualifiers |
| 2048 |
.glue |
| 2049 |
.try_interpolate(TryWrap((self.base_name, self.qualifiers))) |
| 2050 |
.try_write_to_parts(sink)?; |
| 2051 |
Ok(result.map_err(either::Either::into_inner)) |
| 2052 |
} |
| 2053 |
} |
| 2054 |
|
| 2055 |
fn writeable_length_hint(&self) -> LengthHint { |
| 2056 |
if self.qualifiers.is_empty() { |
| 2057 |
self.base_name.writeable_length_hint() |
| 2058 |
} else { |
| 2059 |
self.qualifiers |
| 2060 |
.glue |
| 2061 |
.try_interpolate(TryWrap((self.base_name, self.qualifiers))) |
| 2062 |
.writeable_length_hint() |
| 2063 |
} |
| 2064 |
} |
| 2065 |
|
| 2066 |
fn try_writeable_borrow(&self) -> Option<Result<&str, (Self::Error, &str)>> { |
| 2067 |
if self.qualifiers.is_empty() { |
| 2068 |
self.base_name.try_writeable_borrow() |
| 2069 |
} else { |
| 2070 |
None |
| 2071 |
} |
| 2072 |
} |
| 2073 |
} |
| 2074 |
|
| 2075 |
#[cfg(test)] |
| 2076 |
mod tests { |
| 2077 |
use super::*; |
| 2078 |
use icu_locale_core::{langid, locale}; |
| 2079 |
|
| 2080 |
#[test] |
| 2081 |
fn test_language_identifier_display_name_owned_table() { |
| 2082 |
let prefs_en = DisplayNamesPreferences::from(locale!("en")); |
| 2083 |
let options = LanguageIdentifierDisplayNameOptions::default(); |
| 2084 |
let inputs = [langid!("en-US"), langid!("zh"), langid!("az")]; |
| 2085 |
|
| 2086 |
macro_rules! check_row { |
| 2087 |
($constructor:ident) => { |
| 2088 |
let items = inputs.iter().map(|id| { |
| 2089 |
LanguageIdentifierDisplayName::$constructor(prefs_en, id.clone(), options).map( |
| 2090 |
|display_name| { |
| 2091 |
display_name |
| 2092 |
.as_borrowed() |
| 2093 |
.try_write_to_string() |
| 2094 |
.map(|s| s.into_owned()) |
| 2095 |
.map_err(|e| e.0) |
| 2096 |
}, |
| 2097 |
) |
| 2098 |
}); |
| 2099 |
assert_eq!( |
| 2100 |
super::super::format_table_row(stringify!($constructor), items), |
| 2101 |
table_row!($constructor) |
| 2102 |
); |
| 2103 |
}; |
| 2104 |
} |
| 2105 |
|
| 2106 |
check_row!(try_new_tiny); |
| 2107 |
check_row!(try_new_light); |
| 2108 |
check_row!(try_new_short_light); |
| 2109 |
check_row!(try_new_long_light); |
| 2110 |
check_row!(try_new_menu_light); |
| 2111 |
check_row!(try_new_short_menu_light); |
| 2112 |
check_row!(try_new_heavy); |
| 2113 |
check_row!(try_new_short_heavy); |
| 2114 |
check_row!(try_new_long_heavy); |
| 2115 |
check_row!(try_new_menu_heavy); |
| 2116 |
check_row!(try_new_short_menu_heavy); |
| 2117 |
} |
| 2118 |
} |