removed redundancy
This commit is contained in:
@@ -24,7 +24,9 @@ impl Initializer for ViewEngineInitializer {
|
||||
}
|
||||
|
||||
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(
|
||||
ArcLoader::builder(&I18N_DIR, unic_langid::langid!("sk"))
|
||||
.shared_resources(Some(&[I18N_SHARED.into()]))
|
||||
@@ -33,31 +35,27 @@ impl Initializer for ViewEngineInitializer {
|
||||
.map_err(|e| Error::string(&e.to_string()))?,
|
||||
);
|
||||
info!("locales loaded");
|
||||
|
||||
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(())
|
||||
})?
|
||||
Some(arc)
|
||||
} else {
|
||||
engines::TeraView::build()?.post_process(|tera| {
|
||||
None
|
||||
};
|
||||
|
||||
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))))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user