Skip to content

Commit aa66d12

Browse files
committed
[WIP] Added locales folder for translations and added translation to strings that are used
1 parent 81c4ba6 commit aa66d12

12 files changed

+69
-21
lines changed

locales/app.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
_version: 2
2+
3+
"Dry running:":
4+
en: "Dry running:"
5+
"Rebooting...":
6+
en: "Rebooting..."
7+
"Plugins upgraded":
8+
en: "Plugins upgraded"
9+
"Would self-update":
10+
en: "Would self-update"
11+
"Pulling":
12+
en: "Pulling"
13+
"No Breaking changes":
14+
en: "No Breaking changes"
15+
"in":
16+
en: "in"
17+
"Dropping you to shell. Fix what you need and then exit the shell.":
18+
en: "Dropping you to shell. Fix what you need and then exit the shell."
19+
"Topgrade launched in a new tmux session":
20+
en: "Topgrade launched in a new tmux session"
21+
"Topgrade upgraded to ":
22+
en: "Topgrade upgraded to "
23+
"Topgrade is up-to-date":
24+
en: "Topgrade is up-to-date"
25+
"Updating modules...":
26+
en: "Updating modules..."
27+
"Powershell Modules Update":
28+
en: "Powershell Modules Update"
29+
"Powershell is not installed":
30+
en: "Powershell is not installed"
31+
"Error detecting current distribution:":
32+
en: "Error detecting current distribution:"
33+
"Error:":
34+
en: "Error:"

src/breaking_changes.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::{
1818
path::PathBuf,
1919
str::FromStr,
2020
};
21+
use rust_i18n::t;
2122

2223
/// Version string x.y.z
2324
static VERSION_STR: &str = env!("CARGO_PKG_VERSION");
@@ -121,9 +122,9 @@ pub(crate) fn print_breaking_changes() {
121122
let header = format!("Topgrade {VERSION_STR} Breaking Changes");
122123
print_separator(header);
123124
let contents = if BREAKINGCHANGES.is_empty() {
124-
"No Breaking changes"
125+
t!("No Breaking changes").to_string()
125126
} else {
126-
BREAKINGCHANGES
127+
BREAKINGCHANGES.to_string()
127128
};
128129
println!("{contents}\n");
129130
}

src/executor.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::path::Path;
44
use std::process::{Child, Command, ExitStatus, Output};
55

66
use color_eyre::eyre::Result;
7+
use rust_i18n::t;
78
use tracing::debug;
89

