forked from
smokesignal.events/smokesignal
i18n+filtering fork - fluent-templates v2
1use axum::response::IntoResponse;
2use axum_template::{RenderHtml, TemplateEngine};
3use minijinja::context as template_context;
4
5pub fn render_alert<E: TemplateEngine, S: Into<String>>(
6 engine: E,
7 language: &str,
8 message: S,
9 default_context: minijinja::Value,
10) -> impl IntoResponse {
11 RenderHtml(
12 format!("prompt.{}.html", language),
13 engine,
14 template_context! { ..default_context, ..template_context! {
15 message => message.into(),
16 }},
17 )
18}
19
20#[cfg(feature = "reload")]
21pub mod reload_env {
22 use std::path::PathBuf;
23
24 use minijinja::{path_loader, Environment};
25 use minijinja_autoreload::AutoReloader;
26 use crate::i18n::{register_i18n_functions, I18nTemplateContext};
27
28 pub fn build_env(http_external: &str, version: &str) -> AutoReloader {
29 let http_external = http_external.to_string();
30 let version = version.to_string();
31 AutoReloader::new(move |notifier| {
32 let template_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("templates");
33 let mut env = Environment::new();
34 env.set_trim_blocks(true);
35 env.set_lstrip_blocks(true);
36 env.add_global("base", format!("https://{}", http_external));
37 env.add_global("version", version.clone());
38 env.set_loader(path_loader(&template_path));
39
40 // Register i18n functions with default locale context
41 let i18n_context = I18nTemplateContext::with_default_locales();
42 register_i18n_functions(&mut env, i18n_context);
43
44 notifier.set_fast_reload(true);
45 notifier.watch_path(&template_path, true);
46 Ok(env)
47 })
48 }
49}
50
51#[cfg(feature = "embed")]
52pub mod embed_env {
53 use minijinja::Environment;
54 use crate::i18n::{register_i18n_functions, I18nTemplateContext};
55
56 pub fn build_env(http_external: String, version: String) -> Environment<'static> {
57 let mut env = Environment::new();
58 env.set_trim_blocks(true);
59 env.set_lstrip_blocks(true);
60 env.add_global("base", format!("https://{}", http_external));
61 env.add_global("version", version.clone());
62
63 // Register i18n functions with default locale context
64 let i18n_context = I18nTemplateContext::with_default_locales();
65 register_i18n_functions(&mut env, i18n_context);
66
67 minijinja_embed::load_templates!(&mut env);
68 env
69 }
70}