Skip to content

Commit

Permalink
i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub committed Oct 14, 2024
1 parent b19ab25 commit 87d4921
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/controller/meta_handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::LazyLock;
use std::{collections::HashMap, sync::LazyLock};

use super::{db_utils::u32_to_ivec, fmt::md2html, Claim, SiteConfig};
use crate::{error::AppError, DB};
Expand All @@ -14,6 +14,13 @@ use http::{HeaderName, StatusCode};
use rinja_axum::{into_response, Template};
use tracing::error;

static I18N: LazyLock<HashMap<&str, &str>> = LazyLock::new(|| {
let mut i18n = HashMap::new();
i18n.insert("sign_in", "Sign in");
i18n.insert("sign_up", "Sign up");
i18n
});

#[derive(Template)]
#[template(path = "error.html", escape = "none")]
struct PageError<'a> {
Expand Down Expand Up @@ -176,6 +183,7 @@ pub(super) struct PageData<'a> {
pub(super) site_description: String,
pub(super) claim: Option<Claim>,
pub(super) has_unread: bool,
pub(super) i18n: HashMap<&'a str, &'a str>,
}

impl<'a> PageData<'a> {
Expand All @@ -186,12 +194,14 @@ impl<'a> PageData<'a> {
has_unread: bool,
) -> Self {
let site_description = md2html(&site_config.description);
let i18n = I18N.clone();
Self {
title,
site_name: &site_config.site_name,
site_description,
claim,
has_unread,
i18n,
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/controller/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use ring::{
use rinja_axum::{into_response, Template};
use serde::Deserialize;
use sled::Db;
use std::{cmp::Ordering, fmt::Display, num::NonZeroU32, time::Duration};
use std::{cmp::Ordering, collections::HashMap, fmt::Display, num::NonZeroU32, time::Duration};
use tokio::time::sleep;

/// Page data: `user.html`
Expand Down Expand Up @@ -913,6 +913,10 @@ pub(crate) async fn signup() -> Result<impl IntoResponse, AppError> {
DB.open_tree("captcha")?
.insert(&captcha_id, &*captcha.chars_as_string())?;

let mut i18n = HashMap::new();
i18n.insert("sign_in", "Sign in");
i18n.insert("sign_up", "Sign up");

let page_signup = PageSignup {
page_data,
captcha_id,
Expand Down
2 changes: 1 addition & 1 deletion templates/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<form id="signup" class="box" action="/signup" method="post">
<fieldset>
<div class="content">
<center><h1>Sign up</h1></center>
<center><h1>{{ page_data.i18n.get("sign_up").unwrap() }}</h1></center>
</div>
<div class="field">
<div class="control has-icons-left">
Expand Down

0 comments on commit 87d4921

Please sign in to comment.