i18n: extract include_i18n macro use into method for consistent compilation order

This commit is contained in:
Vincent Breitmoser 2022-02-26 17:25:09 +01:00
parent e4718d7598
commit a9c4786d14
3 changed files with 13 additions and 8 deletions

View File

@ -292,8 +292,9 @@ pub fn pop_mail(dir: &Path) -> Result<Option<String>> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::web::get_i18n;
use super::*; use super::*;
use gettext_macros::include_i18n;
use std::str::FromStr; use std::str::FromStr;
use tempfile::{tempdir, TempDir}; use tempfile::{tempdir, TempDir};
@ -302,7 +303,7 @@ mod test {
const TO: &str = "recipient@example.org"; const TO: &str = "recipient@example.org";
fn configure_i18n(lang: &'static str) -> I18n { fn configure_i18n(lang: &'static str) -> I18n {
let langs = include_i18n!(); let langs = get_i18n();
let catalog = langs let catalog = langs
.clone() .clone()
.into_iter() .into_iter()

View File

@ -3,9 +3,8 @@ use std::path::{Path, PathBuf};
use handlebars::Handlebars; use handlebars::Handlebars;
use gettext_macros::include_i18n;
use crate::i18n::I18NHelper; use crate::i18n::I18NHelper;
use crate::web::get_i18n;
use crate::Result; use crate::Result;
#[derive(Debug)] #[derive(Debug)]
@ -55,7 +54,7 @@ fn load_localized_template_names(
pub fn load_handlebars(template_dir: &Path) -> Result<Handlebars<'static>> { pub fn load_handlebars(template_dir: &Path) -> Result<Handlebars<'static>> {
let mut handlebars = Handlebars::new(); let mut handlebars = Handlebars::new();
let i18ns = include_i18n!(); let i18ns = get_i18n();
let i18n_helper = I18NHelper::new(i18ns); let i18n_helper = I18NHelper::new(i18ns);
handlebars.register_helper("text", Box::new(i18n_helper)); handlebars.register_helper("text", Box::new(i18n_helper));

View File

@ -396,6 +396,12 @@ pub fn serve() -> Result<rocket::Rocket<rocket::Build>> {
compile_i18n!(); 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( fn rocket_factory(
mut rocket: rocket::Rocket<rocket::Build>, mut rocket: rocket::Rocket<rocket::Build>,
) -> Result<rocket::Rocket<rocket::Build>> { ) -> Result<rocket::Rocket<rocket::Build>> {
@ -467,14 +473,14 @@ fn rocket_factory(
rocket = rocket rocket = rocket
.attach(Template::custom(|engines: &mut Engines| { .attach(Template::custom(|engines: &mut Engines| {
let i18ns = include_i18n!(); let i18ns = get_i18n();
let i18n_helper = I18NHelper::new(i18ns); let i18n_helper = I18NHelper::new(i18ns);
engines engines
.handlebars .handlebars
.register_helper("text", Box::new(i18n_helper)); .register_helper("text", Box::new(i18n_helper));
})) }))
.attach(maintenance_mode) .attach(maintenance_mode)
.manage(include_i18n!()) .manage(get_i18n())
.manage(hagrid_state) .manage(hagrid_state)
.manage(stateless_token_service) .manage(stateless_token_service)
.manage(stateful_token_service) .manage(stateful_token_service)
@ -594,7 +600,6 @@ pub mod tests {
use mail::pop_mail; use mail::pop_mail;
use super::*; use super::*;
use crate::database::*;
/// Fake base URI to use in tests. /// Fake base URI to use in tests.
const BASE_URI: &str = "http://local.connection"; const BASE_URI: &str = "http://local.connection";