,
+ pub tr: Tr,
pub message: String,
pub as_code: bool, // Display the message in markup.
}
impl MessageTemplate {
- pub fn new(message: &str) -> MessageTemplate {
+ pub fn new(message: &str, tr: Tr) -> MessageTemplate {
MessageTemplate {
user: None,
+ tr,
message: message.to_string(),
as_code: false,
}
}
- pub fn new_with_user(message: &str, user: Option) -> MessageTemplate {
+ pub fn new_with_user(message: &str, tr: Tr, user: Option) -> MessageTemplate {
MessageTemplate {
user,
+ tr,
message: message.to_string(),
as_code: false,
}
@@ -52,6 +60,7 @@ impl MessageTemplate {
#[template(path = "sign_up_form.html")]
pub struct SignUpFormTemplate {
pub user: Option,
+ pub tr: Tr,
pub email: String,
pub message: String,
@@ -63,6 +72,7 @@ pub struct SignUpFormTemplate {
#[template(path = "sign_in_form.html")]
pub struct SignInFormTemplate {
pub user: Option,
+ pub tr: Tr,
pub email: String,
pub message: String,
@@ -72,6 +82,7 @@ pub struct SignInFormTemplate {
#[template(path = "ask_reset_password.html")]
pub struct AskResetPasswordTemplate {
pub user: Option,
+ pub tr: Tr,
pub email: String,
pub message: String,
@@ -82,6 +93,7 @@ pub struct AskResetPasswordTemplate {
#[template(path = "reset_password.html")]
pub struct ResetPasswordTemplate {
pub user: Option,
+ pub tr: Tr,
pub reset_token: String,
pub message: String,
@@ -92,6 +104,7 @@ pub struct ResetPasswordTemplate {
#[template(path = "profile.html")]
pub struct ProfileTemplate {
pub user: Option,
+ pub tr: Tr,
pub username: String,
pub email: String,
@@ -104,6 +117,8 @@ pub struct ProfileTemplate {
#[template(path = "recipe_view.html")]
pub struct RecipeViewTemplate {
pub user: Option,
+ pub tr: Tr,
+
pub recipes: Recipes,
pub recipe: model::Recipe,
@@ -113,6 +128,8 @@ pub struct RecipeViewTemplate {
#[template(path = "recipe_edit.html")]
pub struct RecipeEditTemplate {
pub user: Option,
+ pub tr: Tr,
+
pub recipes: Recipes,
pub recipe: model::Recipe,
@@ -123,5 +140,7 @@ pub struct RecipeEditTemplate {
#[template(path = "recipes_list_fragment.html")]
pub struct RecipesListFragmentTemplate {
pub user: Option,
+ pub tr: Tr,
+
pub recipes: Recipes,
}
diff --git a/backend/src/main.rs b/backend/src/main.rs
index bb3d485..af80c90 100644
--- a/backend/src/main.rs
+++ b/backend/src/main.rs
@@ -1,7 +1,7 @@
use std::{net::SocketAddr, path::Path};
use axum::{
- extract::{ConnectInfo, FromRef, Request, State},
+ extract::{ConnectInfo, Extension, FromRef, Request, State},
http::StatusCode,
middleware::{self, Next},
response::{Response, Result},
@@ -12,10 +12,12 @@ use axum_extra::extract::cookie::CookieJar;
use chrono::prelude::*;
use clap::Parser;
use config::Config;
+use itertools::Itertools;
use tower_http::{services::ServeDir, trace::TraceLayer};
use tracing::{event, Level};
use data::{db, model};
+use translation::Tr;
mod config;
mod consts;
@@ -26,6 +28,7 @@ mod html_templates;
mod ron_extractor;
mod ron_utils;
mod services;
+mod translation;
mod utils;
#[derive(Clone)]
@@ -191,6 +194,7 @@ async fn main() {
.fallback(services::not_found)
.layer(TraceLayer::new_for_http())
// FIXME: Should be 'route_layer' but it doesn't work for 'fallback(..)'.
+ .layer(middleware::from_fn(translation))
.layer(middleware::from_fn_with_state(
state.clone(),
user_authentication,
@@ -218,6 +222,39 @@ async fn user_authentication(
Ok(next.run(req).await)
}
+async fn translation(
+ Extension(user): Extension