From a9c4786d14ed8d416d8e167278f48d24d920732d Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 26 Feb 2022 17:25:09 +0100 Subject: [PATCH] i18n: extract include_i18n macro use into method for consistent compilation order --- src/mail.rs | 5 +++-- src/template_helpers.rs | 5 ++--- src/web/mod.rs | 11 ++++++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/mail.rs b/src/mail.rs index 7b48989..94fc9b7 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -292,8 +292,9 @@ pub fn pop_mail(dir: &Path) -> Result> { #[cfg(test)] mod test { + use crate::web::get_i18n; + use super::*; - use gettext_macros::include_i18n; use std::str::FromStr; use tempfile::{tempdir, TempDir}; @@ -302,7 +303,7 @@ mod test { const TO: &str = "recipient@example.org"; fn configure_i18n(lang: &'static str) -> I18n { - let langs = include_i18n!(); + let langs = get_i18n(); let catalog = langs .clone() .into_iter() diff --git a/src/template_helpers.rs b/src/template_helpers.rs index 06dc3ae..717ef5d 100644 --- a/src/template_helpers.rs +++ b/src/template_helpers.rs @@ -3,9 +3,8 @@ use std::path::{Path, PathBuf}; use handlebars::Handlebars; -use gettext_macros::include_i18n; - use crate::i18n::I18NHelper; +use crate::web::get_i18n; use crate::Result; #[derive(Debug)] @@ -55,7 +54,7 @@ fn load_localized_template_names( pub fn load_handlebars(template_dir: &Path) -> Result> { let mut handlebars = Handlebars::new(); - let i18ns = include_i18n!(); + let i18ns = get_i18n(); let i18n_helper = I18NHelper::new(i18ns); handlebars.register_helper("text", Box::new(i18n_helper)); diff --git a/src/web/mod.rs b/src/web/mod.rs index de4b81c..2f76ab6 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -396,6 +396,12 @@ pub fn serve() -> Result> { compile_i18n!(); +// The include_i18n macro must be called after compile_i18n, which must be called after i18n macros +// *in compilation order*. We use a helper function here to make this order consistent. +pub fn get_i18n() -> Vec<(&'static str, gettext::Catalog)> { + include_i18n!() +} + fn rocket_factory( mut rocket: rocket::Rocket, ) -> Result> { @@ -467,14 +473,14 @@ fn rocket_factory( rocket = rocket .attach(Template::custom(|engines: &mut Engines| { - let i18ns = include_i18n!(); + let i18ns = get_i18n(); let i18n_helper = I18NHelper::new(i18ns); engines .handlebars .register_helper("text", Box::new(i18n_helper)); })) .attach(maintenance_mode) - .manage(include_i18n!()) + .manage(get_i18n()) .manage(hagrid_state) .manage(stateless_token_service) .manage(stateful_token_service) @@ -594,7 +600,6 @@ pub mod tests { use mail::pop_mail; use super::*; - use crate::database::*; /// Fake base URI to use in tests. const BASE_URI: &str = "http://local.connection";