rage: Move i18n code into shared module

This commit is contained in:
Jack Grigg 2024-01-09 03:48:25 +00:00
parent 8303080cda
commit 3f7ab2fe3c
4 changed files with 40 additions and 58 deletions

View file

@ -2,33 +2,24 @@
use age::{cli_common::file_io, secrecy::ExposeSecret};
use clap::Parser;
use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
DesktopLanguageRequester,
};
use lazy_static::lazy_static;
use rust_embed::RustEmbed;
use std::io::Write;
mod cli;
mod error;
#[derive(RustEmbed)]
#[folder = "i18n"]
struct Localizations;
lazy_static! {
static ref LANGUAGE_LOADER: FluentLanguageLoader = fluent_language_loader!();
mod i18n {
include!("../rage/i18n.rs");
}
#[macro_export]
macro_rules! fl {
($message_id:literal) => {{
i18n_embed_fl::fl!($crate::LANGUAGE_LOADER, $message_id)
i18n_embed_fl::fl!($crate::i18n::LANGUAGE_LOADER, $message_id)
}};
($message_id:literal, $($args:expr),* $(,)?) => {{
i18n_embed_fl::fl!($crate::LANGUAGE_LOADER, $message_id, $($args), *)
i18n_embed_fl::fl!($crate::i18n::LANGUAGE_LOADER, $message_id, $($args), *)
}};
}
@ -39,12 +30,8 @@ fn main() -> Result<(), error::Error> {
.parse_default_env()
.init();
let requested_languages = DesktopLanguageRequester::requested_languages();
i18n_embed::select(&*LANGUAGE_LOADER, &Localizations, &requested_languages).unwrap();
let requested_languages = i18n::load_languages();
age::localizer().select(&requested_languages).unwrap();
// Unfortunately the common Windows terminals don't support Unicode Directionality
// Isolation Marks, so we disable them for now.
LANGUAGE_LOADER.set_use_isolating(false);
let opts = cli::AgeOptions::parse();

View file

@ -8,13 +8,7 @@ use age::{
use clap::{CommandFactory, Parser};
use fuse_mt::FilesystemMT;
use fuser::MountOption;
use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
DesktopLanguageRequester,
};
use lazy_static::lazy_static;
use log::info;
use rust_embed::RustEmbed;
use std::fmt;
use std::fs::File;
@ -25,22 +19,18 @@ mod cli;
mod tar;
mod zip;
#[derive(RustEmbed)]
#[folder = "i18n"]
struct Localizations;
lazy_static! {
static ref LANGUAGE_LOADER: FluentLanguageLoader = fluent_language_loader!();
mod i18n {
include!("../rage/i18n.rs");
}
#[macro_export]
macro_rules! fl {
($message_id:literal) => {{
i18n_embed_fl::fl!($crate::LANGUAGE_LOADER, $message_id)
i18n_embed_fl::fl!($crate::i18n::LANGUAGE_LOADER, $message_id)
}};
($message_id:literal, $($args:expr),* $(,)?) => {{
i18n_embed_fl::fl!($crate::LANGUAGE_LOADER, $message_id, $($args), *)
i18n_embed_fl::fl!($crate::i18n::LANGUAGE_LOADER, $message_id, $($args), *)
}};
}
@ -186,12 +176,8 @@ fn main() -> Result<(), Error> {
.parse_default_env()
.init();
let requested_languages = DesktopLanguageRequester::requested_languages();
i18n_embed::select(&*LANGUAGE_LOADER, &Localizations, &requested_languages).unwrap();
let requested_languages = i18n::load_languages();
age::localizer().select(&requested_languages).unwrap();
// Unfortunately the common Windows terminals don't support Unicode Directionality
// Isolation Marks, so we disable them for now.
LANGUAGE_LOADER.set_use_isolating(false);
if console::user_attended() && args().len() == 1 {
cli::AgeMountOptions::command().print_help()?;

24
rage/src/bin/rage/i18n.rs Normal file
View file

@ -0,0 +1,24 @@
use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
unic_langid::LanguageIdentifier,
DesktopLanguageRequester,
};
use lazy_static::lazy_static;
use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[folder = "i18n"]
struct Localizations;
lazy_static! {
pub(crate) static ref LANGUAGE_LOADER: FluentLanguageLoader = fluent_language_loader!();
}
pub(crate) fn load_languages() -> Vec<LanguageIdentifier> {
let requested_languages = DesktopLanguageRequester::requested_languages();
i18n_embed::select(&*LANGUAGE_LOADER, &Localizations, &requested_languages).unwrap();
// Unfortunately the common Windows terminals don't support Unicode Directionality
// Isolation Marks, so we disable them for now.
LANGUAGE_LOADER.set_use_isolating(false);
requested_languages
}

View file

@ -10,12 +10,7 @@ use age::{
Identity, IdentityFile, IdentityFileEntry, Recipient,
};
use clap::{CommandFactory, Parser};
use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
DesktopLanguageRequester,
};
use lazy_static::lazy_static;
use rust_embed::RustEmbed;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::path::Path;
@ -25,22 +20,16 @@ use cli::AgeOptions;
mod error;
#[derive(RustEmbed)]
#[folder = "i18n"]
struct Localizations;
lazy_static! {
static ref LANGUAGE_LOADER: FluentLanguageLoader = fluent_language_loader!();
}
mod i18n;
#[macro_export]
macro_rules! fl {
($message_id:literal) => {{
i18n_embed_fl::fl!($crate::LANGUAGE_LOADER, $message_id)
i18n_embed_fl::fl!($crate::i18n::LANGUAGE_LOADER, $message_id)
}};
($message_id:literal, $($args:expr),* $(,)?) => {{
i18n_embed_fl::fl!($crate::LANGUAGE_LOADER, $message_id, $($args), *)
i18n_embed_fl::fl!($crate::i18n::LANGUAGE_LOADER, $message_id, $($args), *)
}};
}
@ -532,12 +521,8 @@ fn main() -> Result<(), error::Error> {
.parse_default_env()
.init();
let requested_languages = DesktopLanguageRequester::requested_languages();
i18n_embed::select(&*LANGUAGE_LOADER, &Localizations, &requested_languages).unwrap();
let requested_languages = i18n::load_languages();
age::localizer().select(&requested_languages).unwrap();
// Unfortunately the common Windows terminals don't support Unicode Directionality
// Isolation Marks, so we disable them for now.
LANGUAGE_LOADER.set_use_isolating(false);
// If you are piping input with no other args, this will not allow
// it.