Skip to content

Commit

Permalink
Merge pull request #435 from str4d/i18n-cleanups
Browse files Browse the repository at this point in the history
i18n cleanups
  • Loading branch information
str4d authored Jan 7, 2024
2 parents 4bac30b + 6039c09 commit e2a75df
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 248 deletions.
2 changes: 2 additions & 0 deletions age/i18n/es-AR/age.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

-age = age
-rage = rage
-ssh-rsa = ssh-rsa
-ssh-ed25519 = ssh-ed25519
## CLI helpers

Expand Down
2 changes: 2 additions & 0 deletions age/i18n/zh-CN/age.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

-age = age
-rage = rage
-ssh-rsa = ssh-rsa
-ssh-ed25519 = ssh-ed25519
## CLI helpers

Expand Down
2 changes: 2 additions & 0 deletions age/i18n/zh-TW/age.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

-age = age
-rage = rage
-ssh-rsa = ssh-rsa
-ssh-ed25519 = ssh-ed25519
## CLI helpers

Expand Down
32 changes: 8 additions & 24 deletions age/src/cli_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::fs::File;
use std::io::{self, BufReader};
use subtle::ConstantTimeEq;

use crate::{fl, identity::IdentityFile, wfl, Callbacks, Identity};
use crate::{fl, identity::IdentityFile, wfl, wlnfl, Callbacks, Identity};

