Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/experimental/src/displaynames/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ pub use displaynames::DisplayNamesPreferences;
pub use options::DisplayNamesOptions;
pub use options::Fallback;
pub use options::LanguageDisplay;
pub use options::LanguageIdentifierDisplayNameOptions;
pub use options::Style;
8 changes: 8 additions & 0 deletions components/experimental/src/displaynames/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ pub struct DisplayNamesOptions {
pub language_display: LanguageDisplay,
}

/// A bag of options defining how a language identifier display name will be formatted.
#[derive(Copy, Debug, Eq, PartialEq, Clone, Default)]
#[non_exhaustive]
pub struct LanguageIdentifierDisplayNameOptions {
/// The language display kind, defaults to "dialect".
pub language_display: Option<LanguageDisplay>,
}

/// An enum for formatting style.
#[allow(missing_docs)] // The variants are self explanatory.
#[non_exhaustive]
Expand Down
7 changes: 5 additions & 2 deletions components/experimental/src/displaynames/single/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions components/experimental/src/displaynames/single/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::displaynames::provider::*;
use crate::displaynames::single::{
RegionDisplayNameOwned, ScriptDisplayNameOwned, VariantDisplayNameOwned,
};
use crate::displaynames::{DisplayNamesOptions, DisplayNamesPreferences, LanguageDisplay};
use crate::displaynames::{
DisplayNamesPreferences, LanguageDisplay, LanguageIdentifierDisplayNameOptions,
};
use alloc::vec::Vec;
use icu_pattern::DoublePlaceholderPattern;
use icu_provider::DataPayloadOr;
Expand All @@ -19,13 +21,13 @@ use tinystr::TinyAsciiStr;
///
/// ```
/// use icu::experimental::displaynames::{
/// DisplayNamesPreferences, DisplayNamesOptions, single::LanguageIdentifierDisplayNameOwned,
/// DisplayNamesPreferences, LanguageIdentifierDisplayNameOptions, single::LanguageIdentifierDisplayNameOwned,
/// };
/// use icu::locale::{locale, langid};
/// use writeable::assert_writeable_eq;
///
/// let prefs = DisplayNamesPreferences::from(locale!("en"));
/// let options = DisplayNamesOptions::default();
/// let options = LanguageIdentifierDisplayNameOptions::default();
/// let display_name = LanguageIdentifierDisplayNameOwned::try_new(
/// prefs,
/// langid!("fr-CA"),
Expand All @@ -39,7 +41,7 @@ use tinystr::TinyAsciiStr;
#[derive(Debug)]
pub struct LanguageIdentifierDisplayNameOwned {
formatting_locale: DataLocale,
options: DisplayNamesOptions,
options: LanguageIdentifierDisplayNameOptions,
language_payload: DataPayload<LocaleNamesLanguageMediumV1>,
script_payload: DataPayloadOr<LocaleNamesScriptMediumV1, ()>,
region_payload: DataPayloadOr<LocaleNamesRegionMediumV1, ()>,
Expand All @@ -50,7 +52,7 @@ pub struct LanguageIdentifierDisplayNameOwned {

impl LanguageIdentifierDisplayNameOwned {
icu_provider::gen_buffer_data_constructors!(
(prefs: DisplayNamesPreferences, subject: icu_locale::LanguageIdentifier, options: DisplayNamesOptions) -> result: Result<Self, DataError>,
(prefs: DisplayNamesPreferences, subject: icu_locale::LanguageIdentifier, options: LanguageIdentifierDisplayNameOptions) -> result: Result<Self, DataError>,
/// Loads the language display name for a given language identifier and locale using compiled data.
functions: [
try_new,
Expand All @@ -65,7 +67,7 @@ impl LanguageIdentifierDisplayNameOwned {
provider: &D,
prefs: DisplayNamesPreferences,
mut subject: icu_locale::LanguageIdentifier,
options: DisplayNamesOptions,
options: LanguageIdentifierDisplayNameOptions,
) -> Result<Self, DataError>
where
D: DataProvider<LocaleNamesLanguageMediumV1>
Expand All @@ -91,7 +93,7 @@ impl LanguageIdentifierDisplayNameOwned {
let mut language_payload = None;

// Only try dialect if requested (which is the default)
if options.language_display == LanguageDisplay::Dialect {
if options.language_display.unwrap_or_default() == LanguageDisplay::Dialect {
for (language, script, region) in [
(subject.language, Some(subject.script), Some(subject.region)),
(subject.language, Some(subject.script), None),
Expand Down
12 changes: 9 additions & 3 deletions components/experimental/tests/displaynames/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use icu_experimental::displaynames::{DisplayNamesOptions, multi::LocaleDisplayNamesFormatter};
use icu_experimental::displaynames::{
DisplayNamesOptions, LanguageIdentifierDisplayNameOptions, multi::LocaleDisplayNamesFormatter,
};
use icu_locale_core::Locale;
use icu_locale_core::locale;
use std::borrow::Cow;
Expand Down Expand Up @@ -174,8 +176,12 @@ fn test_concatenate() {
// Test the newer LanguageIdentifierDisplayName
use icu_experimental::displaynames::single::LanguageIdentifierDisplayNameOwned;
let lang_id = cas.input_1.id.clone();
let result =
LanguageIdentifierDisplayNameOwned::try_new(locale.clone().into(), lang_id, options);
let single_options = LanguageIdentifierDisplayNameOptions::default();
let result = LanguageIdentifierDisplayNameOwned::try_new(
locale.clone().into(),
lang_id,
single_options,
);
match result {
Ok(single_display_name) => {
assert_eq!(cas.single_should_err, false, "{cas:?}");
Expand Down
Loading