Skip to content

Commit

Permalink
Move PluralRules to use Preferences (#5798)
Browse files Browse the repository at this point in the history
Migrate PluralRules to Preferences
  • Loading branch information
zbraniecki authored Nov 7, 2024
1 parent 6ad31fb commit 9c32b51
Show file tree
Hide file tree
Showing 25 changed files with 112 additions and 103 deletions.
12 changes: 8 additions & 4 deletions components/experimental/src/compactdecimal/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ impl CompactDecimalFormatter {
locale: &DataLocale,
options: CompactDecimalFormatterOptions,
) -> Result<Self, DataError> {
let temp_loc = locale.clone().into_locale();
Ok(Self {
fixed_decimal_formatter: FixedDecimalFormatter::try_new(
locale,
options.fixed_decimal_formatter_options,
)?,
plural_rules: PluralRules::try_new_cardinal(locale)?,
plural_rules: PluralRules::try_new_cardinal(temp_loc.into())?,
compact_data: DataProvider::<ShortCompactDecimalFormatDataV1Marker>::load(
&crate::provider::Baked,
DataRequest {
Expand Down Expand Up @@ -128,13 +129,14 @@ impl CompactDecimalFormatter {
+ DataProvider<icu_plurals::provider::CardinalV1Marker>
+ ?Sized,
{
let temp_loc = locale.clone().into_locale();
Ok(Self {
fixed_decimal_formatter: FixedDecimalFormatter::try_new_unstable(
provider,
locale,
options.fixed_decimal_formatter_options,
)?,
plural_rules: PluralRules::try_new_cardinal_unstable(provider, locale)?,
plural_rules: PluralRules::try_new_cardinal_unstable(provider, temp_loc.into())?,
compact_data: DataProvider::<ShortCompactDecimalFormatDataV1Marker>::load(
provider,
DataRequest {
Expand Down Expand Up @@ -171,12 +173,13 @@ impl CompactDecimalFormatter {
locale: &DataLocale,
options: CompactDecimalFormatterOptions,
) -> Result<Self, DataError> {
let temp_loc = locale.clone().into_locale();
Ok(Self {
fixed_decimal_formatter: FixedDecimalFormatter::try_new(
locale,
options.fixed_decimal_formatter_options,
)?,
plural_rules: PluralRules::try_new_cardinal(locale)?,
plural_rules: PluralRules::try_new_cardinal(temp_loc.into())?,
compact_data: DataProvider::<LongCompactDecimalFormatDataV1Marker>::load(
&crate::provider::Baked,
DataRequest {
Expand Down Expand Up @@ -212,13 +215,14 @@ impl CompactDecimalFormatter {
+ DataProvider<icu_plurals::provider::CardinalV1Marker>
+ ?Sized,
{
let temp_loc = locale.clone().into_locale();
Ok(Self {
fixed_decimal_formatter: FixedDecimalFormatter::try_new_unstable(
provider,
locale,
options.fixed_decimal_formatter_options,
)?,
plural_rules: PluralRules::try_new_cardinal_unstable(provider, locale)?,
plural_rules: PluralRules::try_new_cardinal_unstable(provider, temp_loc.into())?,
compact_data: DataProvider::<LongCompactDecimalFormatDataV1Marker>::load(
provider,
DataRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ impl LongCurrencyFormatter {

let patterns = crate::provider::Baked.load(Default::default())?.payload;

let plural_rules = PluralRules::try_new_cardinal(locale)?;
let temp_loc = locale.clone().into_locale();
let plural_rules = PluralRules::try_new_cardinal(temp_loc.into())?;

Ok(Self {
extended,
Expand Down Expand Up @@ -127,7 +128,8 @@ impl LongCurrencyFormatter {

let patterns = provider.load(Default::default())?.payload;

let plural_rules = PluralRules::try_new_cardinal_unstable(provider, locale)?;
let temp_loc = locale.clone().into_locale();
let plural_rules = PluralRules::try_new_cardinal_unstable(provider, temp_loc.into())?;

Ok(Self {
extended,
Expand Down
6 changes: 4 additions & 2 deletions components/experimental/src/dimension/units/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ impl UnitsFormatter {
let fixed_decimal_formatter =
FixedDecimalFormatter::try_new(locale, FixedDecimalFormatterOptions::default())?;

let plural_rules = PluralRules::try_new_cardinal(locale)?;
let temp_loc = locale.clone().into_locale();
let plural_rules = PluralRules::try_new_cardinal(temp_loc.into())?;

// TODO: Remove this allocation once we have separate markers for different widths.
let attribute = Self::attribute(options.width, unit);
Expand Down Expand Up @@ -126,7 +127,8 @@ impl UnitsFormatter {
FixedDecimalFormatterOptions::default(),
)?;

let plural_rules = PluralRules::try_new_cardinal_unstable(provider, locale)?;
let temp_loc = locale.clone().into_locale();
let plural_rules = PluralRules::try_new_cardinal_unstable(provider, temp_loc.into())?;

// TODO: Remove this allocation once we have separate markers for different widths.
let attribute = Self::attribute(options.width, unit);
Expand Down
6 changes: 4 additions & 2 deletions components/experimental/src/relativetime/relativetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ macro_rules! constructor {
locale: &DataLocale,
options: RelativeTimeFormatterOptions,
) -> Result<Self, DataError> {
let plural_rules = PluralRules::try_new_cardinal(locale)?;
let temp_loc = locale.clone().into_locale();
let plural_rules = PluralRules::try_new_cardinal(temp_loc.into())?;
// Initialize FixedDecimalFormatter with default options
let fixed_decimal_format = FixedDecimalFormatter::try_new(
locale,
Expand Down Expand Up @@ -167,7 +168,8 @@ macro_rules! constructor {
+ DataProvider<DecimalSymbolsV1Marker>
+ ?Sized,
{
let plural_rules = PluralRules::try_new_cardinal_unstable(provider, locale)?;
let temp_loc = locale.clone().into_locale();
let plural_rules = PluralRules::try_new_cardinal_unstable(provider, temp_loc.into())?;
// Initialize FixedDecimalFormatter with default options
let fixed_decimal_format = FixedDecimalFormatter::try_new_unstable(
provider,
Expand Down
2 changes: 1 addition & 1 deletion components/icu/examples/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn main() {
}

{
let pr = PluralRules::try_new_cardinal(&locale!("en").into())
let pr = PluralRules::try_new_cardinal(locale!("en").into())
.expect("Failed to create PluralRules.");

match pr.category_for(email_count) {
Expand Down
2 changes: 1 addition & 1 deletion components/plurals/README.md

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

8 changes: 4 additions & 4 deletions components/plurals/benches/pluralrules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn pluralrules(c: &mut Criterion) {
c.bench_function("plurals/pluralrules/overview", |b| {
b.iter(|| {
for lang in &plurals_data.langs {
let pr = PluralRules::try_new(&lang.into(), PluralRuleType::Cardinal).unwrap();
let pr = PluralRules::try_new(lang.into(), PluralRuleType::Cardinal).unwrap();
for s in &numbers_data.usize {
let _ = pr.category_for(*s);
}
Expand All @@ -35,13 +35,13 @@ fn pluralrules(c: &mut Criterion) {
c.bench_function("plurals/pluralrules/construct/fs", |b| {
b.iter(|| {
for lang in &plurals_data.langs {
PluralRules::try_new(&lang.into(), PluralRuleType::Ordinal).unwrap();
PluralRules::try_new(&lang.into(), PluralRuleType::Cardinal).unwrap();
PluralRules::try_new(lang.into(), PluralRuleType::Ordinal).unwrap();
PluralRules::try_new(lang.into(), PluralRuleType::Cardinal).unwrap();
}
});
});

let pr = PluralRules::try_new(&locale!("ru").into(), PluralRuleType::Cardinal).unwrap();
let pr = PluralRules::try_new(locale!("ru").into(), PluralRuleType::Cardinal).unwrap();
c.bench_function("plurals/pluralrules/select/fs", |b| {
b.iter(|| {
for s in &numbers_data.usize {
Expand Down
2 changes: 1 addition & 1 deletion components/plurals/examples/elevator_floors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const VALUES: &[usize] = &[0, 2, 25, 1, 3, 2, 4, 10, 7, 0];

fn main() {
println!("\n====== Elevator Floor (en) example ============");
let pr = PluralRules::try_new_ordinal(&locale!("en").into())
let pr = PluralRules::try_new_ordinal(locale!("en").into())
.expect("Failed to create a PluralRules instance.");

for value in VALUES {
Expand Down
2 changes: 1 addition & 1 deletion components/plurals/examples/unread_emails.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const VALUES: &[usize] = &[0, 2, 25, 1, 3, 2, 4, 10, 7, 0];

fn main() {
println!("\n====== Unread Emails (en) example ============");
let pr = PluralRules::try_new_cardinal(&locale!("en").into())
let pr = PluralRules::try_new_cardinal(locale!("en").into())
.expect("Failed to create a PluralRules instance.");

for value in VALUES {
Expand Down
Loading

0 comments on commit 9c32b51

Please sign in to comment.