Skip to main content

beam_core/
i18n.rs

1use std::collections::HashMap;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum Locale {
5    Zh,
6    En,
7}
8
9impl Locale {
10    pub fn from_str(s: &str) -> Self {
11        match s {
12            "en" => Self::En,
13            _ => Self::Zh,
14        }
15    }
16
17    pub fn as_str(&self) -> &str {
18        match self {
19            Self::Zh => "zh",
20            Self::En => "en",
21        }
22    }
23}
24
25pub struct I18n {
26    locale: Locale,
27    messages: HashMap<&'static str, &'static str>,
28}
29
30impl I18n {
31    pub fn new(locale: Locale) -> Self {
32        let messages = match locale {
33            Locale::Zh => HashMap::from([
34                ("permission_denied", "权限不足"),
35                ("session_closed", "会话已关闭"),
36                ("session_created", "会话已创建"),
37                ("session_not_found", "未找到会话"),
38                ("grant_success", "授权成功"),
39                ("grant_denied_msg", "已拒绝授权"),
40                ("workflow_running", "工作流运行中"),
41                ("workflow_complete", "工作流已完成"),
42                ("workflow_failed", "工作流失败"),
43                ("setup_complete", "设置完成"),
44                ("migration_complete", "迁移完成"),
45                ("bot_not_found", "未找到机器人配置"),
46                ("daemon_started", "守护进程已启动"),
47                ("daemon_stopped", "守护进程已停止"),
48            ]),
49            Locale::En => HashMap::from([
50                ("permission_denied", "Permission denied"),
51                ("session_closed", "Session closed"),
52                ("session_created", "Session created"),
53                ("session_not_found", "Session not found"),
54                ("grant_success", "Grant successful"),
55                ("grant_denied_msg", "Grant denied"),
56                ("workflow_running", "Workflow running"),
57                ("workflow_complete", "Workflow completed"),
58                ("workflow_failed", "Workflow failed"),
59                ("setup_complete", "Setup complete"),
60                ("migration_complete", "Migration complete"),
61                ("bot_not_found", "Bot config not found"),
62                ("daemon_started", "Daemon started"),
63                ("daemon_stopped", "Daemon stopped"),
64            ]),
65        };
66        Self { locale, messages }
67    }
68
69    pub fn t(&self, key: &str) -> &'static str {
70        self.messages.get(key).copied().unwrap_or("")
71    }
72
73    pub fn locale(&self) -> Locale {
74        self.locale
75    }
76}