Skip to content

Commit

Permalink
rage: Add localized manpages to the Debian package
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Jan 20, 2024
1 parent 066d780 commit 2e7059d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 18 deletions.
1 change: 1 addition & 0 deletions rage/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ to 1.0.0 are beta releases.
- `rage-keygen -y IDENTITY_FILE` to convert identity files to recipients.
- Elvish completions to the Debian package. These are not automatically
discovered; Elvish users will need to manually import them.
- Localized manpages to the Debian package.

### Changed
- MSRV is now 1.65.0.
Expand Down
4 changes: 2 additions & 2 deletions rage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ assets = [

# From the default `/etc/manpath.config` created by the `man-db` package:
# > MANPATH_MAP /usr/bin /usr/share/man
["target/release/manpages/rage*.1.gz", "usr/share/man/man1/", "644"],
["target/release/manpages/**/rage*.1.gz", "usr/share/man/", "644"],

["../README.md", "usr/share/doc/rage/README.md", "644"],
]
Expand Down Expand Up @@ -125,7 +125,7 @@ clap = { workspace = true, features = ["string", "unstable-styles"] }
clap_complete = "4"
clap_mangen = "0.2"
flate2 = "1"
i18n-embed = { workspace = true, features = ["desktop-requester"] }
i18n-embed.workspace = true
i18n-embed-fl.workspace = true
lazy_static.workspace = true
rust-embed.workspace = true
Expand Down
38 changes: 33 additions & 5 deletions rage/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use clap_mangen::{
Man,
};
use flate2::{write::GzEncoder, Compression};
use i18n_embed::unic_langid::LanguageIdentifier;

mod i18n {
include!("src/bin/rage/i18n.rs");
Expand Down Expand Up @@ -243,8 +244,6 @@ impl Cli {
}

fn main() -> io::Result<()> {
i18n::load_languages();

// `OUT_DIR` is "intentionally opaque as it is only intended for `rustc` interaction"
// (https://github.com/rust-lang/cargo/issues/9858). Peek into the black box and use
// it to figure out where the target directory is.
Expand All @@ -257,9 +256,38 @@ fn main() -> io::Result<()> {
.to_path_buf(),
};

let mut cli = Cli::build();
cli.generate_completions(&out_dir.join("completions"))?;
cli.generate_manpages(&out_dir.join("manpages"))?;
// Generate the completions in English, because these aren't easily localizable.
i18n::load_languages(&[]);
Cli::build().generate_completions(&out_dir.join("completions"))?;

// Generate manpages for all supported languages.
let manpage_dir = out_dir.join("manpages");
for lang_dir in fs::read_dir("./i18n")? {
let lang_dir = lang_dir?.file_name();
let lang: LanguageIdentifier = lang_dir
.to_str()
.expect("should be valid Unicode")
.parse()
.expect("should be valid language identifier");

// Render the manpages into the correct folder structure, so that local checks can
// be performed with `man -M target/debug/manpages BINARY_NAME`.
let mut out_dir = if lang.language.as_str() == "en" {
manpage_dir.clone()
} else {
let mut lang_str = lang.language.as_str().to_owned();
if let Some(region) = lang.region {
// Locales for manpages use the POSIX format with underscores.
lang_str += "_";
lang_str += region.as_str();
}
manpage_dir.join(lang_str)
};
out_dir.push("man1");

i18n::load_languages(&[lang]);
Cli::build().generate_manpages(&out_dir)?;
}

Ok(())
}
6 changes: 4 additions & 2 deletions rage/src/bin/rage-keygen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use age::{cli_common::file_io, secrecy::ExposeSecret};
use clap::Parser;
use i18n_embed::DesktopLanguageRequester;

use std::io::{self, Write};

Expand Down Expand Up @@ -30,8 +31,9 @@ fn main() -> Result<(), error::Error> {
.parse_default_env()
.init();

let requested_languages = i18n::load_languages();
age::localizer().select(&requested_languages).unwrap();
let supported_languages =
i18n::load_languages(&DesktopLanguageRequester::requested_languages());
age::localizer().select(&supported_languages).unwrap();

let opts = cli::AgeOptions::parse();

Expand Down
6 changes: 4 additions & 2 deletions rage/src/bin/rage-mount/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use age::{
use clap::{CommandFactory, Parser};
use fuse_mt::FilesystemMT;
use fuser::MountOption;
use i18n_embed::DesktopLanguageRequester;
use log::info;

use std::fmt;
Expand Down Expand Up @@ -176,8 +177,9 @@ fn main() -> Result<(), Error> {
.parse_default_env()
.init();

let requested_languages = i18n::load_languages();
age::localizer().select(&requested_languages).unwrap();
let supported_languages =
i18n::load_languages(&DesktopLanguageRequester::requested_languages());
age::localizer().select(&supported_languages).unwrap();

if console::user_attended() && args().len() == 1 {
cli::AgeMountOptions::command().print_help()?;
Expand Down
17 changes: 12 additions & 5 deletions rage/src/bin/rage/i18n.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
unic_langid::LanguageIdentifier,
DesktopLanguageRequester,
};
use lazy_static::lazy_static;
use rust_embed::RustEmbed;
Expand All @@ -14,11 +13,19 @@ 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();
/// Selects the most suitable available language in order of preference by
/// `requested_languages`, and loads it using the `rage` [`LANGUAGE_LOADER`] from the
/// languages available in `rage/i18n/`.
///
/// Returns the available languages that were negotiated as being the most suitable to be
/// selected, and were loaded by [`i18n_embed::select`].
pub(crate) fn load_languages(
requested_languages: &[LanguageIdentifier],
) -> Vec<LanguageIdentifier> {
let supported_languages =
i18n_embed::select(&*LANGUAGE_LOADER, &Localizations, &requested_languages).unwrap();

Check failure on line 26 in rage/src/bin/rage/i18n.rs

View workflow job for this annotation

GitHub Actions / Clippy (MSRV)

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> rage/src/bin/rage/i18n.rs:26:63 | 26 | i18n_embed::select(&*LANGUAGE_LOADER, &Localizations, &requested_languages).unwrap(); | ^^^^^^^^^^^^^^^^^^^^ help: change this to: `requested_languages` | = note: `-D clippy::needless-borrow` implied by `-D warnings` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
// 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
supported_languages
}
6 changes: 4 additions & 2 deletions rage/src/bin/rage/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use age::{
Identity, IdentityFile, IdentityFileEntry, Recipient,
};
use clap::{CommandFactory, Parser};
use i18n_embed::DesktopLanguageRequester;

use std::fs::File;
use std::io::{self, BufRead, BufReader};
Expand Down Expand Up @@ -521,8 +522,9 @@ fn main() -> Result<(), error::Error> {
.parse_default_env()
.init();

let requested_languages = i18n::load_languages();
age::localizer().select(&requested_languages).unwrap();
let supported_languages =
i18n::load_languages(&DesktopLanguageRequester::requested_languages());
age::localizer().select(&supported_languages).unwrap();

// If you are piping input with no other args, this will not allow
// it.
Expand Down

0 comments on commit 2e7059d

Please sign in to comment.