From 3f7ab2fe3c0de729f87aca827de746198f1b86b7 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 9 Jan 2024 03:48:25 +0000 Subject: [PATCH] rage: Move i18n code into shared module --- rage/src/bin/rage-keygen/main.rs | 25 ++++++------------------- rage/src/bin/rage-mount/main.rs | 24 +++++------------------- rage/src/bin/rage/i18n.rs | 24 ++++++++++++++++++++++++ rage/src/bin/rage/main.rs | 25 +++++-------------------- 4 files changed, 40 insertions(+), 58 deletions(-) create mode 100644 rage/src/bin/rage/i18n.rs diff --git a/rage/src/bin/rage-keygen/main.rs b/rage/src/bin/rage-keygen/main.rs index d374a3b..a5e9cf2 100644 --- a/rage/src/bin/rage-keygen/main.rs +++ b/rage/src/bin/rage-keygen/main.rs @@ -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(); diff --git a/rage/src/bin/rage-mount/main.rs b/rage/src/bin/rage-mount/main.rs index 4e7485b..8b6393f 100644 --- a/rage/src/bin/rage-mount/main.rs +++ b/rage/src/bin/rage-mount/main.rs @@ -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()?; diff --git a/rage/src/bin/rage/i18n.rs b/rage/src/bin/rage/i18n.rs new file mode 100644 index 0000000..27abf2a --- /dev/null +++ b/rage/src/bin/rage/i18n.rs @@ -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 { + 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 +} diff --git a/rage/src/bin/rage/main.rs b/rage/src/bin/rage/main.rs index a389c58..fecf4d7 100644 --- a/rage/src/bin/rage/main.rs +++ b/rage/src/bin/rage/main.rs @@ -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.