Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions docs/src/l10n.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,29 @@ You can override the locale at runtime by running:

## 📥 Retrieving Messages

Two APIs are available:
We have a single macro to handle translations.
It can be used in two ways:

### `get_message(id: &str) -> String`
### `translate!(id: &str) -> String`

Returns the message from the current locale bundle.

```
let msg = get_message("id-greeting");
let msg = translate!("id-greeting");
```

If not found, falls back to `en-US`. If still missing, returns the ID itself.

---

### `get_message_with_args(id: &str, args: HashMap<String, String>) -> String`
### `translate!(id: &str, args: key-value pairs) -> String`

Supports variable interpolation and pluralization.

```
let msg = get_message_with_args(
let msg = translate!(
"error-io",
HashMap::from([
("error".to_string(), std::io::Error::last_os_error().to_string())
])
"error" => std::io::Error::last_os_error()
);
```

Expand Down
13 changes: 13 additions & 0 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/uu/arch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ path = "src/arch.rs"
platform-info = { workspace = true }
clap = { workspace = true }
uucore = { workspace = true }
fluent = { workspace = true }

[[bin]]
name = "arch"
Expand Down
8 changes: 4 additions & 4 deletions src/uu/arch/src/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use platform_info::*;

use clap::Command;
use uucore::error::{UResult, USimpleError};
use uucore::locale::get_message;
use uucore::translate;

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
uu_app().try_get_matches_from(args)?;

let uts =
PlatformInfo::new().map_err(|_e| USimpleError::new(1, get_message("cannot-get-system")))?;
PlatformInfo::new().map_err(|_e| USimpleError::new(1, translate!("cannot-get-system")))?;

println!("{}", uts.machine().to_string_lossy().trim());
Ok(())
Expand All @@ -23,7 +23,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
pub fn uu_app() -> Command {
Command::new(uucore::util_name())
.version(uucore::crate_version!())
.about(get_message("arch-about"))
.after_help(get_message("arch-after-help"))
.about(translate!("arch-about"))
.after_help(translate!("arch-after-help"))
.infer_long_args(true)
}
1 change: 1 addition & 0 deletions src/uu/base32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ path = "src/base32.rs"
[dependencies]
clap = { workspace = true }
uucore = { workspace = true, features = ["encoding"] }
fluent = { workspace = true }

[[bin]]
name = "base32"
Expand Down
6 changes: 3 additions & 3 deletions src/uu/base32/src/base32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
pub mod base_common;

use clap::Command;
use uucore::{encoding::Format, error::UResult, locale::get_message};
use uucore::{encoding::Format, error::UResult, translate};

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Expand All @@ -23,7 +23,7 @@ pub fn uu_app() -> Command {
}

fn get_info() -> (&'static str, &'static str) {
let about: &'static str = Box::leak(get_message("base32-about").into_boxed_str());
let usage: &'static str = Box::leak(get_message("base32-usage").into_boxed_str());
let about: &'static str = Box::leak(translate!("base32-about").into_boxed_str());
let usage: &'static str = Box::leak(translate!("base32-usage").into_boxed_str());
(about, usage)
}
35 changes: 8 additions & 27 deletions src/uu/base32/src/base_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// spell-checker:ignore hexupper lsbf msbf unpadded nopad aGVsbG8sIHdvcmxkIQ

