Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert preferences to DataLocale #5779

Merged
merged 1 commit into from
Nov 7, 2024
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
16 changes: 3 additions & 13 deletions components/list/src/list_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ macro_rules! constructor {
ListLength::Short => ListFormatterPatternsV2::SHORT,
ListLength::Wide => ListFormatterPatternsV2::WIDE,
};
let locale = get_data_locale_from_prefs(prefs);
let locale = DataLocale::from_preferences_locale::<$marker>(prefs.locale_prefs);
let data = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_marker_attributes_and_locale(
length,
&locale),
&locale
),
..Default::default()
})?
.payload
Expand All @@ -73,17 +74,6 @@ macro_rules! constructor {
};
}

fn get_data_locale_from_prefs(prefs: ListFormatterPreferences) -> DataLocale {
// TODO(#5764): This should utilize region source priority.
DataLocale::from_subtags(
prefs.locale_prefs.language,
prefs.locale_prefs.script,
prefs.locale_prefs.region,
prefs.locale_prefs.variant,
prefs.locale_prefs.subdivision,
)
}

impl ListFormatter {
constructor!(
try_new_and,
Expand Down
11 changes: 6 additions & 5 deletions components/locale_core/src/preferences/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,12 @@ macro_rules! __define_preferences {
}
}

impl $name {
/// Constructs a `Locale` corresponding to these preferences.
pub fn into_locale(self) -> $crate::Locale {
impl From<$name> for $crate::Locale {
fn from(other: $name) -> Self {
use $crate::preferences::PreferenceKey;
let mut result = $crate::Locale::from(self.locale_prefs);
let mut result = Self::from(other.locale_prefs);
$(
if let Some(value) = &self.$key {
if let Some(value) = other.$key {
if let Some(ue) = <$pref>::unicode_extension_key() {
let val = value.unicode_extension_value().unwrap();
result.extensions.unicode.keywords.set(ue, val);
Expand All @@ -544,7 +543,9 @@ macro_rules! __define_preferences {
)*
result
}
}

impl $name {
/// Extends the preferences with the values from another set of preferences.
pub fn extend(&mut self, other: $name) {
self.locale_prefs.extend(other.locale_prefs);
Expand Down
1 change: 0 additions & 1 deletion ffi/capi/tests/missing_apis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ icu::list::ListFormatterOptions::default#FnInStruct
icu::list::ListFormatterOptions::with_length#FnInStruct
icu::list::ListFormatterPreferences#Struct
icu::list::ListFormatterPreferences::extend#FnInStruct
icu::list::ListFormatterPreferences::into_locale#FnInStruct
icu::locale::preferences::LocalePreferences#Struct
icu::locale::preferences::LocalePreferences::default#FnInStruct
icu::locale::preferences::LocalePreferences::extend#FnInStruct
Expand Down
33 changes: 21 additions & 12 deletions provider/core/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use icu_locale_core::subtags::{Language, Region, Script, Subtag, Variant};
use icu_locale_core::{LanguageIdentifier, Locale, ParseError};
use zerovec::ule::VarULE;

use crate::fallback::LocaleFallbackPriority;
use crate::DataMarker;

/// The request type passed into all data provider implementations.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[allow(clippy::exhaustive_structs)] // this type is stable
Expand Down Expand Up @@ -277,20 +280,26 @@ impl DataLocale {
)
}

/// Creates a [`DataLocale`] from a list of subtags.
pub const fn from_subtags(
language: Language,
script: Option<Script>,
region: Option<Region>,
variant: Option<Variant>,
subdivision: Option<Subtag>,
/// Returns a [`DataLocale`] usable for the marker `M`.
pub const fn from_preferences_locale<M: DataMarker>(
locale: icu_locale_core::preferences::LocalePreferences,
) -> Self {
Self {
language,
script,
region,
variant,
subdivision,
language: locale.language,
script: locale.script,
region: match (locale.region, locale.ue_region) {
(Some(_), Some(r))
if matches!(
M::INFO.fallback_config.priority,
LocaleFallbackPriority::Region
) =>
{
Some(r)
}
(r, _) => r,
},
variant: locale.variant,
subdivision: locale.subdivision,

keywords: unicode_ext::Keywords::new(),
}
Expand Down