8000 Merge pull request #8175 from sylvestre/l10n-shuf · uutils/coreutils@ce617a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce617a5

Browse files
authored
Merge pull request #8175 from sylvestre/l10n-shuf
l10n: port shuf for translation + add french
2 parents 9ccaa54 + 51017b6 commit ce617a5

File tree

3 files changed

+87
-19
lines changed

3 files changed

+87
-19
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,22 @@ shuf-about = Shuffle the input by outputting a random permutation of input lines
44
shuf-usage = shuf [OPTION]... [FILE]
55
shuf -e [OPTION]... [ARG]...
66
shuf -i LO-HI [OPTION]...
7+
8+
# Help messages
9+
shuf-help-echo = treat each ARG as an input line
10+
shuf-help-input-range = treat each number LO through HI as an input line
11+
shuf-help-head-count = output at most COUNT lines
12+
shuf-help-output = write result to FILE instead of standard output
13+
shuf-help-random-source = get random bytes from FILE
14+
shuf-help-repeat = output lines can be repeated
15+
shuf-help-zero-terminated = line delimiter is NUL, not newline
16+
17+
# Error messages
18+
shuf-error-unexpected-argument = unexpected argument { $arg } found
19+
shuf-error-failed-to-open-for-writing = failed to open { $file } for writing
20+
shuf-error-failed-to-open-random-source = failed to open random source { $file }
21+
shuf-error-read-error = read error
22+
shuf-error-no-lines-to-repeat = no lines to repeat
23+
shuf-error-start-exceeds-end = start exceeds end
24+
shuf-error-missing-dash = missing '-'
25+
shuf-error-write-failed = write failed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
shuf-about = Mélanger l'entrée en affichant une permutation aléatoire des lignes d'entrée.
2+
Chaque permutation de sortie est également probable.
3+
Sans FICHIER, ou quand FICHIER est -, lire l'entrée standard.
4+
shuf-usage = shuf [OPTION]... [FICHIER]
5+
shuf -e [OPTION]... [ARG]...
6+
shuf -i MIN-MAX [OPTION]...
7+
8+
# Messages d'aide
9+
shuf-help-echo = traiter chaque ARG comme une ligne d'entrée
10+
shuf-help-input-range = traiter chaque nombre de MIN à MAX comme une ligne d'entrée
11+
shuf-help-head-count = afficher au maximum NOMBRE lignes
12+
shuf-help-output = écrire le résultat dans FICHIER au lieu de la sortie standard
13+
shuf-help-random-source = obtenir des octets aléatoires depuis FICHIER
14+
shuf-help-repeat = les lignes de sortie peuvent être répétées
15+
shuf-help-zero-terminated = le délimiteur de ligne est NUL, pas nouvelle ligne
16+
17+
# Messages d'erreur
18+
shuf-error-unexpected-argument = argument inattendu { $arg } trouvé
19+
shuf-error-failed-to-open-for-writing = échec de l'ouverture de { $file } en écriture
20+
shuf-error-failed-to-open-random-source = échec de l'ouverture de la source aléatoire { $file }
21+
shuf-error-read-error = erreur de lecture
22+
shuf-error-no-lines-to-repeat = aucune ligne à répéter
23+
shuf-error-start-exceeds-end = le début dépasse la fin
24+
shuf-error-missing-dash = '-' manquant
25+
shuf-error-write-failed = échec de l'écriture

src/uu/shuf/src/shuf.rs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use clap::{Arg, ArgAction, Command};
1010
use rand::prelude::SliceRandom;
1111
use rand::seq::IndexedRandom;
1212
use rand::{Rng, RngCore};
13-
use std::collections::HashSet;
13+
use std::collections::{HashMap, HashSet};
1414
use std::ffi::{OsStr, OsString};
1515
use std::fs::File;
1616
use std::io::{BufWriter, Error, Read, Write, stdin, stdout};
@@ -20,7 +20,7 @@ use std::str::FromStr;
2020
use uucore::display::{OsWrite, Quotable};
2121
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
2222
use uucore::format_usage;
23-
use uucore::locale::get_message;
23+
use uucore::locale::{get_message, get_message_with_args};
2424

