Skip to content

Commit 069a43a

Browse files
authored
Merge pull request #8092 from sylvestre/l10n-uptime
l10n: port uptime to translation + add french
2 parents ba447eb + a0e45f2 commit 069a43a

4 files changed

Lines changed: 120 additions & 31 deletions

File tree

src/uu/uptime/locales/en-US.ftl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,37 @@ uptime-usage = uptime [OPTION]...
55
uptime-about-musl-warning = Warning: When built with musl libc, the `uptime` utility may show '0 users'
66
due to musl's stub implementation of utmpx functions. Boot time and load averages
77
are still calculated using alternative mechanisms.
8+
9+
# Help messages
10+
uptime-help-since = system up since
11+
uptime-help-path = file to search boot time from
12+
13+
# Error messages
14+
uptime-error-io = couldn't get boot time: { $error }
15+
uptime-error-target-is-dir = couldn't get boot time: Is a directory
16+
uptime-error-target-is-fifo = couldn't get boot time: Illegal seek
17+
uptime-error-couldnt-get-boot-time = couldn't get boot time
18+
19+
# Output messages
20+
uptime-output-unknown-uptime = up ???? days ??:??,
21+
22+
uptime-user-count = { $count ->
23+
[one] 1 user
24+
*[other] { $count } users
25+
}
26+
27+
# Error messages
28+
uptime-lib-error-system-uptime = could not retrieve system uptime
29+
uptime-lib-error-system-loadavg = could not retrieve system load average
30+
uptime-lib-error-windows-loadavg = Windows does not have an equivalent to the load average on Unix-like systems
31+
uptime-lib-error-boot-time = boot time larger than current time
32+
33+
# Uptime formatting
34+
uptime-format = { $days ->
35+
[0] { $time }
36+
[one] { $days } day, { $time }
37+
*[other] { $days } days { $time }
38+
}
39+
40+
# Load average formatting
41+
uptime-lib-format-loadavg = load average: { $avg1 }, { $avg5 }, { $avg15 }

src/uu/uptime/locales/fr-FR.ftl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
uptime-about = Afficher l'heure actuelle, la durée pendant laquelle le système a été actif,
2+
le nombre d'utilisateurs sur le système, et le nombre moyen de tâches
3+
dans la file d'attente d'exécution au cours des 1, 5 et 15 dernières minutes.
4+
uptime-usage = uptime [OPTION]...
5+
uptime-about-musl-warning = Avertissement : Lorsque compilé avec musl libc, l'utilitaire `uptime` peut afficher '0 utilisateur'
6+
en raison de l'implémentation stub des fonctions utmpx de musl. L'heure de démarrage et les moyennes de charge
7+
sont toujours calculées en utilisant des mécanismes alternatifs.
8+
9+
# Messages d'aide
10+
uptime-help-since = système actif depuis
11+
uptime-help-path = fichier pour rechercher l'heure de démarrage
12+
13+
# Messages d'erreur
14+
uptime-error-io = impossible d'obtenir l'heure de démarrage : { $error }
15+
uptime-error-target-is-dir = impossible d'obtenir l'heure de démarrage : Est un répertoire
16+
uptime-error-target-is-fifo = impossible d'obtenir l'heure de démarrage : Recherche illégale
17+
uptime-error-couldnt-get-boot-time = impossible d'obtenir l'heure de démarrage
18+
19+
# Messages de sortie
20+
uptime-output-unknown-uptime = actif ???? jours ??:??,
21+
22+
uptime-user-count = { $count ->
23+
[one] 1 utilisateur
24+
*[other] { $count } utilisateurs
25+
}
26+
27+
# Messages d'erreur
28+
uptime-lib-error-system-uptime = impossible de récupérer la durée de fonctionnement du système
29+
uptime-lib-error-system-loadavg = impossible de récupérer la charge moyenne du système
30+
uptime-lib-error-windows-loadavg = Windows n'a pas d'équivalent à la charge moyenne des systèmes de type Unix
31+
uptime-lib-error-boot-time = heure de démarrage supérieure à l'heure actuelle
32+
33+
# Formatage de la durée de fonctionnement
34+
uptime-format = { $days ->
35+
[0] { $time }
36+
[one] { $days } jour, { $time }
37+
*[other] { $days } jours { $time }
38+
}
39+
40+
# Formatage de la charge moyenne
41+
uptime-lib-format-loadavg = charge moyenne : { $avg1 }, { $avg5 }, { $avg15 }

