Files
Sikuwa/i18n/__init__.py
so陈 13a1072c6f
Some checks are pending
CI / Test (Python 3.10 on macos-latest) (push) Waiting to run
CI / Test (Python 3.11 on macos-latest) (push) Waiting to run
CI / Test (Python 3.12 on macos-latest) (push) Waiting to run
CI / Test (Python 3.8 on macos-latest) (push) Waiting to run
CI / Test (Python 3.9 on macos-latest) (push) Waiting to run
CI / Test (Python 3.10 on ubuntu-latest) (push) Waiting to run
CI / Test (Python 3.11 on ubuntu-latest) (push) Waiting to run
CI / Test (Python 3.12 on ubuntu-latest) (push) Waiting to run
CI / Test (Python 3.8 on ubuntu-latest) (push) Waiting to run
CI / Test (Python 3.9 on ubuntu-latest) (push) Waiting to run
CI / Test (Python 3.10 on windows-latest) (push) Waiting to run
CI / Test (Python 3.11 on windows-latest) (push) Waiting to run
CI / Test (Python 3.12 on windows-latest) (push) Waiting to run
CI / Test (Python 3.8 on windows-latest) (push) Waiting to run
CI / Test (Python 3.9 on windows-latest) (push) Waiting to run
CI / Lint (push) Waiting to run
CI / Release (push) Blocked by required conditions
Documentation / Build Documentation (push) Waiting to run
Sikuwa first commit
2026-02-20 23:53:48 +08:00

34 lines
880 B
Python

import gettext
import os
from pathlib import Path
# 获取翻译文件目录
i18n_dir = Path(__file__).parent
locale_dir = i18n_dir / "locales"
# 初始化翻译系统
translation = gettext.translation(
"sikuwa",
localedir=str(locale_dir), # 使用字符串路径
languages=None, # 使用系统默认语言
fallback=True # 如果找不到翻译文件,使用原始字符串
)
# 导出翻译函数
_ = translation.gettext
# 提供切换语言的功能
def set_language(lang_code):
"""切换当前使用的语言"""
global translation, _
try:
translation = gettext.translation(
"sikuwa",
localedir=str(locale_dir), # 使用字符串路径
languages=[lang_code],
fallback=True
)
_ = translation.gettext
return True
except Exception as e:
return False