-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.py
More file actions
30 lines (26 loc) · 968 Bytes
/
Copy pathi18n.py
File metadata and controls
30 lines (26 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
import json
import streamlit as st
@st.cache_data
def load_locales():
"""自动读取本地 locales 目录下的所有本地化 JSON 文件"""
locales = {}
base_dir = os.path.dirname(os.path.abspath(__file__))
locales_dir = os.path.join(base_dir, "locales")
if os.path.exists(locales_dir):
for file in os.listdir(locales_dir):
if file.endswith(".json"):
lang_name = file.split(".")[0]
with open(os.path.join(locales_dir, file), "r", encoding="utf-8") as f:
locales[lang_name] = json.load(f)
return locales
# 全局单例加载
LOCALES = load_locales()
def _(key: str) -> str:
"""
核心翻译响应函数。
读取当前 session_state 里的语言标记,找不到 key 时安全退回 key 本身。
"""
current_lang = st.session_state.get("lang", "zh")
lang_dict = LOCALES.get(current_lang, {})
return lang_dict.get(key, key)