-
Notifications
You must be signed in to change notification settings - Fork 282
feat(currency): Resolve and apply locale-specific currency precision #8169
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
base: main
Are you sure you want to change the base?
Changes from all commits
11c54c9
9272a2b
7650c4a
cf1194a
671a6c6
66b752d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ | |
| use core::fmt::Display; | ||
|
|
||
| use crate::dimension::provider::{ | ||
| currency::compact::ShortCurrencyCompactV1, currency::essentials::CurrencyEssentialsV1, | ||
| currency::compact::ShortCurrencyCompactV1, | ||
| currency::essentials::{CurrencyEssentials, CurrencyEssentialsV1, PatternSelection}, | ||
| currency::fractions::{CurrencyFractionsV1, FractionInfo, Rounding}, | ||
| }; | ||
| use fixed_decimal::Decimal; | ||
| use icu_decimal::preferences::CompactDecimalFormatterPreferences; | ||
|
|
@@ -15,6 +17,7 @@ use icu_plurals::{PluralRules, PluralRulesPreferences}; | |
| use icu_provider::prelude::*; | ||
| use writeable::Writeable; | ||
|
|
||
| use super::formatter::apply_precision; | ||
| use super::{CurrencyCode, options::CurrencyFormatterOptions}; | ||
| use icu_pattern::DoublePlaceholderPattern; | ||
|
|
||
|
|
@@ -64,6 +67,8 @@ pub struct CompactCurrencyFormatter { | |
|
|
||
| plural_rules: PluralRules, | ||
|
|
||
| fractions: DataPayload<CurrencyFractionsV1>, | ||
|
|
||
| /// Options bag for the compact currency formatter to determine the behavior of the formatter. | ||
| /// for example: width. | ||
| options: CurrencyFormatterOptions, | ||
|
|
@@ -130,12 +135,15 @@ impl CompactCurrencyFormatter { | |
|
|
||
| let plural_rules = PluralRules::try_new_cardinal((&prefs).into())?; | ||
|
|
||
| let fractions = crate::provider::Baked.load(Default::default())?.payload; | ||
|
|
||
| Ok(Self { | ||
| _short_currency_compact: short_currency_compact, | ||
| essential, | ||
| decimal_formatter, | ||
| compact_data, | ||
| plural_rules, | ||
| fractions, | ||
| options, | ||
| }) | ||
| } | ||
|
|
@@ -150,6 +158,7 @@ impl CompactCurrencyFormatter { | |
| D: ?Sized | ||
| + DataProvider<CurrencyEssentialsV1> | ||
| + DataProvider<ShortCurrencyCompactV1> | ||
| + DataProvider<CurrencyFractionsV1> | ||
| + DataProvider<icu_decimal::provider::DecimalCompactShortV1> | ||
| + DataProvider<icu_decimal::provider::DecimalSymbolsV1> | ||
| + DataProvider<icu_decimal::provider::DecimalDigitsV1> | ||
|
|
@@ -194,12 +203,15 @@ impl CompactCurrencyFormatter { | |
| return Err(DataError::custom("missing standard pattern")); | ||
| } | ||
|
|
||
| let fractions = provider.load(Default::default())?.payload; | ||
|
|
||
| Ok(Self { | ||
| _short_currency_compact: short_currency_compact, | ||
| essential, | ||
| decimal_formatter, | ||
| compact_data, | ||
| plural_rules, | ||
| fractions, | ||
| options, | ||
| }) | ||
| } | ||
|
|
@@ -225,13 +237,21 @@ impl CompactCurrencyFormatter { | |
| value: &'l Decimal, | ||
| currency_code: &'l CurrencyCode, | ||
| ) -> impl Writeable + Display + 'l { | ||
| let (currency_placeholder, pattern, _pattern_selection) = self | ||
| let (currency_placeholder, pattern, pattern_selection) = self | ||
| .essential | ||
| .get() | ||
| .name_and_pattern(self.options.width, currency_code); | ||
|
|
||
| let pattern = pattern.unwrap_or_else(|| <&DoublePlaceholderPattern>::default()); | ||
|
|
||
| let fraction_info = self.resolve_fraction_info( | ||
| Some(self.essential.get()), | ||
| Some(pattern_selection), | ||
| *currency_code, | ||
| ); | ||
|
|
||
| let value = apply_precision(value.clone(), fraction_info); | ||
|
|
||
| // TODO: The current behavior is the behavior when there is no compact currency pattern found. | ||
| // Therefore, in the next PR, we will add the code to handle using the compact currency patterns. | ||
|
|
||
|
|
@@ -252,4 +272,48 @@ impl CompactCurrencyFormatter { | |
| )), | ||
| ) | ||
| } | ||
|
|
||
| /// Resolves the fraction/precision information for a given currency. | ||
| /// | ||
| /// The resolution follows a 3-step fallback: | ||
| /// 1. Try currency-specific override in the global map (e.g., JPY = 0 decimals). | ||
| /// 2. Try locale-specific pattern precision from `CurrencyEssentials` (if available). | ||
| /// 3. Fallback to the global default. | ||
| fn resolve_fraction_info( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: move this method to be on |
||
| &self, | ||
| essentials: Option<&CurrencyEssentials>, | ||
| pattern_selection: Option<PatternSelection>, | ||
| currency_code: CurrencyCode, | ||
| ) -> FractionInfo { | ||
| let iso_code = currency_code.0.to_unvalidated(); | ||
|
|
||
| // 1. Try currency-specific override in global map | ||
| if let Some(ule) = self.fractions.get().fractions.get(&iso_code) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
| return zerovec::ule::AsULE::from_unaligned(*ule); | ||
| } | ||
|
|
||
| // 2. Try locale-specific pattern precision | ||
| if let Some(essentials) = essentials { | ||
| let selection = pattern_selection.unwrap_or(PatternSelection::Standard); | ||
| let index = match selection { | ||
| PatternSelection::Standard => essentials.standard_fractions_index, | ||
| PatternSelection::StandardAlphaNextToNumber => { | ||
| essentials.standard_next_to_alpha_fractions_index | ||
| } | ||
| }; | ||
| if index != u8::MAX | ||
| && let Some(digits) = essentials.fractions_vec.get(index as usize) | ||
| { | ||
| return FractionInfo { | ||
| digits, | ||
| rounding: Rounding::R1, | ||
| cash_digits: None, | ||
| cash_rounding: None, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| // 3. Fallback to global default | ||
| self.fractions.get().default | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a comment to this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Added doc comments to both `compact_formatter.rs` and `formatter.rs`.