removed redundancy

This commit is contained in:
Priec
2026-06-21 20:09:57 +02:00
parent db6b609937
commit 8dc153efcc

View File

@@ -24,7 +24,9 @@ impl Initializer for ViewEngineInitializer {
} }
async fn after_routes(&self, router: AxumRouter, _ctx: &AppContext) -> Result<AxumRouter> { async fn after_routes(&self, router: AxumRouter, _ctx: &AppContext) -> Result<AxumRouter> {
let tera_engine = if std::path::Path::new(I18N_DIR).exists() { // Load locales only if present; `t` is registered conditionally below so
// the single post-process closure covers both cases.
let locales = if std::path::Path::new(I18N_DIR).exists() {
let arc = std::sync::Arc::new( let arc = std::sync::Arc::new(
ArcLoader::builder(&I18N_DIR, unic_langid::langid!("sk")) ArcLoader::builder(&I18N_DIR, unic_langid::langid!("sk"))
.shared_resources(Some(&[I18N_SHARED.into()])) .shared_resources(Some(&[I18N_SHARED.into()]))
@@ -33,32 +35,28 @@ impl Initializer for ViewEngineInitializer {
.map_err(|e| Error::string(&e.to_string()))?, .map_err(|e| Error::string(&e.to_string()))?,
); );
info!("locales loaded"); info!("locales loaded");
Some(arc)
engines::TeraView::build()?.post_process(move |tera| {
tera.register_function("t", FluentLoader::new(arc.clone()));
// `csrf_token()`: the in-flight request's CSRF token (bound by
// `shared::csrf::protect`), rendered into `<body hx-headers>`
// and `ui::csrf_field()`. Inlined so its `tera::Error` return is
// inferred from `register_function` — we never name a `tera`
// type, keeping it off our direct deps and pinned to loco's.
tera.register_function("csrf_token", |_args: &HashMap<String, serde_json::Value>| {
Ok(serde_json::Value::String(
crate::shared::csrf::current_token().unwrap_or_default(),
))
});
Ok(())
})?
} else { } else {
engines::TeraView::build()?.post_process(|tera| { None
tera.register_function("csrf_token", |_args: &HashMap<String, serde_json::Value>| {
Ok(serde_json::Value::String(
crate::shared::csrf::current_token().unwrap_or_default(),
))
});
Ok(())
})?
}; };
let tera_engine = engines::TeraView::build()?.post_process(move |tera| {
if let Some(arc) = &locales {
tera.register_function("t", FluentLoader::new(arc.clone()));
}
// `csrf_token()`: the in-flight request's CSRF token (bound by
// `shared::csrf::protect`), rendered into `<body hx-headers>` and
// `ui::csrf_field()`. Inlined so its `tera::Error` return is inferred
// from `register_function` — we never name a `tera` type, keeping it
// off our direct deps and pinned to loco's.
tera.register_function("csrf_token", |_args: &HashMap<String, serde_json::Value>| {
Ok(serde_json::Value::String(
crate::shared::csrf::current_token().unwrap_or_default(),
))
});
Ok(())
})?;
Ok(router.layer(Extension(ViewEngine::from(tera_engine)))) Ok(router.layer(Extension(ViewEngine::from(tera_engine))))
} }
} }