Skip to content

[Project Fluent] Basic internationalization support (would close PR #2553) #2674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
763 changes: 757 additions & 6 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion crates/atuin-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ edition = "2024"
description = "client library for atuin"

rust-version = { workspace = true }
version = { workspace = true }
version = "18.5.0-beta.2"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see below: if it is not easy to address the cargo-i18n issue then I'm fairly sure we can ignore it - either way will restore these before merge)

authors = { workspace = true }
license = { workspace = true }
homepage = { workspace = true }
Expand All @@ -20,6 +20,7 @@ check-update = []

[dependencies]
atuin-common = { path = "../atuin-common", version = "18.5.0-beta.2" }
atuin-macro = { path = "../atuin-macro", version = "18.5.0-beta.2" }

log = { workspace = true }
base64 = { workspace = true }
Expand Down Expand Up @@ -73,6 +74,11 @@ palette = { version = "0.7.5", features = ["serializing"] }
lazy_static = "1.4.0"
strum_macros = "0.26.3"
strum = { version = "0.26.2", features = ["strum_macros"] }
fluent = "0.16.1"
i18n-embed = { version = "0.15.3", features = ["autoreload", "desktop-requester", "filesystem-assets", "fluent", "fluent-syntax", "fluent-system", "gettext-system", "locale_config", "notify", "tr", "walkdir"] }
rust-embed = "8"
i18n-embed-fl = "0.9.3"
cargo-i18n = "0.2.13"

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
Expand Down
10 changes: 9 additions & 1 deletion crates/atuin-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ edition = "2024"
description = "common library for atuin"

rust-version = { workspace = true }
version = { workspace = true }
version = "18.5.0-beta.2" # required to be concrete for cargo i18n
authors = { workspace = true }
license = { workspace = true }
homepage = { workspace = true }
Expand All @@ -13,6 +13,8 @@ repository = { workspace = true }
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
atuin-macro = { path = "../atuin-macro" }

time = { workspace = true }
serde = { workspace = true }
uuid = { workspace = true }
Expand All @@ -25,8 +27,14 @@ directories = { workspace = true }
sysinfo = "0.30.7"
base64 = { workspace = true }
getrandom = "0.2"
sys-locale = "0.3.2"

lazy_static = "1.4.0"
i18n-embed = { version = "0.15.3", features = ["fluent", "fluent-system", "tr", "locale_config", "desktop-requester", "walkdir", "gettext-system", "autoreload", "filesystem-assets", "notify"] }
rust-embed = "8"
i18n-embed-fl = "0.9.3"
slugify = "0.1.0"
paste = "1.0.15"

[dev-dependencies]
pretty_assertions = { workspace = true }
13 changes: 13 additions & 0 deletions crates/atuin-common/i18n.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# (Required) The language identifier of the language used in the
# source code for gettext system, and the primary fallback language
# (for which all strings must be present) when using the fluent
# system.
fallback_language = "en-US"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

part of me would love to force en-GB 😏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be honest, it was en-GB until a commit or two ago, so ... :D

That said, it may be an gentle way to encourage a wide sample, as more people will spot that is falling back to -ise spellings instead of correctly picking up US locales, than the other way around 🤔


# Use the fluent localization system.
[fluent]
domain = "atuin"
# (Required) The path to the assets directory.
# The paths inside the assets directory should be structured like so:
# `assets_dir/{language}/{domain}.ftl`
assets_dir = "../../i18n"
37 changes: 37 additions & 0 deletions crates/atuin-common/src/i18n.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use i18n_embed::{DesktopLanguageRequester, fluent::{
FluentLanguageLoader, fluent_language_loader
}};
use rust_embed::RustEmbed;
pub use i18n_embed_fl::fl;

#[derive(RustEmbed)]
#[folder = "../../i18n"] // path to the compiled localization resources
struct Localizations;

use lazy_static::lazy_static;
pub use atuin_macro::tl;

lazy_static! {
pub static ref LOADER: FluentLanguageLoader = {
// Load languages from central internationalization folder.
let language_loader: FluentLanguageLoader = fluent_language_loader!();
let requested_languages = DesktopLanguageRequester::requested_languages();

let _result = i18n_embed::select(
&language_loader, &Localizations, &requested_languages);
language_loader
};
}

#[macro_export]
macro_rules! t {
($message_id:literal) => {
$crate::i18n::tl!($crate::i18n::fl, $crate::i18n::LOADER, $message_id)
};

($message_id:literal, $($args:expr),*) => {{
$crate::i18n::tl!($crate::i18n::fl, $crate::i18n::LOADER, $message_id, $($args), *)
}};
}

pub use t;
1 change: 1 addition & 0 deletions crates/atuin-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ pub mod api;
pub mod record;
pub mod shell;
pub mod utils;
pub mod i18n;
41 changes: 41 additions & 0 deletions crates/atuin-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[package]
name = "atuin-macro"
edition = "2021"
description = "macro library for atuin"

rust-version = { workspace = true }
version = "18.5.0-beta.2"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: this seems to be a cargo-i18n issue that it cannot accept non-literal versions.

That said, running cargo i18n doesn't actually seem to be necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But might need an upstream change in cargo-i18n to fix :/

authors = { workspace = true }
license = { workspace = true }
homepage = { workspace = true }
repository = { workspace = true }

[lib]
proc-macro = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
time = { workspace = true }
serde = { workspace = true }
uuid = { workspace = true }
typed-builder = { workspace = true }
eyre = { workspace = true }
sqlx = { workspace = true }
semver = { workspace = true }
thiserror = { workspace = true }
directories = { workspace = true }
sysinfo = "0.30.7"
base64 = { workspace = true }
getrandom = "0.2"
sys-locale = "0.3.2"

lazy_static = "1.4.0"
i18n-embed = { version = "0.15.3", features = ["fluent", "fluent-system", "tr", "locale_config", "desktop-requester", "walkdir", "gettext-system", "autoreload", "filesystem-assets", "notify"] }
rust-embed = "8"
i18n-embed-fl = "0.9.3"
slugify = "0.1.0"
paste = "1.0.15"
syn = "2.0.96"
quote = "1.0.38"
proc-macro2 = "1.0.93"
52 changes: 52 additions & 0 deletions crates/atuin-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#![forbid(unsafe_code)]
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn;
use syn::parse::Parser;
use slugify::slugify;

#[proc_macro]
pub fn tl(tokens: TokenStream) -> TokenStream {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've generally been pretty against macros in our codebase but in this case I think it makes sense

obv this is just a draft but for the final version I'd really appreciate it if you could thoroughly document what's going on with the code here, just to make it as approachable as possible for future contributors! 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course - will do! This was the main point against fluent-rs, alongside activity, as their macro (for conceptual reasons) requires slug-strings in the codebase rather than human readable ones, so using it means one of:

  1. a wrapper macro to go from human-readable to slugs
  2. switching all translatable strings to slugs
  3. another idea I had not thought of

2 has the downside of making things like making UI bugs harder to see in reviews, and grepping for error messages harder, as well as more visual change to existing code when making strings translatable, so I thought that was probably too much friction. On the other hand, as mentioned, 1 adds custom magical machinery, a new macro module, and somewhat undermines fluent's own differentiating motivation from gettext (although, of course, it is still possible to use it in the intended way on a case-by-case basis).

However, one other benefit of fluent is that it is a standard and seems to be supported by (for example) Weblate - it might even be possible to get a free instance on their libre-project plan, if desired.

let args = syn::punctuated::Punctuated::<syn::Expr, syn::Token![,]>::parse_terminated.parse(tokens).unwrap();
let mut arg_iter = args.iter();
let fl = arg_iter.next().unwrap();
let loader = arg_iter.next().unwrap();
let message_id: proc_macro2::TokenStream = match arg_iter.next() {
Some(syn::Expr::Group(arg)) => {
match *arg.expr.clone() {
syn::Expr::Lit(arg) => {
let quoted: String = match arg.lit.clone() {
syn::Lit::Str(message_id) => message_id.value(),
_ => panic!("Message ID must be a literal string")
};
let slug = slugify!(quoted.as_str());
quote!(#slug)
},
arg => panic!("Message ID {:?} must be a literal", arg)
}
},
Some(syn::Expr::Lit(arg)) => {
let quoted: String = match arg.lit.clone() {
syn::Lit::Str(message_id) => message_id.value(),
_ => panic!("Message ID must be a literal string")
};
let slug = slugify!(quoted.as_str());
quote!(#slug)
},
arg => panic!("Message ID {:?} must be a literal", arg)
};
let args: Vec<_> = arg_iter.collect();

if args.is_empty() {
TokenStream::from(quote!(
#fl!(#loader, #message_id)
))
} else {
TokenStream::from(quote!(
#fl!(#loader, #message_id, #(#args),*)
))
}
}

5 changes: 4 additions & 1 deletion crates/atuin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "atuin - magical shell history"
readme = "./README.md"

rust-version = { workspace = true }
version = { workspace = true }
version = "18.5.0-beta.2"
authors = { workspace = true }
license = { workspace = true }
homepage = { workspace = true }
Expand Down Expand Up @@ -80,6 +80,9 @@ tracing-subscriber = { workspace = true }
uuid = { workspace = true }
sysinfo = "0.30.7"
regex = "1.10.5"
i18n-embed = { version = "0.15.3", features = ["fluent", "fluent-system", "tr", "locale_config", "desktop-requester", "walkdir", "gettext-system", "autoreload", "filesystem-assets", "notify"] }
rust-embed = "8"
i18n-embed-fl = "0.9.3"

[target.'cfg(any(target_os = "windows", target_os = "macos"))'.dependencies]
arboard = { version = "3.4", optional = true }
Expand Down
14 changes: 14 additions & 0 deletions crates/atuin/i18n.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# (Required) The language identifier of the language used in the
# source code for gettext system, and the primary fallback language
# (for which all strings must be present) when using the fluent
# system.
fallback_language = "en-US"

# Use the fluent localization system.
[fluent]
domain = "atuin"

# (Required) The path to the assets directory.
# The paths inside the assets directory should be structured like so:
# `assets_dir/{language}/{domain}.ftl`
assets_dir = "../../i18n"
6 changes: 5 additions & 1 deletion crates/atuin/src/command/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ fn render_not_found(subcommand: &str, bin: &str) -> StyledStr {
let _ = write!(output, "{error}error:{error:#} ");
let _ = write!(
output,
"unrecognized subcommand '{invalid}{subcommand}{invalid:#}' "
"{} ",
Copy link
Contributor Author

@philtweir philtweir Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an awkward example but maybe helps show where extra steps are required (e.g. a simple non-literal/const string would be wrapped by t!(..) and done)

t!(
"unrecognized subcommand '%{subcommand}'",
subcommand = format!("{invalid}{subcommand}{invalid:#}")
)
);
let _ = write!(
output,
Expand Down
2 changes: 2 additions & 0 deletions crates/atuin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![warn(clippy::pedantic, clippy::nursery)]
#![allow(clippy::use_self, clippy::missing_const_for_fn)] // not 100% reliable
#[macro_use]
extern crate atuin_common;

use clap::Parser;
use eyre::Result;
Expand Down
2 changes: 2 additions & 0 deletions i18n/en-GB/atuin.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
unrecognized-subcommand-subcommand =
unrecognised subcommand '{ $subcommand }'
2 changes: 2 additions & 0 deletions i18n/en-US/atuin.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
unrecognized-subcommand-subcommand =
unrecognized subcommand '{ $subcommand }'
Loading