From a4c7d610a3f93bfc7ac0cb3cc9c2d070f1dd1663 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 24 Oct 2020 19:53:53 +0500 Subject: [PATCH] Add helper "translate" --- Cargo.lock | 1 + Cargo.toml | 1 + src/i18n.rs | 2 ++ src/i18n/handlebars_helpers.rs | 44 ++++++++++++++++++++++++++++++++++ src/web.rs | 16 +++++++++++-- templates/home/index.html.hbs | 6 +++-- 6 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/i18n/handlebars_helpers.rs diff --git a/Cargo.lock b/Cargo.lock index a98bce2..65bc4a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -309,6 +309,7 @@ dependencies = [ "diesel", "dotenv", "fluent-bundle", + "handlebars", "r2d2", "regex", "rocket", diff --git a/Cargo.toml b/Cargo.toml index 9ada3dc..3267a18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ publish = true bcrypt = "0.8.2" dotenv = "0.15.0" fluent-bundle = "0.13.1" +handlebars = "1.1.0" r2d2 = "0.8.9" regex = "1.4.1" rocket_csrf = "0.2.0" diff --git a/src/i18n.rs b/src/i18n.rs index 2eb479f..58ddd60 100644 --- a/src/i18n.rs +++ b/src/i18n.rs @@ -1,3 +1,5 @@ +pub mod handlebars_helpers; + use std::collections::HashMap; use fluent_bundle::{FluentError, FluentResource}; diff --git a/src/i18n/handlebars_helpers.rs b/src/i18n/handlebars_helpers.rs new file mode 100644 index 0000000..606c767 --- /dev/null +++ b/src/i18n/handlebars_helpers.rs @@ -0,0 +1,44 @@ +use super::I18n; + +use handlebars::{ + Context, + Handlebars, + Helper, + HelperDef, + HelperResult, + Output, + RenderContext, + RenderError, +}; + +pub fn translate(i18n: std::sync::Arc) -> Box { + Box::new(move | + helper: &Helper, + _: &Handlebars, + _: &Context, + _: &mut RenderContext, + output: &mut dyn Output, + | -> HelperResult { + let locale = helper.param(0) + .ok_or(RenderError::new("expected locale param"))? + .value().as_str() + .ok_or(RenderError::new("expected locale param"))?; + + let key = helper.param(1) + .ok_or(RenderError::new("expected key param"))? + .value().as_str() + .ok_or(RenderError::new("expected key param"))?; + + let i18n = i18n.clone(); + + let l10n = i18n.l10n(locale).ok() + .ok_or(RenderError::new("unknown locale"))?; + + let translated = l10n.translate(key).ok() + .ok_or(RenderError::new("translation error"))?; + + output.write(&translated)?; + + Ok(()) + }) +} diff --git a/src/web.rs b/src/web.rs index fefa328..7b0cb24 100644 --- a/src/web.rs +++ b/src/web.rs @@ -1,6 +1,6 @@ use crate::config; use crate::database; -use crate::i18n::I18n; +use crate::i18n::{self, I18n}; use crate::routes; use rocket_contrib::serve::{Options as ServeOptions, StaticFiles}; @@ -17,12 +17,24 @@ pub fn rocket(config: &config::Config) -> Result { Ok(i18n) => i18n, }; + let i18n_arc = match I18n::new(&locales_path, &["en", "ru"]) { + Err(_) => return Err(()), + Ok(i18n) => std::sync::Arc::new(i18n), + }; + let result = rocket::custom(rocket_config) .manage(i18n) .manage(database::create_db_pool(config)) .attach(rocket_csrf::Fairing::new()) - .attach(Template::custom(|engines| { + .attach(Template::custom(move |engines| { engines.handlebars.set_strict_mode(true); + + let i18n_arc = i18n_arc.clone(); + + engines.handlebars.register_helper( + "translate", + i18n::handlebars_helpers::translate(i18n_arc), + ); })) .mount("/", routes::routes()) .mount("/", StaticFiles::new(public_path, ServeOptions::None)); diff --git a/templates/home/index.html.hbs b/templates/home/index.html.hbs index 7c62257..1aa95fd 100644 --- a/templates/home/index.html.hbs +++ b/templates/home/index.html.hbs @@ -1,6 +1,8 @@
-

{{ i18n_fedihub }}

-

{{ i18n_federated_services_without_censorship }}

+

{{ translate "en" "fedihub" }}

+

+ {{ translate "en" "federated-services-without-censorship" }} +