From c58b4158a7d35d50b9a5a4d034827d9c5ef7139d Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 25 Feb 2020 13:11:47 +1000 Subject: [PATCH] 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. --- rslib/src/i18n/mod.rs | 30 +++++++++++++----------------- rslib/src/sched/timespan.rs | 4 ++-- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/rslib/src/i18n/mod.rs b/rslib/src/i18n/mod.rs index b0f650447..4f6c4626c 100644 --- a/rslib/src/i18n/mod.rs +++ b/rslib/src/i18n/mod.rs @@ -323,7 +323,11 @@ fn set_bundle_formatter_for_langs(bundle: &mut FluentBundle, langs: &[Lang let num_formatter = NumberFormatter::new(langs); let formatter = move |val: &FluentValue, _intls: &Mutex| -> Option { 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, } }; @@ -371,17 +375,12 @@ impl NumberFormatter { } } - fn format(&self, num: f64) -> String { - // is it an integer? - if (num - num.round()).abs() < std::f64::EPSILON { - num.to_string() + /// Given a pre-formatted number, change the decimal separator as appropriate. + fn format(&self, num: String) -> String { + if self.decimal_separator != "." { + num.replace(".", self.decimal_separator) } else { - // currently we hard-code to 2 decimal places - let mut formatted = format!("{:.2}", num); - if self.decimal_separator != "." { - formatted = formatted.replace(".", self.decimal_separator) - } - formatted + num } } } @@ -395,11 +394,8 @@ mod test { #[test] 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")]); - assert_eq!(&fmter.format(1.007), "1,01"); + assert_eq!(&fmter.format("1.007".to_string()), "1,007"); } #[test] @@ -414,7 +410,7 @@ mod test { assert_eq!( 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!( @@ -423,7 +419,7 @@ mod test { ); assert_eq!( i18n.tr_("plural", Some(tr_args!["hats"=>1.1])), - "You have 1.10 hats." + "You have 1.1 hats." ); assert_eq!( i18n.tr_("plural", Some(tr_args!["hats"=>3])), diff --git a/rslib/src/sched/timespan.rs b/rslib/src/sched/timespan.rs index 9d426ee8a..b5725a7b1 100644 --- a/rslib/src/sched/timespan.rs +++ b/rslib/src/sched/timespan.rs @@ -173,7 +173,7 @@ mod test { let i18n = I18n::new(&["zz"], ""); assert_eq!(answer_button_time(30.0, &i18n), "30s"); 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] @@ -181,7 +181,7 @@ mod test { let i18n = I18n::new(&["zz"], ""); assert_eq!(time_span(1.0, &i18n), "1 second"); 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]