pub struct I18nManager { /* private fields */ }Expand description
Fluent 消息翻译管理器。
持有 FTL 消息目录和当前 locale 信息。
通过 I18nManager::init 自动检测系统语言环境并初始化。
Implementations§
Source§impl I18nManager
impl I18nManager
Sourcepub fn init() -> &'static Self
pub fn init() -> &'static Self
检测系统语言环境并初始化全局管理器。
首次调用时根据系统 locale 加载对应的 FTL 消息文件。 后续调用直接返回已初始化的实例。
Sourcepub fn init_with_locale(locale: &str) -> Result<&'static Self, I18nError>
pub fn init_with_locale(locale: &str) -> Result<&'static Self, I18nError>
使用指定 BCP-47 locale 标签初始化全局管理器。
§Errors
返回 I18nError::InvalidLocale 如果全局管理器已初始化。
§Panics
不会 panic。OnceLock::set 失败后通过 unwrap 获取的是已设置的值,保证安全。
Sourcepub fn global() -> Option<&'static I18nManager>
pub fn global() -> Option<&'static I18nManager>
获取全局 I18nManager 实例。
如果 init 或 init_with_locale
尚未调用,返回 None。
Sourcepub fn translate(&self, message_id: &str, args: &[(&str, &str)]) -> String
pub fn translate(&self, message_id: &str, args: &[(&str, &str)]) -> String
翻译消息 key,带参数替换。
如果消息 key 不存在,返回 key 本身作为 fallback。
Sourcepub fn locale_tag(&self) -> &str
pub fn locale_tag(&self) -> &str
当前 locale 的 BCP-47 标签。
Trait Implementations§
Auto Trait Implementations§
impl Freeze for I18nManager
impl RefUnwindSafe for I18nManager
impl Send for I18nManager
impl Sync for I18nManager
impl Unpin for I18nManager
impl UnsafeUnpin for I18nManager
impl UnwindSafe for I18nManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more