2525
mod rand_read_adapter;
2626

@@ -71,7 +71,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
7171
if let Some(second_file) = operands.next() {
7272
return Err(UUsageError::new(
7373
1,
74-
format!("unexpected argument {} found", second_file.quote()),
74+
get_message_with_args(
75+
"shuf-error-unexpected-argument",
76+
HashMap::from([("arg".to_string(), second_file.quote().to_string())]),
77+
),
7578
));
7679
};
7780
Mode::Default(file.into())
@@ -101,8 +104,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
101104
let mut output = BufWriter::new(match options.output {
102105
None => Box::new(stdout()) as Box<dyn OsWrite>,
103106
Some(ref s) => {
104-
let file = File::create(s)
105-
.map_err_context(|| format!("failed to open {} for writing", s.quote()))?;
107+
let file = File::create(s).map_err_context(|| {
108+
get_message_with_args(
109+
"shuf-error-failed-to-open-for-writing",
110+
HashMap::from([("file".to_string(), s.quote().to_string())]),
111+
)
112+
})?;
106113
Box::new(file) as Box<dyn OsWrite>
107114
}
108115
});
@@ -114,8 +121,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
114121

115122
let mut rng = match options.random_source {
116123
Some(ref r) => {
117-
let file = File::open(r)
118-
.map_err_context(|| format!("failed to open random source {}", r.quote()))?;
124+
let file = File::open(r).map_err_context(|| {
125+
get_message_with_args(
126+
"shuf-error-failed-to-open-random-source",
127+
HashMap::from([("file".to_string(), r.quote().to_string())]),
128+
)
129+
})?;
119130
WrappedRng::RngFile(rand_read_adapter::ReadRng::new(file))
120131
}
121132
None => WrappedRng::RngDefault(rand::rng()),
@@ -149,7 +160,7 @@ pub fn uu_app() -> Command {
149160
Arg::new(options::ECHO)
150161
.short('e')
151162
.long(options::ECHO)
152-
.help("treat each ARG as an input line")
163+
.help(get_message("shuf-help-echo"))
153164
.action(ArgAction::SetTrue)
154165
.overrides_with(options::ECHO)
155166
.conflicts_with(options::INPUT_RANGE),
@@ -159,7 +170,7 @@ pub fn uu_app() -> Command {
159170
.short('i')
160171
.long(options::INPUT_RANGE)
161172
.value_name("LO-HI")
162-
.help("treat each number LO through HI as an input line")
173+
.help(get_message("shuf-help-input-range"))
163174
.value_parser(parse_range)
164175
.conflicts_with(options::FILE_OR_ARGS),
165176
)
@@ -169,39 +180,39 @@ pub fn uu_app() -> Command {
169180
.long(options::HEAD_COUNT)
170181
.value_name("COUNT")
171182
.action(ArgAction::Append)
172-
.help("output at most COUNT lines")
183+
.help(get_message("shuf-help-head-count"))
173184
.value_parser(usize::from_str),
174185
)
175186
.arg(
176187
Arg::new(options::OUTPUT)
177188
.short('o')
178189
.long(options::OUTPUT)
179190
.value_name("FILE")
180-
.help("write result to FILE instead of standard output")
191+
.help(get_message("shuf-help-output"))
181192
.value_parser(ValueParser::path_buf())
182193
.value_hint(clap::ValueHint::FilePath),
183194
)
184195
.arg(
185196
Arg::new(options::RANDOM_SOURCE)
186197
.long(options::RANDOM_SOURCE)
187198
.value_name("FILE")
188-
.help("get random bytes from FILE")
199+
.help(get_message("shuf-help-random-source"))
189200
.value_parser(ValueParser::path_buf())
190201
.value_hint(clap::ValueHint::FilePath),
191202
)
192203
.arg(
193204
Arg::new(options::REPEAT)
194205
.short('r')
195206
.long(options::REPEAT)
196-
.help("output lines can be repeated")
207+
.help(get_message("shuf-help-repeat"))
197208
.action(ArgAction::SetTrue)
198209
.overrides_with(options::REPEAT),
199210
)
200211
.arg(
201212
Arg::new(options::ZERO_TERMINATED)
202213
.short('z')
203214
.long(options::ZERO_TERMINATED)
204-
.help("line delimiter is NUL, not newline")
215+
.help(get_message("shuf-help-zero-terminated"))
205216
.action(ArgAction::SetTrue)
206217
.overrides_with(options::ZERO_TERMINATED),
207218
)
@@ -218,7 +229,7 @@ fn read_input_file(filename: &Path) -> UResult<Vec<u8>> {
218229
let mut data = Vec::new();
219230
stdin()
220231
.read_to_end(&mut data)
221-
.map_err_context(|| "read error".into())?;
232+
.map_err_context(|| get_message("shuf-error-read-error"))?;
222233
Ok(data)
223234
} else {
224235
std::fs::read(filename).map_err_context(|| filename.maybe_quote().to_string())
@@ -250,15 +261,18 @@ trait Shufable {
250261

251262
impl<'a> Shufable for Vec<&'a [u8]> {
252263
type Item = &'a [u8];
264+
253265
fn is_empty(&self) -> bool {
254266
(**self).is_empty()
255267
}
268+
256269
fn choose(&self, rng: &mut WrappedRng) -> Self::Item {
257270
// Note: "copied()" only copies the reference, not the entire [u8].
258271
// Returns None if the slice is empty. We checked this before, so
259272
// this is safe.
260273
(**self).choose(rng).unwrap()
261274
}
275+
262276
fn partial_shuffle<'b>(
263277
&'b mut self,
264278
rng: &'b mut WrappedRng,
@@ -271,12 +285,15 @@ impl<'a> Shufable for Vec<&'a [u8]> {
271285

272286
impl<'a> Shufable for Vec<&'a OsStr> {
273287
type Item = &'a OsStr;
288+
274289
fn is_empty(&self) -> bool {
275290
(**self).is_empty()
276291
}
292+
277293
fn choose(&self, rng: &mut WrappedRng) -> Self::Item {
278294
(**self).choose(rng).unwrap()
279295
}
296+
280297< F438 /td>
fn partial_shuffle<'b>(
281298
&'b mut self,
282299
rng: &'b mut WrappedRng,
@@ -288,12 +305,15 @@ impl<'a> Shufable for Vec<&'a OsStr> {
288305

289306
impl Shufable for RangeInclusive<usize> {
290307
type Item = usize;
308+
291309
fn is_empty(&self) -> bool {
292310
self.is_empty()
293311
}
312+
294313
fn choose(&self, rng: &mut WrappedRng) -> usize {
295314
rng.random_range(self.clone())
296315
}
316+
297317
fn partial_shuffle<'b>(
298318
&'b mut self,
299319
rng: &'b mut WrappedRng,
@@ -420,10 +440,14 @@ fn shuf_exec(
420440
rng: &mut WrappedRng,
421441
output: &mut BufWriter<Box<dyn OsWrite>>,
422442
) -> UResult<()> {
423-
let ctx = || "write failed".to_string();
443+
let ctx = || get_message("shuf-error-write-failed");
444+
424445
if opts.repeat {
425446
if input.is_empty() {
426-
return Err(USimpleError::new(1, "no lines to repeat"));
447+
return Err(USimpleError::new(
448+
1,
449+
get_message("shuf-error-no-lines-to-repeat"),
450+
));
427451
}
428452
for _ in 0..opts.head_count {
429453
let r = input.choose(rng);
@@ -449,10 +473,10 @@ fn parse_range(input_range: &str) -> Result<RangeInclusive<usize>, String> {
449473
if begin <= end || begin == end + 1 {
450474
Ok(begin..=end)
451475
} else {
452-
Err("start exceeds end".into())
476+
Err(get_message("shuf-error-start-exceeds-end"))
453477
}
454478
} else {
455-
Err("missing '-'".into())
479+
Err(get_message("shuf-error-missing-dash"))
456480
}
457481
}
458482

0 commit comments

Comments
 (0)
0