910
use crate::command::CommandExt;
@@ -209,7 +210,8 @@ pub struct DryCommand {
209210
impl DryCommand {
210211
fn dry_run(&self) {
211212
print!(
212-
"Dry running: {} {}",
213+
"{} {} {}",
214+
t!("Dry running:"),
213215
self.program.to_string_lossy(),
214216
shell_words::join(
215217
self.args
@@ -219,7 +221,7 @@ impl DryCommand {
219221
)
220222
);
221223
match &self.directory {
222-
Some(dir) => println!(" in {}", dir.to_string_lossy()),
224+
Some(dir) => println!(" {} {}", t!("in"), dir.to_string_lossy()),
223225
None => println!(),
224226
};
225227
}

src/main.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use etcetera::base_strategy::Windows;
1818
#[cfg(unix)]
1919
use etcetera::base_strategy::Xdg;
2020
use once_cell::sync::Lazy;
21+
use rust_i18n::{i18n, t};
2122
use tracing::debug;
2223

2324
use self::config::{CommandLineArgs, Config, Step};
@@ -54,6 +55,8 @@ pub(crate) static XDG_DIRS: Lazy<Xdg> = Lazy::new(|| Xdg::new().expect("No home
5455
#[cfg(windows)]
5556
pub(crate) static WINDOWS_DIRS: Lazy<Windows> = Lazy::new(|| Windows::new().expect("No home directory"));
5657

58+
i18n!("locales", fallback = "en");
59+
5760
fn run() -> Result<()> {
5861
install_color_eyre()?;
5962
ctrlc::set_handler();
@@ -97,7 +100,7 @@ fn run() -> Result<()> {
97100
};
98101

99102
if opt.show_config_reference() {
100-
print!("{}", config::EXAMPLE_CONFIG);
103+
print!("{}", config::EXAMPLE_CONFIG); // TODO: Find a way to use a translated example config
101104
return Ok(());
102105
}
103106

@@ -210,7 +213,7 @@ fn run() -> Result<()> {
210213
runner.execute(Step::System, "System update", || distribution.upgrade(&ctx))?;
211214
}
212215
Err(e) => {
213-
println!("Error detecting current distribution: {e}");
216+
println!("{} {e}", t!("Error detecting current distribution:"));
214217
}
215218
}
216219
runner.execute(Step::ConfigUpdate, "config-update", || linux::run_config_update(&ctx))?;
@@ -524,7 +527,7 @@ fn main() {
524527
// The `Debug` implementation of `eyre::Result` prints a multi-line
525528
// error message that includes all the 'causes' added with
526529
// `.with_context(...)` calls.
527-
println!("Error: {error:?}");
530+
println!("{} {error:?}", t!("Error:"));
528531
}
529532
exit(1);
530533
}

src/self_update.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::process::Command;
55

66
use crate::config::Step;
77
use color_eyre::eyre::{bail, Result};
8+
use rust_i18n::t;
89
use self_update_crate::backends::github::Update;
910
use self_update_crate::update::UpdateStatus;
1011

@@ -18,7 +19,7 @@ pub fn self_update(ctx: &ExecutionContext) -> Result<()> {
1819
print_separator("Self update");
1920

2021
if ctx.run_type().dry() {
21-
println!("Would self-update");
22+
println!("{}", t!("Would self-update"));
2223
Ok(())
2324
} else {
2425
let assume_yes = ctx.config().yes(Step::SelfUpdate);
@@ -38,17 +39,17 @@ pub fn self_update(ctx: &ExecutionContext) -> Result<()> {
3839
.update_extended()?;
3940

4041
if let UpdateStatus::Updated(release) = &result {
41-
println!("\nTopgrade upgraded to {}:\n", release.version);
42+
println!("\n{}{}:\n", t!("Topgrade upgraded to "), release.version);
4243
if let Some(body) = &release.body {
4344
println!("{body}");
4445
}
4546
} else {
46-
println!("Topgrade is up-to-date");
47+
println!("{}", t!("Topgrade is up-to-date"));
4748
}
4849

4950
{
5051
if result.updated() {
51-
print_info("Respawning...");
52+
print_info(t!("Respawning..."));
5253
let mut command = Command::new(current_exe?);
5354
command.args(env::args().skip(1)).env("TOPGRADE_NO_SELF_UPGRADE", "");
5455

src/steps/git.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::terminal::print_separator;
2020
use crate::utils::{require, PathExt};
2121
use crate::{error::SkipStep, terminal::print_warning, HOME_DIR};
2222
use etcetera::base_strategy::BaseStrategy;
23+
use rust_i18n::t;
2324

2425
#[cfg(unix)]
2526
use crate::XDG_DIRS;
@@ -296,7 +297,7 @@ impl RepoStep {
296297
let before_revision = get_head_revision(&self.git, &repo);
297298

298299
if ctx.config().verbose() {
299-
println!("{} {}", style("Pulling").cyan().bold(), repo.as_ref().display());
300+
println!("{} {}", style(t!("Pulling")).cyan().bold(), repo.as_ref().display());
300301
}
301302

302303
let mut command = AsyncCommand::new(&self.git);
@@ -322,7 +323,7 @@ impl RepoStep {
322323
.wrap_err_with(|| format!("Failed to pull {}", repo.as_ref().display()));
323324

324325
if result.is_err() {
325-
println!("{} pulling {}", style("Failed").red().bold(), repo.as_ref().display());
326+
println!("{} {} {}", style("Failed").red().bold(), t!("pulling"), repo.as_ref().display());
326327
} else {
327328
let after_revision = get_head_revision(&self.git, repo.as_ref());
328329

src/steps/kakoune.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::terminal::print_separator;
22
use crate::utils::require;
33
use color_eyre::eyre::Result;
4+
use rust_i18n::t;
45

56
use crate::execution_context::ExecutionContext;
67

@@ -17,7 +18,7 @@ pub fn upgrade_kak_plug(ctx: &ExecutionContext) -> Result<()> {
1718
.args(["-ui", "dummy", "-e", UPGRADE_KAK])
1819
.output()?;
1920

20-
println!("Plugins upgraded");
21+
println!("{}", t!("Plugins upgraded"));
2122

2223
Ok(())
2324
}

src/steps/os/unix.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use color_eyre::eyre::Context;
1313
use color_eyre::eyre::Result;
1414
use home;
1515
use ini::Ini;
16+
use rust_i18n::t;
1617
use tracing::debug;
1718

1819
#[cfg(target_os = "linux")]
@@ -719,6 +720,6 @@ pub fn run_maza(ctx: &ExecutionContext) -> Result<()> {
719720
}
720721

721722
pub fn reboot() -> Result<()> {
722-
print!("Rebooting...");
723+
print!("{}", t!("Rebooting..."));
723724
Command::new("sudo").arg("reboot").status_checked()
724725
}

src/steps/powershell.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::path::PathBuf;
44
use std::process::Command;
55

66
use color_eyre::eyre::Result;
7+
use rust_i18n::t;
78

89
use crate::command::CommandExt;
910
use crate::execution_context::ExecutionContext;
@@ -62,9 +63,9 @@ impl Powershell {
6263
}
6364

6465
pub fn update_modules(&self, ctx: &ExecutionContext) -> Result<()> {
65-
let powershell = require_option(self.path.as_ref(), String::from("Powershell is not installed"))?;
66+
let powershell = require_option(self.path.as_ref(), t!("Powershell is not installed").to_string())?;
6667

67-
print_separator("Powershell Modules Update");
68+
print_separator(t!("Powershell Modules Update"));
6869

6970
let mut cmd = vec!["Update-Module"];
7071

@@ -76,7 +77,7 @@ impl Powershell {
7677
cmd.push("-Force")
7778
}
7879

79-
println!("Updating modules...");
80+
println!("{}", t!("Updating modules..."));
8081
ctx.run_type()
8182
.execute(powershell)
8283
// This probably doesn't need `shell_words::join`.

src/steps/tmux.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::{
1616

1717
#[cfg(unix)]
1818
use std::os::unix::process::CommandExt as _;
19+
use rust_i18n::t;
1920

2021
pub fn run_tpm(ctx: &ExecutionContext) -> Result<()> {
2122
let tpm = HOME_DIR.join(".tmux/plugins/tpm/bin/update_plugins").require()?;
@@ -149,7 +150,7 @@ pub fn run_in_tmux(args: Vec<String>) -> Result<()> {
149150
let err = tmux.build().args(["attach-session", "-t", &session]).exec();
150151
Err(eyre!("{err}")).context("Failed to `execvp(3)` tmux")
151152
} else {
152-
println!("Topgrade launched in a new tmux session");
153+
println!("{}", t!("Topgrade launched in a new tmux session"));
153154
Ok(())
154155
}
155156
}

src/steps/vim.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::{
1515
io::{self, Write},
1616
process::Command,
1717
};
18+
use rust_i18n::t;
1819
use tracing::debug;
1920

2021
const UPGRADE_VIM: &str = include_str!("upgrade.vim");
@@ -64,7 +65,7 @@ fn upgrade(command: &mut Executor, ctx: &ExecutionContext) -> Result<()> {
6465
if !status.success() {
6566
return Err(TopgradeError::ProcessFailed(command.get_program(), status).into());
6667
} else {
67-
println!("Plugins upgraded")
68+
println!("{}", t!("Plugins upgraded"))
6869
}
6970
}
7071

src/terminal.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use color_eyre::eyre::Context;
1111
use console::{style, Key, Term};
1212
use lazy_static::lazy_static;
1313
use notify_rust::{Notification, Timeout};
14+
use rust_i18n::t;
1415
use tracing::{debug, error};
1516
#[cfg(windows)]
1617
use which_crate::which;
@@ -224,7 +225,7 @@ impl Terminal {
224225
match self.term.read_key() {
225226
Ok(Key::Char('y')) | Ok(Key::Char('Y')) => break Ok(true),
226227
Ok(Key::Char('s')) | Ok(Key::Char('S')) => {
227-
println!("\n\nDropping you to shell. Fix what you need and then exit the shell.\n");
228+
println!("\n\n{}\n", t!("Dropping you to shell. Fix what you need and then exit the shell."));
228229
if let Err(err) = run_shell().context("Failed to run shell") {
229230
self.term.write_fmt(format_args!("{err:?}\n{prompt_inner}")).ok();
230231
} else {

0 commit comments

Comments
 (0)