use clap::{Arg, ArgAction, Command};
use std::collections::HashMap;
use std::fs::File;
use std::io::{self, ErrorKind, Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
Expand All @@ -18,7 +17,7 @@ use uucore::encoding::{
use uucore::encoding::{EncodingWrapper, SupportsFastDecodeAndEncode};
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::format_usage;
use uucore::locale::{get_message, get_message_with_args};
use uucore::translate;

pub const BASE_CMD_PARSE_ERROR: i32 = 1;

Expand Down Expand Up @@ -52,10 +51,7 @@ impl Config {
if let Some(extra_op) = values.next() {
return Err(UUsageError::new(
BASE_CMD_PARSE_ERROR,
get_message_with_args(
"base-common-extra-operand",
HashMap::from([("operand".to_string(), extra_op.quote().to_string())]),
),
translate!("base-common-extra-operand", "operand" => extra_op.quote()),
));
}

Expand All @@ -67,13 +63,7 @@ impl Config {
if !path.exists() {
return Err(USimpleError::new(
BASE_CMD_PARSE_ERROR,
get_message_with_args(
"base-common-no-such-file",
HashMap::from([(
"file".to_string(),
path.maybe_quote().to_string(),
)]),
),
translate!("base-common-no-such-file", "file" => path.maybe_quote()),
));
}

Expand All @@ -89,10 +79,7 @@ impl Config {
num.parse::<usize>().map_err(|_| {
USimpleError::new(
BASE_CMD_PARSE_ERROR,
get_message_with_args(
"base-common-invalid-wrap-size",
HashMap::from([("size".to_string(), num.quote().to_string())]),
),
translate!("base-common-invalid-wrap-size", "size" => num.quote()),
)
})
})
Expand Down Expand Up @@ -128,15 +115,15 @@ pub fn base_app(about: &'static str, usage: &str) -> Command {
.short('d')
.visible_short_alias('D')
.long(options::DECODE)
.help(get_message("base-common-help-decode"))
.help(translate!("base-common-help-decode"))
.action(ArgAction::SetTrue)
.overrides_with(options::DECODE),
)
.arg(
Arg::new(options::IGNORE_GARBAGE)
.short('i')
.long(options::IGNORE_GARBAGE)
.help(get_message("base-common-help-ignore-garbage"))
.help(translate!("base-common-help-ignore-garbage"))
.action(ArgAction::SetTrue)
.overrides_with(options::IGNORE_GARBAGE),
)
Expand All @@ -145,10 +132,7 @@ pub fn base_app(about: &'static str, usage: &str) -> Command {
.short('w')
.long(options::WRAP)
.value_name("COLS")
.help(get_message_with_args(
"base-common-help-wrap",
HashMap::from([("default".to_string(), WRAP_DEFAULT.to_string())]),
))
.help(translate!("base-common-help-wrap", "default" => WRAP_DEFAULT))
.overrides_with(options::WRAP),
)
// "multiple" arguments are used to check whether there is more than one
Expand Down Expand Up @@ -830,10 +814,7 @@ fn format_read_error(kind: ErrorKind) -> String {
}
}

get_message_with_args(
"base-common-read-error",
HashMap::from([("error".to_string(), kind_string_capitalized)]),
)
translate!("base-common-read-error", "error" => kind_string_capitalized)
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions src/uu/base64/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ path = "src/base64.rs"
clap = { workspace = true }
uucore = { workspace = true, features = ["encoding"] }
uu_base32 = { workspace = true }
fluent = { workspace = true }

[[bin]]
name = "base64"
Expand Down
7 changes: 4 additions & 3 deletions src/uu/base64/src/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

use clap::Command;
use uu_base32::base_common;
use uucore::{encoding::Format, error::UResult, locale::get_message};
use uucore::translate;
use uucore::{encoding::Format, error::UResult};

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Expand All @@ -22,7 +23,7 @@ pub fn uu_app() -> Command {
}

fn get_info() -> (&'static str, &'static str) {
let about: &'static str = Box::leak(get_message("base64-about").into_boxed_str());
let usage: &'static str = Box::leak(get_message("base64-usage").into_boxed_str());
let about: &'static str = Box::leak(translate!("base64-about").into_boxed_str());
let usage: &'static str = Box::leak(translate!("base64-usage").into_boxed_str());
(about, usage)
}
1 change: 1 addition & 0 deletions src/uu/basename/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ path = "src/basename.rs"
[dependencies]
clap = { workspace = true }
uucore = { workspace = true }
fluent = { workspace = true }

[[bin]]
name = "basename"
Expand Down
Loading
Loading