Skip to content

Commit

Permalink
[wip]
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub committed Oct 19, 2024
1 parent 5523116 commit c59e257
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 44 deletions.
9 changes: 6 additions & 3 deletions i18n/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ captcha = "Captcha"
easy = "Easy"
medium = "Medium"
hard = "Hard"
captcha_type = "Captcha Type"
default_home_page = "Default Home Page"
captcha_name = "Captcha Name"
home_page = "Default Home Page"
spam_regex = "Spam Regex"
lang = "Language"

all = "All"
joined = "Joined"
following = "Following"
explore_all = "Explore All"
list = "List"

save = "Save"

Expand Down Expand Up @@ -81,6 +83,7 @@ upload = "Upload"
inn_name = "Inn Name"
inn_type = "Inn Type"
inn_feed_help = "Submit url again to unsubscribe."
inn_mod_max = "Inn Mod Max"

edit = "Edit"
new_comment = "New Comment"
Expand Down
108 changes: 108 additions & 0 deletions i18n/zh_cn.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
sign_in = "登录"
sign_out = "登出"
sign_up = "注册"

site_settings = "站点设置"
db_view = "数据库视图"
stats = "统计"

site_name = "站点名称"
domain = "域名"
description = "描述"
read_only = "只读"
title_max_length = "标题最大长度"
article_max_length = "文章最大长度"
comment_max_length = "评论最大长度"
solo_interval = "Solo 间隔"
post_interval = "帖子间隔"
comment_interval = "评论间隔"
per_page = "每页显示"
captcha_difficulty = "验证码难度"
about = "关于"
captcha = "验证码"
easy = "简单"
medium = "中等"
hard = "困难"
captcha_name = "验证码名称"
home_page = "默认首页"
spam_regex = "垃圾正则"
lang = "语言"

all = "全部"
joined = "已加入"
following = "关注中"
list = "列表"

save = "保存"

delete_permanently = "永久删除"
delete_sure = "你确定要删除吗?"
delete = "删除"
prev = "上一页"
next = "下一页"
error = "错误"

star = "收藏"
unread = "未读"
subscribe = "订阅"
unsubscribe = "取消订阅"

url = "URL"
folder = "文件夹"
new = "新建"
new_folder = "新建文件夹"
is_public = "是否公开"
add = "添加"
source = "来源"
load_image = "加载图片"

new_post = "新帖子"
exit = "退出"
limited = "受限"
rejected = "已拒绝"
pending = "待处理"

members = "成员"
mod = "版主"
join = "加入"
active = "活跃"
users = "用户"
topics = "话题"

public = "公开"
apply = "申请"
hidden = "隐藏"
private = "私有"
private_help = "私有客栈不能更改为其他类型。"
private_hidden = "私有隐藏"
limited_edit_seconds = "限制编辑秒数"
limited_edit_seconds_help = "用户被禁止编辑之前的时间限制?(设置为 0 表示可随时编辑)"

avatar_help = "请清除浏览器缓存"
upload = "上传"
inn_name = "客栈名称"
inn_type = "客栈类型"
inn_feed_help = "再次提交 URL 以取消订阅。"
inn_mod_max = "可担任版主上限"

edit = "编辑"
new_comment = "新评论"
submit = "提交"
preview = "预览"
join_to_comment = "加入以评论"
sign_in_to_comment = "登录以评论"
save_draft = "保存草稿"
delete_draft = "删除草稿"
draft = "草稿"
uname_or_id = "用户名或 UID"
recovery_code = "恢复代码"
new_password = "新密码"
confirm = "确认"
change = "更改"
search = "搜索"
post = "发布"
comment = "评论"

stay_logged_in = "保持登录"
forgot_password = "忘记密码?"
password_reset = "密码重置"
3 changes: 2 additions & 1 deletion src/controller/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{
Claim, Feed, FormPost, Item, SiteConfig,
};
use crate::{
controller::{Comment, Inn, Post, Solo, User},
controller::{filters, Comment, Inn, Post, Solo, User},
error::AppError,
DB,
};
Expand Down Expand Up @@ -393,6 +393,7 @@ impl Default for SiteConfig {
captcha_name: "Lucy".into(),
home_page: 0,
spam_regex: None,
lang: "en".into(),
}
}
}
Expand Down
15 changes: 3 additions & 12 deletions src/controller/meta_handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, sync::LazyLock};
use std::sync::LazyLock;

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

static I18N: LazyLock<HashMap<&str, HashMap<String, String>>> = LazyLock::new(|| {
let mut i18n = HashMap::new();
let en = include_str!("../../i18n/en.toml");
let en = basic_toml::from_str::<HashMap<String, String>>(en).unwrap();
i18n.insert("en", en);
i18n
});

#[derive(Template)]
#[template(path = "error.html", escape = "none")]
struct PageError<'a> {
Expand Down Expand Up @@ -184,7 +176,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<String, String>,
pub(super) lang: &'a str,
}

impl<'a> PageData<'a> {
Expand All @@ -195,14 +187,13 @@ impl<'a> PageData<'a> {
has_unread: bool,
) -> Self {
let site_description = md2html(&site_config.description);
let i18n = I18N.get("en").unwrap().to_owned();
Self {
title,
site_name: &site_config.site_name,
site_description,
claim,
has_unread,
i18n,
lang: &site_config.lang,
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ pub(super) struct SiteConfig {
home_page: u8,
#[garde(skip)]
spam_regex: Option<String>,
#[garde(length(max = 16))]
lang: String,
}

impl SiteConfig {
Expand All @@ -503,3 +505,34 @@ struct Claim {
last_write: i64,
session_id: String,
}

mod filters {
use std::{collections::HashMap, sync::LazyLock};
use tracing::error;

static I18N: LazyLock<HashMap<(&str, &str), &str>> = LazyLock::new(|| {
let mut i18n = HashMap::new();
let en = include_str!("../../i18n/en.toml");
let en = basic_toml::from_str::<HashMap<&str, &str>>(en).unwrap();
for (k, v) in en.iter() {
i18n.insert(("en", *k), *v);
}

let zh_cn = include_str!("../../i18n/zh_cn.toml");
let zh_cn = basic_toml::from_str::<HashMap<&str, &str>>(zh_cn).unwrap();
for (k, v) in zh_cn.iter() {
i18n.insert(("zh_cn", *k), *v);
}
i18n
});

pub(super) fn l10n(s: &str, lang: &str) -> ::rinja::Result<&'static str> {
if let Some(v) = I18N.get(&(lang, s)) {
Ok(v)
} else {
let en = I18N.get(&("en", s)).unwrap();
error!("No translation for {} in {}", s, lang);
Ok(en)
}
}
}
1 change: 1 addition & 0 deletions src/controller/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::{
generate_nanoid_ttl, get_count, get_count_by_prefix, get_id_by_name, get_range,
is_valid_name, ivec_to_u32, set_one, set_one_with_key, IterType,
},
filters,
fmt::{clean_html, ts_to_date},
get_ids_by_prefix, get_one, incr_id,
meta_handler::{PageData, ParamsPage},
Expand Down
Loading

0 comments on commit c59e257

Please sign in to comment.