src/uu/uptime/src/uptime.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
// spell-checker:ignore getloadavg behaviour loadavg uptime upsecs updays upmins uphours boottime nusers utmpxname gettime clockid
6+
// spell-checker:ignore getloadavg behaviour loadavg uptime upsecs updays upmins uphours boottime nusers utmpxname gettime clockid couldnt
77

88
use chrono::{Local, TimeZone, Utc};
9+
use std::collections::HashMap;
910
#[cfg(unix)]
1011
use std::ffi::OsString;
1112
use std::io;
@@ -17,8 +18,8 @@ use uucore::uptime::*;
1718
use clap::{Arg, ArgAction, Command, ValueHint, builder::ValueParser};
1819

1920
use uucore::format_usage;
20-
use uucore::locale::get_message;
2121

22+
use uucore::locale::{get_message, get_message_with_args};
2223
#[cfg(unix)]
2324
#[cfg(not(target_os = "openbsd"))]
2425
use uucore::utmpx::*;
@@ -31,11 +32,11 @@ pub mod options {
3132
#[derive(Debug, Error)]
3233
pub enum UptimeError {
3334
// io::Error wrapper
34-
#[error("couldn't get boot time: {0}")]
35+
#[error("{}", get_message_with_args("uptime-error-io", HashMap::from([("error".to_string(), format!("{}", .0))])))]
3536
IoErr(#[from] io::Error),
36-
#[error("couldn't get boot time: Is a directory")]
37+
#[error("{}", get_message("uptime-error-target-is-dir"))]
3738
TargetIsDir,
38-
#[error("couldn't get boot time: Illegal seek")]
39+
#[error("{}", get_message("uptime-error-target-is-fifo"))]
3940
TargetIsFifo,
4041
}
4142

@@ -78,13 +79,13 @@ pub fn uu_app() -> Command {
7879
Arg::new(options::SINCE)
7980
.short('s')
8081
.long(options::SINCE)
81-
.help("system up since")
82+
.help(get_message("uptime-help-since"))
8283
.action(ArgAction::SetTrue),
8384
);
8485
#[cfg(unix)]
8586
cmd.arg(
8687
Arg::new(options::PATH)
87-
.help("file to search boot time from")
88+
.help(get_message("uptime-help-path"))
8889
.action(ArgAction::Set)
8990
.num_args(0..=1)
9091
.value_parser(ValueParser::os_string())
@@ -130,9 +131,9 @@ fn uptime_with_file(file_path: &OsString) -> UResult<()> {
130131
let bytes = file_path.as_os_str().as_bytes();
131132

132133
if bytes[bytes.len() - 1] != b'x' {
133-
show_error!("couldn't get boot time");
134+
show_error!("{}", get_message("uptime-error-couldnt-get-boot-time"));
134135
print_time();
135-
print!("up ???? days ??:??,");
136+
print!("{}", get_message("uptime-output-unknown-uptime"));
136137
print_nusers(Some(0));
137138
print_loadavg();
138139
set_exit_code(1);
@@ -142,7 +143,7 @@ fn uptime_with_file(file_path: &OsString) -> UResult<()> {
142143

143144
if non_fatal_error {
144145
print_time();
145-
print!("up ???? days ??:??,");
146+
print!("{}", get_message("uptime-output-unknown-uptime"));
146147
print_nusers(Some(0));
147148
print_loadavg();
148149
return Ok(());
@@ -157,10 +158,10 @@ fn uptime_with_file(file_path: &OsString) -> UResult<()> {
157158
if let Some(time) = boot_time {
158159
print_uptime(Some(time))?;
159160
} else {
160-
show_error!("couldn't get boot time");
161+
show_error!("{}", get_message("uptime-error-couldnt-get-boot-time"));
161162
set_exit_code(1);
162163

163-
print!("up ???? days ??:??,");
164+
print!("{}", get_message("uptime-output-unknown-uptime"));
164165
}
165166
user_count = count;
166167
}
@@ -171,10 +172,10 @@ fn uptime_with_file(file_path: &OsString) -> UResult<()> {
171172
if upsecs >= 0 {
172173
print_uptime(Some(upsecs))?;
173174
} else {
174-
show_error!("couldn't get boot time");
175+
show_error!("{}", get_message("uptime-error-couldnt-get-boot-time"));
175176
set_exit_code(1);
176177

177-
print!("up ???? days ??:??,");
178+
print!("{}", get_message("uptime-output-unknown-uptime"));
178179
}
179180
user_count = get_nusers(file_path.to_str().expect("invalid utmp path file"));
180181
}

src/uucore/src/lib/features/uptime.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@
1313
// See https://github.com/uutils/coreutils/pull/7289 for discussion.
1414

1515
use crate::error::{UError, UResult};
16+
use crate::locale::{get_message, get_message_with_args};
1617
use chrono::Local;
1718
use libc::time_t;
19+
use std::collections::HashMap;
1820
use thiserror::Error;
1921

2022
#[derive(Debug, Error)]
2123
pub enum UptimeError {
22-
#[error("could not retrieve system uptime")]
24+
#[error("{}", get_message("uptime-lib-error-system-uptime"))]
2325
SystemUptime,
24-
#[error("could not retrieve system load average")]
26+
#[error("{}", get_message("uptime-lib-error-system-loadavg"))]
2527
SystemLoadavg,
26-
#[error("Windows does not have an equivalent to the load average on Unix-like systems")]
28+
#[error("{}", get_message("uptime-lib-error-windows-loadavg"))]
2729
WindowsLoadavg,
28-
#[error("boot time larger than current time")]
30+
#[error("{}", get_message("uptime-lib-error-boot-time"))]
2931
BootTime,
3032
}
3133

@@ -174,11 +176,14 @@ pub fn get_formatted_uptime(boot_time: Option<time_t>) -> UResult<String> {
174176
let up_days = up_secs / 86400;
175177
let up_hours = (up_secs - (up_days * 86400)) / 3600;
176178
let up_mins = (up_secs - (up_days * 86400) - (up_hours * 3600)) / 60;
177-
match up_days.cmp(&1) {
178-
std::cmp::Ordering::Equal => Ok(format!("{up_days:1} day, {up_hours:2}:{up_mins:02}")),
179-
std::cmp::Ordering::Greater => Ok(format!("{up_days:1} days {up_hours:2}:{up_mins:02}")),
180-
_ => Ok(format!("{up_hours:2}:{up_mins:02}")),
181-
}
179+
180+
Ok(get_message_with_args(
181+
"uptime-format",
182+
HashMap::from([
183+
("days".to_string(), up_days.to_string()),
184+
("time".to_string(), format!("{up_hours:02}:{up_mins:02}")),
185+
]),
186+
))
182187
}
183188

184189
/// Get the number of users currently logged in
@@ -305,11 +310,10 @@ pub fn get_nusers() -> usize {
305310
/// e.g. "0 users", "1 user", "2 users"
306311
#[inline]
307312
pub fn format_nusers(n: usize) -> String {
308-
if n == 1 {
309-
String::from("1 user")
310-
} else {
311-
format!("{n} users")
312-
}
313+
get_message_with_args(
314+
"uptime-user-count",
315+
HashMap::from([("count".to_string(), n.to_string())]),
316+
)
313317
}
314318

315319
/// Get the number of users currently logged in in a human-readable format
@@ -368,18 +372,27 @@ pub fn get_loadavg() -> UResult<(f64, f64, f64)> {
368372
#[inline]
369373
pub fn get_formatted_loadavg() -> UResult<String> {
370374
let loadavg = get_loadavg()?;
371-
Ok(format!(
372-
"load average: {:.2}, {:.2}, {:.2}",
373-
loadavg.0, loadavg.1, loadavg.2
375+
Ok(get_message_with_args(
376+
"uptime-lib-format-loadavg",
377+
HashMap::from([
378+
("avg1".to_string(), format!("{:.2}", loadavg.0)),
379+
("avg5".to_string(), format!("{:.2}", loadavg.1)),
380+
("avg15".to_string(), format!("{:.2}", loadavg.2)),
381+
]),
374382
))
375383
}
376384

377385
#[cfg(test)]
378386
mod tests {
379387
use super::*;
388+
use crate::locale;
380389

381390
#[test]
382391
fn test_format_nusers() {
392+
unsafe {
393+
std::env::set_var("LANG", "en_US.UTF-8");
394+
}
395+
let _ = locale::setup_localization("uptime");
383396
assert_eq!("0 users", format_nusers(0));
384397
assert_eq!("1 user", format_nusers(1));
385398
assert_eq!("2 users", format_nusers(2));

0 commit comments

Comments
 (0)