-
-
Notifications
You must be signed in to change notification settings - Fork 631
[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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. part of me would love to force en-GB 😏 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" |
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,4 @@ pub mod api; | |
pub mod record; | ||
pub mod shell; | ||
pub mod utils; | ||
pub mod i18n; |
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! 🙏 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
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),*) | ||
)) | ||
} | ||
} | ||
|
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" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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:#}' " | ||
"{} ", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!( | ||
"unrecognized subcommand '%{subcommand}'", | ||
subcommand = format!("{invalid}{subcommand}{invalid:#}") | ||
) | ||
); | ||
let _ = write!( | ||
output, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
unrecognized-subcommand-subcommand = | ||
unrecognised subcommand '{ $subcommand }' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
unrecognized-subcommand-subcommand = | ||
unrecognized subcommand '{ $subcommand }' |
There was a problem hiding this comment.
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)