| Crates.io | clap-i18n-richformatter |
| lib.rs | clap-i18n-richformatter |
| version | 0.1.4 |
| created_at | 2025-08-28 07:49:55.991658+00 |
| updated_at | 2025-08-29 03:04:28.581472+00 |
| description | Clap rich formatter with i18n support |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1813712 |
| size | 56,244 |
i18n support for clap::error::RichFormatter!
Add clap-i18n-richformatter to Cargo.toml:
clap-i18n-richformatter = "0.1.0"
Initialise clap-i18n-richformatter as such:
use clap::Parser;
use clap_i18n_richformatter::{ClapI18nRichFormatter, init_clap_rich_formatter_localizer};
#[derive(Debug, Parser)]
struct ExampleArg {
#[arg(long, short)]
a: bool,
#[arg(long, short, requires = "a")]
b: bool,
}
fn main() {
init_clap_rich_formatter_localizer();
let args = ExampleArg::try_parse()
.map_err(|e| {
let e = e.apply::<ClapI18nRichFormatter>();
e.exit();
})
.unwrap();
dbg!(args);
}
Running LANG=zh_CN.UTF-8 cargo run -- -b and LANG=C cargo run -- -b will now show localised (and non-localised below) error output for Clap:
错误: 未提供下列必要参数:
--a
用法: arg_requires --a --b
如需了解更多信息,请输入 --help。
error: the following required arguments were not provided:
--a
Usage: arg_requires --a --b
For more information, try '--help'.
Your contribution are welcome via Pull Requests:
From https://github.com/clap-rs/clap/issues/380#issuecomment-1254059266:
In discussing this with someone on reddit, I think users can get something working, even if it isn't streamlined yet
- Replace Commands: header with a localized value via
Command::subcommand_help_heading- Replace
value name with a localized value via Command::subcommand_value_name - Replace Arguments and Options: headers with localized values via Command::next_help_heading and/or Arg::help_heading
- Disable the built-in flags (
Command::disable_help_flag(false)andCommand::disable_version_flag(false)) and provide your own versions with localized Arg::help- If the help template has any hard coded strings, replace them with a localized version via
Command::help_template- Fork clap::error::RichFormatter, replacing any hard coded strings with localized values. Use Error::apply to swap the formatter (e.g. use
Parser::try_parseto get the error, call apply, and thenerr.exit())
clap-i18n-richformatter follows the last suggesion from @epage to implement i18n support for Clap errors, since the error messages are quite finite and predictable.
Fork clap::error::RichFormatter, replacing any hard coded strings with localized values. Use Error::apply to swap the formatter (e.g. use
Parser::try_parseto get the error, call apply, and thenerr.exit())