Skip to main content

I18nManager

Struct I18nManager 

Source
pub struct I18nManager { /* private fields */ }
Expand description

Fluent 消息翻译管理器。

持有 FTL 消息目录和当前 locale 信息。 通过 I18nManager::init 自动检测系统语言环境并初始化。

Implementations§

Source§

impl I18nManager

Source

pub fn init() -> &'static Self

检测系统语言环境并初始化全局管理器。

首次调用时根据系统 locale 加载对应的 FTL 消息文件。 后续调用直接返回已初始化的实例。

Source

pub fn init_with_locale(locale: &str) -> Result<&'static Self, I18nError>

使用指定 BCP-47 locale 标签初始化全局管理器。

§Errors

返回 I18nError::InvalidLocale 如果全局管理器已初始化。

§Panics

不会 panic。OnceLock::set 失败后通过 unwrap 获取的是已设置的值,保证安全。

Source

pub fn global() -> Option<&'static I18nManager>

获取全局 I18nManager 实例。

如果 initinit_with_locale 尚未调用,返回 None

Source

pub fn translate(&self, message_id: &str, args: &[(&str, &str)]) -> String

翻译消息 key,带参数替换。

如果消息 key 不存在,返回 key 本身作为 fallback。

Source

pub fn locale_tag(&self) -> &str

当前 locale 的 BCP-47 标签。

Trait Implementations§

Source§

impl Debug for I18nManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.