Skip to content

misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux

Numbers are grouped and pointed the interface language's way (#128) (#133)

Ahmed Nefzaoui99037e044014
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 94cb7104..3595834a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -300,6 +300,11 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see
### Fixed
+- **Numbers are written the interface language's way** ([#128](https://github.com/anefzaoui/misterdownloadmanager/issues/128)):
+ counts, sizes, speeds, percentages and the list's columns group and point their digits as the
+ chosen language does (German `10.200` and `1,50 MB`, French `10 200`, Hindi `1,00,000`),
+ whatever the Windows region; a region in the same language keeps its own way (Swiss German
+ `10’200`). English is unchanged.
- **A finished torrent's progress window lost its tabs while it was still shared**: Files, Peers,
Trackers, Pieces and General (with the share ratio) said "Shown while the torrent is
downloading." They now stay for as long as the torrent is shared.
diff --git a/ROADMAP.md b/ROADMAP.md
index 2b020b73..cdbee818 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -13,6 +13,8 @@ Everything below ships together, in one release.
### Done
+- ✅ Numbers are grouped and pointed the interface language's way (German `10.200`, `1,50 MB`)
+ ([#128](https://github.com/anefzaoui/misterdownloadmanager/issues/128))
- ✅ The extension's button lists the media the tab has loaded (HLS and DASH streams, video and
sound files), to download a stream the page won't give up; copy-protected ones are shown as
protected, never downloaded ([#38](https://github.com/anefzaoui/misterdownloadmanager/issues/38))
diff --git a/crates/app/src/app.rs b/crates/app/src/app.rs
index 8baaff7a..8df77ee4 100644
--- a/crates/app/src/app.rs
+++ b/crates/app/src/app.rs
@@ -7065,7 +7065,7 @@ fn sharing_stopped_note(why: crate::torrents::Reached) -> String {
crate::torrents::Reached::Ratio(r) => crate::i18n::fmt(
// i18n: event log entry, past tense: a finished torrent stopped being shared to others because its share ratio (sent ÷ received) reached the limit set in Torrent settings; {ratio} = e.g. 2.00
crate::i18n::t("Sharing stopped: the share ratio reached {ratio}"),
- &[("ratio", &format!("{r:.2}"))],
+ &[("ratio", &crate::i18n::decimal(r, 2))],
),
crate::torrents::Reached::Time(s) => crate::i18n::fmt(
// i18n: event log entry, past tense: a finished torrent stopped being shared to others after the time set in Torrent settings; {time} = e.g. 1 hour(s) 15 min
diff --git a/crates/app/src/crashui.rs b/crates/app/src/crashui.rs
index 0a0874fe..b8870bc6 100644
--- a/crates/app/src/crashui.rs
+++ b/crates/app/src/crashui.rs
@@ -424,7 +424,7 @@ impl ReportWindow {
// What is happening behind it.
let status: Option<String> = if let Some((done, total)) = fetching {
- let mb = |b: u64| format!("{:.1}", b as f64 / 1_048_576.0);
+ let mb = |b: u64| crate::i18n::decimal(b as f64 / 1_048_576.0, 1);
Some(crate::i18n::fmt(
// i18n: crash report window: downloading the symbols that turn addresses into function names; {done} and {total} are megabytes
crate::i18n::t(
diff --git a/crates/app/src/extractq.rs b/crates/app/src/extractq.rs
index 88060789..93cf3aba 100644
--- a/crates/app/src/extractq.rs
+++ b/crates/app/src/extractq.rs
@@ -360,7 +360,7 @@ fn failed(name: &str, e: &Error) -> String {
Error::MissingVolume(n) => crate::i18n::fmt(
// i18n: why an archive wasn't extracted (after "Couldn't extract {name}: "); {n} is the volume's number
crate::i18n::t("volume {n} of it is missing"),
- &[("n", &n.to_string())],
+ &[("n", &crate::i18n::integer(u64::from(*n)))],
),
Error::Damaged(why) => {
tracing::info!("extract: {name}: {why}");
diff --git a/crates/app/src/help.rs b/crates/app/src/help.rs
index 892c3315..65351d38 100644
--- a/crates/app/src/help.rs
+++ b/crates/app/src/help.rs
@@ -370,7 +370,8 @@ impl Updates {
crate::i18n::t("Downloading the update... {percent}"),
&[(
"percent",
- &fraction.map_or(String::new(), |f| format!("{:.0}%", f * 100.0)),
+ &fraction
+ .map_or(String::new(), |f| crate::i18n::percent(f64::from(f), 0)),
)],
);
winui::text(&p, status_rc.left(), status_rc.top() + 9.0, &line, ink);
diff --git a/crates/app/src/helpers.rs b/crates/app/src/helpers.rs
index c6619436..72cc5c43 100644
--- a/crates/app/src/helpers.rs
+++ b/crates/app/src/helpers.rs
@@ -297,8 +297,8 @@ impl Helpers {
crate::i18n::t("Checking {name} ({n} of {total})..."),
&[
("name", name),
- ("n", &k.to_string()),
- ("total", &total.to_string()),
+ ("n", &crate::i18n::integer(k as u64)),
+ ("total", &crate::i18n::integer(total as u64)),
],
),
ink,
@@ -311,8 +311,8 @@ impl Helpers {
crate::i18n::t("Downloading {name} ({n} of {total})... {percent}"),
&[
("name", name),
- ("n", &k.to_string()),
- ("total", &total.to_string()),
+ ("n", &crate::i18n::integer(k as u64)),
+ ("total", &crate::i18n::integer(total as u64)),
("percent", &format!("{}%", (f * 100.0).floor())),
],
),
diff --git a/crates/app/src/i18n.rs b/crates/app/src/i18n.rs
index 0ebf7ecb..b4800ad0 100644
--- a/crates/app/src/i18n.rs
+++ b/crates/app/src/i18n.rs
@@ -1,7 +1,9 @@
//! The interface language (M16, D-019). craft-i18n reads MDM's catalogs (`i18n/<locale>.tsv`,
//! embedded at build time) and looks the English source text up: a missing translation is the
//! English itself. The language follows Windows' display language until the user picks one in
-//! View → Language; numbers follow the Windows region (craft-i18n's format).
+//! View → Language; numbers are grouped and pointed as the interface language writes them
+//! (CLDR, through craft-i18n's format), or as the system region does when it is in that same
+//! language (#128).
//!
//! `t("Options")` for a label, `tc("menu", "&Options")` where the same English needs different
//! translations, `tn(n, "{n} file", "{n} files")` for counts. Text that is data (file names,
@@ -11,7 +13,7 @@
use std::sync::{OnceLock, RwLock};
pub use craft_i18n::Lang;
-use craft_i18n::{Embedded, I18n};
+use craft_i18n::{Digits, Embedded, Format, I18n, Region};
/// Every catalog in one brotli stream, and each locale's code and range in the unpacked text
/// (build.rs).
@@ -65,7 +67,7 @@ pub fn lang() -> Lang {
return l;
}
let l = i18n().system_lang();
- *CURRENT.write().unwrap() = Some(l);
+ use_lang(l);
l
}
@@ -74,7 +76,33 @@ pub fn set_lang(code: Option<&str>) {
let l = code
.and_then(|c| i18n().from_code(c))
.unwrap_or_else(|| i18n().system_lang());
+ use_lang(l);
+}
+
+/// The interface language becomes `l`, and numbers (counts, sizes, speeds, percentages) are
+/// written its way from now on.
+fn use_lang(l: Lang) {
*CURRENT.write().unwrap() = Some(l);
+ i18n().set_format(number_format(i18n().code(l), &Region::system()));
+}
+
+/// How numbers are written in the interface language `code` (#128): as the system region
+/// writes them when it is in that language (Swiss German's `1’234.5`, a separator the user set in
+/// Windows), else as CLDR writes the language: German `1.234,5`, French `1 234,5` (narrow
+/// no-break space), Hindi `12,34,567`, Arabic `1,234.5` (CLDR writes Arabic without a country in
+/// Latin digits; an Egyptian region, `ar-EG`, gives `١٬٢٣٤٫٥`).
+pub fn number_format(code: &str, system: &Region) -> Format {
+ let language = |tag: &str| {
+ tag.split(['-', '_'])
+ .next()
+ .unwrap_or_default()
+ .to_ascii_lowercase()
+ };
+ if language(&system.numbers) == language(code) {
+ Format::new(system, Digits::Locale)
+ } else {
+ Format::from_tag(code)
+ }
}
/// The interface language's code (`en`, `ar`…).
@@ -128,7 +156,8 @@ pub fn isolate(value: &str) -> String {
craft_i18n::isolate(value)
}
-/// A whole number as the interface language writes it (English: plain digits, as IDM).
+/// A whole number as the interface language writes it (English: plain digits, as IDM):
+/// `10200`, `10.200`, `10 200`.
pub fn integer(n: u64) -> String {
if code() == "en" {
n.to_string()
@@ -137,6 +166,26 @@ pub fn integer(n: u64) -> String {
}
}
+/// `v` with `decimals` digits after the decimal mark, as the interface language writes it
+/// (English: `1234.50`, as IDM; German `1.234,50`).
+pub fn decimal(v: f64, decimals: usize) -> String {
+ if code() == "en" {
+ format!("{v:.decimals$}")
+ } else {
+ i18n().format().number(v, decimals)
+ }
+}
+
+/// `ratio` as a percentage with `decimals` digits, as the interface language writes it (English:
+/// `13.38%`, as IDM; German `13,38 %`, Turkish `%13,38`).
+pub fn percent(ratio: f64, decimals: usize) -> String {
+ if code() == "en" {
+ format!("{:.decimals$}%", 100.0 * ratio)
+ } else {
+ i18n().format().percent(ratio, decimals)
+ }
+}
+
/// Every language offered, English first, each by its own name, for View → Language.
pub fn languages() -> Vec<(Lang, &'static str, &'static str)> {
let i = i18n();
@@ -236,4 +285,78 @@ mod tests {
}
assert!(!i.rtl(Lang::EN));
}
+
+ const NNBSP: char = '\u{202f}';
+
+ /// #128: numbers are grouped and pointed as the interface language writes them, whatever the
+ /// system's region (here the United States'), and as the region does when it is in the same
+ /// language.
+ #[test]
+ fn numbers_follow_the_interface_language() {
+ let us = Region::from_tag("en-US");
+ for (code, n, count, size) in [
+ ("en", 10_200, "10,200".to_owned(), "1,234.50".to_owned()),
+ ("de", 10_200, "10.200".to_owned(), "1.234,50".to_owned()),
+ (
+ "fr",
+ 10_200,
+ format!("10{NNBSP}200"),
+ format!("1{NNBSP}234,50"),
+ ),
+ // CLDR writes Arabic without a country in Latin digits, grouped the English way.
+ ("ar", 10_200, "10,200".to_owned(), "1,234.50".to_owned()),
+ // Hindi groups by lakh and crore.
+ (
+ "hi",
+ 10_010_200,
+ "1,00,10,200".to_owned(),
+ "1,234.50".to_owned(),
+ ),
+ ] {
+ let f = number_format(code, &us);
+ assert_eq!(f.integer(n), count, "{code}");
+ assert_eq!(f.number(1234.5, 2), size, "{code}");
+ }
+ // A region in the interface's language keeps its own way, and a separator the user set.
+ let swiss = Region::from_tag("de-CH");
+ assert_eq!(
+ number_format("de", &swiss).integer(10_200),
+ Format::from_tag("de-CH").integer(10_200)
+ );
+ assert_ne!(number_format("de", &swiss).integer(10_200), "10.200");
+ let egypt = Region::from_tag("ar-EG");
+ assert_eq!(number_format("ar", &egypt).integer(10_200), "١٠٬٢٠٠");
+ assert_eq!(number_format("de", &egypt).integer(10_200), "10.200");
+ let mut own = Region::from_tag("de-DE");
+ own.grouping = Some(Some('\''));
+ assert_eq!(number_format("de", &own).integer(10_200), "10'200");
+ // Catalog codes with a country or a script are read too.
+ assert_eq!(number_format("pt-br", &us).integer(10_200), "10.200");
+ assert_eq!(number_format("zh-hans", &us).integer(10_200), "10,200");
+ }
+
+ /// Counts, sizes and speeds take their numbers from that format, beside the catalogs' units
+ /// (the way the list, the progress window and the playlist dialog write them).
+ #[test]
+ fn counts_sizes_and_speeds_in_the_interface_language() {
+ let us = Region::from_tag("en-US");
+ let codes: Vec<&str> = catalogs().iter().map(|c| c.0).collect();
+ let (i, _) = I18n::new(catalogs(), &codes);
+ let fr_count = format!("10{NNBSP}200");
+ for (code, count, size, speed) in [
+ ("de", "10.200", "1,50", "512,000"),
+ ("fr", fr_count.as_str(), "1,50", "512,000"),
+ ("ar", "10,200", "1.50", "512.000"),
+ ("hi", "10,200", "1.50", "512.000"),
+ ] {
+ let l = i.from_code(code).expect(code);
+ i.set_format(number_format(code, &us));
+ let videos = craft_i18n::strip_isolates(&i.trn(l, 10_200, "{n} video", "{n} videos"));
+ assert!(videos.contains(count), "{code}: {videos}");
+ let mb = craft_i18n::strip_isolates(&i.measure(l, 1.5, 2, "{n} MB"));
+ assert!(mb.contains(size), "{code}: {mb}");
+ let rate = craft_i18n::strip_isolates(&i.measure(l, 512.0, 3, "{n} KB/sec"));
+ assert!(rate.contains(speed), "{code}: {rate}");
+ }
+ }
}
diff --git a/crates/app/src/model.rs b/crates/app/src/model.rs
index 5700305e..5a7e22ce 100644
--- a/crates/app/src/model.rs
+++ b/crates/app/src/model.rs
@@ -876,18 +876,9 @@ fn english() -> bool {
crate::i18n::code() == "en"
}
-/// A number with `decimals` digits, as the interface language's region writes it.
-fn number(v: f64, decimals: usize) -> String {
- crate::i18n::i18n().format().number(v, decimals)
-}
-
-/// `13.38%` (IDM), or the region's percentage.
+/// `13.38%` (IDM), or the interface language's percentage (`13,38 %`).
pub fn percent(ratio: f64) -> String {
- if english() {
- format!("{:.2}%", 100.0 * ratio)
- } else {
- crate::i18n::i18n().format().percent(ratio, 2)
- }
+ crate::i18n::percent(ratio, 2)
}
/// `{n} MB`-style unit text in the interface language (catalog context `unit`).
@@ -973,7 +964,7 @@ pub fn size_list(b: u64) -> String {
pub fn time(secs: f64) -> String {
let s = secs.round() as u64;
if !english() {
- let n = |v: u64| number(v as f64, 0);
+ let n = crate::i18n::integer;
let (h, m, sec) = (n(s / 3600), n((s % 3600) / 60), n(s % 60));
let t = crate::i18n::t;
return match s {
diff --git a/crates/app/src/options.rs b/crates/app/src/options.rs
index b86226a9..8857602d 100644
--- a/crates/app/src/options.rs
+++ b/crates/app/src/options.rs
@@ -1013,7 +1013,7 @@ impl OptionsDialog {
let line = crate::i18n::fmt(
// i18n: checkbox under the number of connections per download; {n} is 32
crate::i18n::t("Allow more than {n} connections"),
- &[("n", &locked.to_string())],
+ &[("n", &crate::i18n::integer(locked as u64))],
);
let was = self.s.many_connections;
winui::checkbox_light(
@@ -1060,7 +1060,10 @@ impl OptionsDialog {
.map(|(h, n)| {
// i18n: a number of connections the app learned by itself for a server
let auto = crate::i18n::t("{n} (auto)");
- vec![h.clone(), crate::i18n::fmt(auto, &[("n", &n.to_string())])]
+ vec![
+ h.clone(),
+ crate::i18n::fmt(auto, &[("n", &crate::i18n::integer(u64::from(*n)))]),
+ ]
}),
)
.collect();
@@ -2764,7 +2767,7 @@ fn dns_dialog(ui: &mut Ui, o: Pos2, e: &mut DnsEdit) -> Outcome<()> {
// i18n: Options → DNS settings: the test's answer; {ms} milliseconds, {ip} the address found
crate::i18n::t("Answered in {ms} ms: {ip}"),
&[
- ("ms", &took.as_millis().to_string()),
+ ("ms", &crate::i18n::integer(took.as_millis() as u64)),
(
"ip",
&ips.first().map(|i| i.to_string()).unwrap_or_default(),
diff --git a/crates/app/src/progress.rs b/crates/app/src/progress.rs
index b430942f..debcc829 100644
--- a/crates/app/src/progress.rs
+++ b/crates/app/src/progress.rs
@@ -954,7 +954,15 @@ fn status_page(ui: &Ui, o: Pos2, item: &Item, snap: Option<&Snapshot>, frac: f64
crate::i18n::mark("Downloaded"),
102.0,
135.0,
- format!("{} ( {:.2} % )", size3(item.downloaded), frac * 100.0),
+ if crate::i18n::code() == "en" {
+ format!("{} ( {:.2} % )", size3(item.downloaded), frac * 100.0) // not translated: English path (IDM's spacing)
+ } else {
+ format!(
+ "{} ( {} )",
+ size3(item.downloaded),
+ crate::i18n::percent(frac, 2)
+ )
+ },
),
(
crate::i18n::mark("Transfer rate"),
@@ -1093,9 +1101,9 @@ impl Progress {
// i18n: a torrent file the user chose not to download
crate::i18n::t("Skipped").to_owned()
} else if f.size == 0 {
- "100 %".to_owned()
+ crate::i18n::percent(1.0, 0)
} else {
- format!("{:.1} %", 100.0 * *done as f64 / f.size as f64)
+ crate::i18n::percent(*done as f64 / f.size as f64, 1)
};
vec![f.path.display().to_string(), model::size(f.size), pct]
})
@@ -1212,9 +1220,9 @@ impl Progress {
let sent = spec.map_or(0, |t| t.uploaded);
let done = st.map_or(item.downloaded, |s| s.done);
let ratio = if done > 0 {
- format!("{:.2}", sent as f64 / done as f64)
+ crate::i18n::decimal(sent as f64 / done as f64, 2)
} else {
- "0.00".into()
+ crate::i18n::decimal(0.0, 2)
};
let shared = spec.map_or(0, |t| t.seeded_secs);
let hash = item