feat: i18n

This commit is contained in:
iff 2024-09-25 17:55:55 +02:00
parent eeeb5791e3
commit eb66bc2c24
6 changed files with 288 additions and 37 deletions

View file

@ -34,7 +34,7 @@ pub fn handle_args() {
}
if shell.is_empty() {
eprintln!("No shell specified. Please specify a shell.");
eprintln!("{}", t!("no-shell"));
std::process::exit(1);
}
@ -44,23 +44,16 @@ pub fn handle_args() {
}
fn print_help() {
let help_message = String::from(
"
Usage: pay_respects [your shell] [--alias [alias]]
Example 1, manual aliasing: `pay_respects bash`
The command will output the command that you can use to execute the binary with
the correct environment. You can alias such output to a shorter key. Such as
alias f=$(pay_respects bash)
Example 2, auto aliasing: `pay_respects bash --alias f`
The command will output a declaration that can be directly embedded in your
config file with `eval $(pay_respects bash --alias)`. For fish, use
`pay_respects fish --alias | source` instead.
",
println!(
"{}",
t!(
"help",
manual = "pay-respects bash",
manual_example = "alias f=$(pay-respects bash)",
auto = "pay-respects bash --alias f",
auto_example = "eval $(pay-respects bash --alias f)",
auto_example_fish = "pay-respects fish --alias | source",
)
);
println!("{}", help_message);
std::process::exit(0);
}

View file

@ -23,6 +23,10 @@ mod shell;
mod style;
mod suggestions;
#[macro_use]
extern crate rust_i18n;
i18n!("i18n", fallback = "en", minify_key = true);
fn main() {
colored::control::set_override(true);
@ -31,7 +35,10 @@ fn main() {
let shell = match std::env::var("_PR_SHELL") {
Ok(shell) => shell,
Err(_) => {
eprintln!("No _PR_SHELL in environment. Did you aliased the command with the correct argument?\n\nUse `pay-respects -h` for help");
eprintln!(
"{}",
t!("no-env-setup", var = "_PR_SHELL", help = "pay-respects -h")
);
std::process::exit(1);
}
};
@ -57,11 +64,10 @@ fn main() {
let msg = execution.err().unwrap();
error_msg = msg.to_lowercase();
let retry_message =
format!("{}", "Looking for new suggestion...".cyan().bold());
let retry_message = format!("{}...", t!("retry"));
// println!("\n{} {}", "ERROR:".red().bold(), msg);
eprintln!("\n{}\n", retry_message);
eprintln!("\n{}\n", retry_message.cyan().bold());
}
} else {
break;
@ -70,11 +76,6 @@ fn main() {
break;
}
}
eprintln!(
"No correction found for the command: {}\n",
last_command.red()
);
eprintln!(
"If you think there should be a correction, please open an issue or send a pull request!"
);
eprintln!("{}: {}\n", t!("no-suggestions"), last_command.red());
eprintln!("{}", t!("contribute"));
}

View file

@ -55,7 +55,14 @@ fn last_command(shell: &str) -> String {
let last_command = match std::env::var("_PR_LAST_COMMAND") {
Ok(command) => command,
Err(_) => {
eprintln!("No _PR_LAST_COMMAND in environment. Did you aliased the command with the correct argument?\n\nUse `pay-respects -h` for help");
eprintln!(
"{}",
t!(
"no-env-setup",
var = "_PR_LAST_COMMAND",
help = "pay-respects -h"
)
);
exit(1);
}
};
@ -76,9 +83,11 @@ fn last_command(shell: &str) -> String {
}
pub fn last_command_expanded_alias(shell: &str) -> String {
let alias = std::env::var("_PR_ALIAS").expect(
"No _PR_ALIAS in environment. Did you aliased the command with the correct argument?",
);
let alias = std::env::var("_PR_ALIAS").expect(&t!(
"no-env-setup",
var = "_PR_ALIAS",
help = "pay-respects -h"
));
let last_command = last_command(shell);
if alias.is_empty() {
return last_command;

View file

@ -3,6 +3,7 @@ use std::os::fd::AsFd;
use std::process::{exit, Stdio};
use std::time::{Duration, Instant};
use colored::Colorize;
use regex_lite::Regex;
use pay_respects_parser::parse_rules;
@ -13,8 +14,8 @@ use crate::shell::PRIVILEGE_LIST;
pub fn suggest_command(shell: &str, last_command: &str, error_msg: &str) -> Option<String> {
let split_command = split_command(last_command);
let executable = match PRIVILEGE_LIST.contains(&split_command[0].as_str()) {
true => split_command.get(1).expect("No command found.").as_str(),
false => split_command.first().expect("No command found.").as_str(),
true => split_command.get(1).expect(&t!("no-command")).as_str(),
false => split_command.first().expect(&t!("no-command")).as_str(),
};
if !PRIVILEGE_LIST.contains(&executable) {
@ -204,7 +205,8 @@ fn compare_string(a: &str, b: &str) -> usize {
pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Result<(), String> {
eprintln!("{}\n", highlighted);
eprintln!("Press enter to execute the suggestion. Or press Ctrl+C to exit.");
let confirm = format!("[{}]", t!("confirm-yes")).green();
eprintln!("{}: {} {}", t!("confirm"), confirm, "[Ctrl+C]".red());
std::io::stdin().read_line(&mut String::new()).unwrap();
for p in PRIVILEGE_LIST {