use Fluent's number formatting; don't show trailing zeros

We can add NUMBER() in the future for more control, but this will
do for the time being.
This commit is contained in:
Damien Elmes 2020-02-25 13:11:47 +10:00
parent b412747a16
commit c58b4158a7
2 changed files with 15 additions and 19 deletions

View File

@ -323,7 +323,11 @@ fn set_bundle_formatter_for_langs<T>(bundle: &mut FluentBundle<T>, langs: &[Lang
let num_formatter = NumberFormatter::new(langs); let num_formatter = NumberFormatter::new(langs);
let formatter = move |val: &FluentValue, _intls: &Mutex<IntlLangMemoizer>| -> Option<String> { let formatter = move |val: &FluentValue, _intls: &Mutex<IntlLangMemoizer>| -> Option<String> {
match val { match val {
FluentValue::Number(n) => Some(num_formatter.format(n.value)), FluentValue::Number(n) => {
let mut num = n.clone();
num.options.maximum_fraction_digits = Some(2);
Some(num_formatter.format(num.as_string().to_string()))
}
_ => None, _ => None,
} }
}; };
@ -371,17 +375,12 @@ impl NumberFormatter {
} }
} }
fn format(&self, num: f64) -> String { /// Given a pre-formatted number, change the decimal separator as appropriate.
// is it an integer? fn format(&self, num: String) -> String {
if (num - num.round()).abs() < std::f64::EPSILON { if self.decimal_separator != "." {
num.to_string() num.replace(".", self.decimal_separator)
} else { } else {
// currently we hard-code to 2 decimal places num
let mut formatted = format!("{:.2}", num);
if self.decimal_separator != "." {
formatted = formatted.replace(".", self.decimal_separator)
}
formatted
} }
} }
} }
@ -395,11 +394,8 @@ mod test {
#[test] #[test]
fn numbers() { fn numbers() {
let fmter = NumberFormatter::new(&[]);
assert_eq!(&fmter.format(1.0), "1");
assert_eq!(&fmter.format(1.007), "1.01");
let fmter = NumberFormatter::new(&[langid!("pl-PL")]); let fmter = NumberFormatter::new(&[langid!("pl-PL")]);
assert_eq!(&fmter.format(1.007), "1,01"); assert_eq!(&fmter.format("1.007".to_string()), "1,007");
} }
#[test] #[test]
@ -414,7 +410,7 @@ mod test {
assert_eq!( assert_eq!(
i18n.tr_("two-args-key", Some(tr_args!["one"=>1.1, "two"=>"2"])), i18n.tr_("two-args-key", Some(tr_args!["one"=>1.1, "two"=>"2"])),
"two args: 1.10 and 2" "two args: 1.1 and 2"
); );
assert_eq!( assert_eq!(
@ -423,7 +419,7 @@ mod test {
); );
assert_eq!( assert_eq!(
i18n.tr_("plural", Some(tr_args!["hats"=>1.1])), i18n.tr_("plural", Some(tr_args!["hats"=>1.1])),
"You have 1.10 hats." "You have 1.1 hats."
); );
assert_eq!( assert_eq!(
i18n.tr_("plural", Some(tr_args!["hats"=>3])), i18n.tr_("plural", Some(tr_args!["hats"=>3])),

View File

@ -173,7 +173,7 @@ mod test {
let i18n = I18n::new(&["zz"], ""); let i18n = I18n::new(&["zz"], "");
assert_eq!(answer_button_time(30.0, &i18n), "30s"); assert_eq!(answer_button_time(30.0, &i18n), "30s");
assert_eq!(answer_button_time(70.0, &i18n), "1m"); assert_eq!(answer_button_time(70.0, &i18n), "1m");
assert_eq!(answer_button_time(1.1 * MONTH, &i18n), "1.10mo"); assert_eq!(answer_button_time(1.1 * MONTH, &i18n), "1.1mo");
} }
#[test] #[test]
@ -181,7 +181,7 @@ mod test {
let i18n = I18n::new(&["zz"], ""); let i18n = I18n::new(&["zz"], "");
assert_eq!(time_span(1.0, &i18n), "1 second"); assert_eq!(time_span(1.0, &i18n), "1 second");
assert_eq!(time_span(30.0, &i18n), "30 seconds"); assert_eq!(time_span(30.0, &i18n), "30 seconds");
assert_eq!(time_span(90.0, &i18n), "1.50 minutes"); assert_eq!(time_span(90.0, &i18n), "1.5 minutes");
} }
#[test] #[test]