#[cfg(feature = "armor")]
use crate::armor::ArmoredReader;
Expand Down Expand Up @@ -55,37 +55,21 @@ impl fmt::Display for ReadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ReadError::IdentityEncryptedWithoutPassphrase(filename) => {
write!(
wfl!(
f,
"{}",
i18n_embed_fl::fl!(
crate::i18n::LANGUAGE_LOADER,
"err-read-identity-encrypted-without-passphrase",
filename = filename.as_str()
)
"err-read-identity-encrypted-without-passphrase",
filename = filename.as_str(),
)
}
ReadError::IdentityNotFound(filename) => write!(
ReadError::IdentityNotFound(filename) => wfl!(
f,
"{}",
i18n_embed_fl::fl!(
crate::i18n::LANGUAGE_LOADER,
"err-read-identity-not-found",
filename = filename.as_str()
)
"err-read-identity-not-found",
filename = filename.as_str(),
),
ReadError::Io(e) => write!(f, "{}", e),
#[cfg(feature = "plugin")]
ReadError::MissingPlugin { binary_name } => {
writeln!(
f,
"{}",
i18n_embed_fl::fl!(
crate::i18n::LANGUAGE_LOADER,
"err-missing-plugin",
plugin_name = binary_name.as_str()
)
)?;
wlnfl!(f, "err-missing-plugin", plugin_name = binary_name.as_str())?;
wfl!(f, "rec-missing-plugin")
}
#[cfg(feature = "ssh")]
Expand Down
8 changes: 2 additions & 6 deletions age/src/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

use std::{cell::Cell, io};

use i18n_embed_fl::fl;

use crate::{
decryptor::PassphraseDecryptor, Callbacks, DecryptError, Decryptor, EncryptError, IdentityFile,
IdentityFileEntry,
decryptor::PassphraseDecryptor, fl, Callbacks, DecryptError, Decryptor, EncryptError,
IdentityFile, IdentityFileEntry,
};

/// The state of the encrypted age identity.
Expand Down Expand Up @@ -46,7 +44,6 @@ impl<R: io::Read> IdentityState<R> {
max_work_factor,
} => {
let passphrase = match callbacks.request_passphrase(&fl!(
crate::i18n::LANGUAGE_LOADER,
"encrypted-passphrase-prompt",
filename = filename.unwrap_or_default()
)) {
Expand Down Expand Up @@ -169,7 +166,6 @@ impl<R: io::Read, C: Callbacks> Identity<R, C> {
// matched, warn the user.
if requested_passphrase && result.is_none() {
self.callbacks.display_message(&fl!(
crate::i18n::LANGUAGE_LOADER,
"encrypted-warn-no-match",
filename = self.filename.as_deref().unwrap_or_default()
));
Expand Down
57 changes: 14 additions & 43 deletions age/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Error type.

use i18n_embed_fl::fl;
use std::fmt;
use std::io;

Expand Down Expand Up @@ -61,30 +60,22 @@ impl fmt::Display for PluginError {
PluginError::Identity {
binary_name,
message,
} => write!(
} => wfl!(
f,
"{}",
fl!(
crate::i18n::LANGUAGE_LOADER,
"err-plugin-identity",
plugin_name = binary_name.as_str(),
message = message.as_str()
)
"err-plugin-identity",
plugin_name = binary_name.as_str(),
message = message.as_str(),
),
PluginError::Recipient {
binary_name,
recipient,
message,
} => write!(
} => wfl!(
f,
"{}",
fl!(
crate::i18n::LANGUAGE_LOADER,
"err-plugin-recipient",
plugin_name = binary_name.as_str(),
recipient = recipient.as_str(),
message = message.as_str()
)
"err-plugin-recipient",
plugin_name = binary_name.as_str(),
recipient = recipient.as_str(),
message = message.as_str(),
),
PluginError::Other {
kind,
Expand Down Expand Up @@ -153,15 +144,7 @@ impl fmt::Display for EncryptError {
EncryptError::Io(e) => e.fmt(f),
#[cfg(feature = "plugin")]
EncryptError::MissingPlugin { binary_name } => {
writeln!(
f,
"{}",
fl!(
crate::i18n::LANGUAGE_LOADER,
"err-missing-plugin",
plugin_name = binary_name.as_str()
)
)?;
wlnfl!(f, "err-missing-plugin", plugin_name = binary_name.as_str())?;
wfl!(f, "rec-missing-plugin")
}
#[cfg(feature = "plugin")]
Expand Down Expand Up @@ -258,14 +241,10 @@ impl fmt::Display for DecryptError {
DecryptError::DecryptionFailed => wfl!(f, "err-decryption-failed"),
DecryptError::ExcessiveWork { required, target } => {
wlnfl!(f, "err-excessive-work")?;
write!(
wfl!(
f,
"{}",
fl!(
crate::i18n::LANGUAGE_LOADER,
"rec-excessive-work",
duration = (1 << (required - target))
)
"rec-excessive-work",
duration = (1 << (required - target)),
)
}
DecryptError::InvalidHeader => wfl!(f, "err-header-invalid"),
Expand All @@ -274,15 +253,7 @@ impl fmt::Display for DecryptError {
DecryptError::KeyDecryptionFailed => wfl!(f, "err-key-decryption"),
#[cfg(feature = "plugin")]
DecryptError::MissingPlugin { binary_name } => {
writeln!(
f,
"{}",
fl!(
crate::i18n::LANGUAGE_LOADER,
"err-missing-plugin",
plugin_name = binary_name.as_str()
)
)?;
wlnfl!(f, "err-missing-plugin", plugin_name = binary_name.as_str())?;
wfl!(f, "rec-missing-plugin")
}
DecryptError::NoMatchingKeys => wfl!(f, "err-no-matching-keys"),
Expand Down
20 changes: 15 additions & 5 deletions age/src/i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ use rust_embed::RustEmbed;

#[derive(RustEmbed)]
#[folder = "i18n"]
struct Translations;

const TRANSLATIONS: Translations = Translations {};
struct Localizations;

lazy_static! {
pub(crate) static ref LANGUAGE_LOADER: FluentLanguageLoader = {
let language_loader = fluent_language_loader!();
// Ensure that the fallback language is always loaded, even if the library user
// doesn't call `localizer().select(languages)`.
let fallback: LanguageIdentifier = "en-US".parse().unwrap();
language_loader.load_languages(&TRANSLATIONS, &[&fallback]).unwrap();
language_loader.load_languages(&Localizations, &[&fallback]).unwrap();
language_loader
};
}
Expand All @@ -30,6 +28,10 @@ macro_rules! fl {
($message_id:literal) => {{
i18n_embed_fl::fl!($crate::i18n::LANGUAGE_LOADER, $message_id)
}};

($message_id:literal, $($args:expr),* $(,)?) => {{
i18n_embed_fl::fl!($crate::i18n::LANGUAGE_LOADER, $message_id, $($args), *)
}};
}

/// age-localized version of the write! macro.
Expand All @@ -39,6 +41,10 @@ macro_rules! wfl {
($f:ident, $message_id:literal) => {
write!($f, "{}", $crate::fl!($message_id))
};

($f:ident, $message_id:literal, $($args:expr),* $(,)?) => {
write!($f, "{}", $crate::fl!($message_id, $($args), *))
};
}

/// age-localized version of the writeln! macro.
Expand All @@ -48,6 +54,10 @@ macro_rules! wlnfl {
($f:ident, $message_id:literal) => {
writeln!($f, "{}", $crate::fl!($message_id))
};

($f:ident, $message_id:literal, $($args:expr),* $(,)?) => {
writeln!($f, "{}", $crate::fl!($message_id, $($args), *))
};
}

/// Returns the [`Localizer`] to be used for localizing this library.
Expand All @@ -65,5 +75,5 @@ macro_rules! wlnfl {
/// age::localizer().select(&requested_languages).unwrap();
/// ```
pub fn localizer() -> Box<dyn Localizer> {
Box::from(DefaultLocalizer::new(&*LANGUAGE_LOADER, &TRANSLATIONS))
Box::from(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations))
}
3 changes: 1 addition & 2 deletions age/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use age_core::{
};
use base64::{prelude::BASE64_STANDARD_NO_PAD, Engine};
use bech32::Variant;
use i18n_embed_fl::fl;

use std::borrow::Borrow;
use std::fmt;
Expand All @@ -22,6 +21,7 @@ use std::time::{Duration, SystemTime};

use crate::{
error::{DecryptError, EncryptError, PluginError},
fl,
util::parse_bech32,
Callbacks,
};
Expand Down Expand Up @@ -68,7 +68,6 @@ impl SlowPluginGuard {
match SystemTime::now().duration_since(start) {
Ok(end) if end >= TEN_SECONDS => {
callbacks.display_message(&fl!(
crate::i18n::LANGUAGE_LOADER,
"plugin-waiting-on-binary",
binary_name = plugin_binary_name,
));
Expand Down
51 changes: 14 additions & 37 deletions age/src/ssh/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use age_core::{
secrecy::{ExposeSecret, Secret},
};
use base64::prelude::BASE64_STANDARD;
use i18n_embed_fl::fl;
use nom::{
branch::alt,
bytes::streaming::{is_not, tag},
Expand All @@ -27,8 +26,9 @@ use super::{
};
use crate::{
error::DecryptError,
fl,
util::read::{base64_arg, wrapped_str_while_encoded},
Callbacks,
wlnfl, Callbacks,
};

/// An SSH private key for decrypting an age file.
Expand Down Expand Up @@ -144,53 +144,31 @@ impl UnsupportedKey {
/// Prints details about this unsupported key.
pub fn display(&self, f: &mut fmt::Formatter, filename: Option<&str>) -> fmt::Result {
if let Some(name) = filename {
writeln!(
f,
"{}",
fl!(
crate::i18n::LANGUAGE_LOADER,
"ssh-unsupported-key",
name = name
)
)?;
wlnfl!(f, "ssh-unsupported-key", name = name)?;
writeln!(f)?;
}
match self {
UnsupportedKey::EncryptedPem => writeln!(
UnsupportedKey::EncryptedPem => wlnfl!(
f,
"{}",
fl!(
crate::i18n::LANGUAGE_LOADER,
"ssh-insecure-key-format",
change_passphrase = "ssh-keygen -o -p",
gen_new = "ssh-keygen -o"
)
"ssh-insecure-key-format",
change_passphrase = "ssh-keygen -o -p",
gen_new = "ssh-keygen -o",
)?,
UnsupportedKey::EncryptedSsh(cipher) => {
let new_issue = format!(
"https://github.com/str4d/rage/issues/new?title=Support%20OpenSSH%20key%20encryption%20cipher%20{}",
cipher,
);
writeln!(
wlnfl!(
f,
"{}",
fl!(
crate::i18n::LANGUAGE_LOADER,
"ssh-unsupported-cipher",
cipher = cipher.as_str(),
new_issue = new_issue.as_str()
)
"ssh-unsupported-cipher",
cipher = cipher.as_str(),
new_issue = new_issue.as_str(),
)?;
}
UnsupportedKey::Type(key_type) => writeln!(
f,
"{}",
fl!(
crate::i18n::LANGUAGE_LOADER,
"ssh-unsupported-key-type",
key_type = key_type.as_str(),
)
)?,
UnsupportedKey::Type(key_type) => {
wlnfl!(f, "ssh-unsupported-key-type", key_type = key_type.as_str())?
}
}
Ok(())
}
Expand Down Expand Up @@ -289,7 +267,6 @@ impl<C: Callbacks> crate::Identity for DecryptableIdentity<C> {
Identity::Unencrypted(key) => key.unwrap_stanza(stanza),
Identity::Encrypted(enc) => {
let passphrase = self.callbacks.request_passphrase(&fl!(
crate::i18n::LANGUAGE_LOADER,
"ssh-passphrase-prompt",
filename = enc.filename.as_deref().unwrap_or_default()
))?;
Expand Down
Loading

0 comments on commit e2a75df

Please sign in to comment.