Struct I18n
pub struct I18n { /* private fields */ }i18n only.Expand description
The main handle for accessing and managing internationalization state.
I18n holds the selected language, fallback language, locale resources, and the active
Fluent bundle used for translating messages. It is Clone + Copy
so it can be freely passed around in components.
There are several ways to obtain an I18n instance:
use_init_i18nto create and provide it to descendant components.I18n::createto manually create one from anI18nConfig(useful when you need to handle errors).I18n::create_globalto create one with global lifetime, suitable for multi-window apps.I18n::getorI18n::try_getto retrieve an already-provided instance from the component context.use_share_i18nto re-provide an existing instance to a different part of the component tree.
Implementations§
§impl I18n
impl I18n
pub fn try_get() -> Option<I18n>
pub fn try_get() -> Option<I18n>
Try to retrieve the I18n instance from the component context.
Returns None if no I18n has been provided via use_init_i18n or use_share_i18n
in an ancestor component.
pub fn get() -> I18n
pub fn get() -> I18n
Retrieve the I18n instance from the component context.
This is the primary way to access the i18n state from within a component that is a
descendant of a component that called use_init_i18n or use_share_i18n.
§Panics
Panics if no I18n has been provided in an ancestor component.
#[derive(PartialEq)]
struct MyComponent;
impl Component for MyComponent {
fn render(&self) -> impl IntoElement {
let mut i18n = I18n::get();
let change_to_english = move |_| i18n.set_language(langid!("en-US"));
rect()
.child(t!("hello_world"))
.child(Button::new().on_press(change_to_english).child("English"))
}
}pub fn create(_: I18nConfig) -> Result<I18n, Error>
pub fn create(_: I18nConfig) -> Result<I18n, Error>
Manually create an I18n instance from an I18nConfig.
Unlike use_init_i18n, this does not automatically provide the instance to descendant
components. Use use_share_i18n to share it afterwards, or call this when you need
explicit error handling during initialization.
The created state is scoped to the current component. For global state that outlives
any single component, see I18n::create_global.
pub fn create_global(_: I18nConfig) -> Result<I18n, Error>
pub fn create_global(_: I18nConfig) -> Result<I18n, Error>
Create an I18n instance with global lifetime.
Unlike I18n::create, the state created here is not scoped to any component and will
live for the entire duration of the application. This is useful for multi-window apps
where i18n state needs to be created in main and then shared across different windows
via use_share_i18n.
This is not a hook, do not use it inside components like you would use_init_i18n.
You would usually want to call this in your main function.
struct MyApp {
i18n: I18n,
}
impl App for MyApp {
fn render(&self) -> impl IntoElement {
// Re-provide the global I18n to this window's component tree
use_share_i18n(move || self.i18n);
rect().child(t!("hello_world"))
}
}
fn main() {
// Create I18n with global lifetime in main, before any window is opened
let i18n = I18n::create_global(I18nConfig::new(langid!("en-US")).with_locale((
langid!("en-US"),
include_str!("../../../examples/i18n/en-US.ftl"),
)))
.expect("Failed to create I18n");
// Pass it to each window's app struct
launch(LaunchConfig::new().with_window(WindowConfig::new_app(MyApp { i18n })))
}pub fn try_translate_with_args(
&self,
msg: &str,
args: Option<&FluentArgs<'_>>,
) -> Result<String, Error>
pub fn try_translate_with_args( &self, msg: &str, args: Option<&FluentArgs<'_>>, ) -> Result<String, Error>
Translate a message by its identifier, optionally with Fluent arguments.
The msg can be a simple message id (e.g. "hello") or a dotted attribute
id (e.g. "my_component.placeholder"). See I18n::decompose_identifier for details.
Returns an error if the message id is not found, the pattern is missing, or Fluent reports errors during formatting.
Prefer the t!, te!, or tid! macros for ergonomic translations.
pub fn decompose_identifier(msg: &str) -> Result<(&str, Option<&str>), Error>
pub fn decompose_identifier(msg: &str) -> Result<(&str, Option<&str>), Error>
Split a message identifier into its message id and optional attribute name.
"hello"returns("hello", None)"my_component.placeholder"returns("my_component", Some("placeholder"))
Returns an error if the identifier contains more than one dot.
pub fn translate_with_args(
&self,
msg: &str,
args: Option<&FluentArgs<'_>>,
) -> String
pub fn translate_with_args( &self, msg: &str, args: Option<&FluentArgs<'_>>, ) -> String
Translate a message by its identifier, optionally with Fluent arguments.
This is the panicking version of I18n::try_translate_with_args.
§Panics
Panics if the translation fails for any reason.
pub fn try_translate(&self, msg: &str) -> Result<String, Error>
pub fn try_translate(&self, msg: &str) -> Result<String, Error>
Translate a message by its identifier, without arguments.
Shorthand for self.try_translate_with_args(msg, None).
pub fn translate(&self, msg: &str) -> String
pub fn translate(&self, msg: &str) -> String
Translate a message by its identifier, without arguments.
This is the panicking version of I18n::try_translate.
§Panics
Panics if the translation fails for any reason.
pub fn language(&self) -> LanguageIdentifier
pub fn language(&self) -> LanguageIdentifier
Get the selected language.
pub fn fallback_language(&self) -> Option<LanguageIdentifier>
pub fn fallback_language(&self) -> Option<LanguageIdentifier>
Get the fallback language.
pub fn try_set_language(&mut self, id: LanguageIdentifier) -> Result<(), Error>
pub fn try_set_language(&mut self, id: LanguageIdentifier) -> Result<(), Error>
Update the selected language, rebuilding the active Fluent bundle.
Returns an error if the bundle cannot be rebuilt for the new language.
pub fn set_language(&mut self, id: LanguageIdentifier)
pub fn set_language(&mut self, id: LanguageIdentifier)
Update the selected language, rebuilding the active Fluent bundle.
This is the panicking version of I18n::try_set_language.
§Panics
Panics if the bundle cannot be rebuilt for the new language.
pub fn try_set_fallback_language(
&mut self,
id: LanguageIdentifier,
) -> Result<(), Error>
pub fn try_set_fallback_language( &mut self, id: LanguageIdentifier, ) -> Result<(), Error>
Update the fallback language, rebuilding the active Fluent bundle.
The given language must have a corresponding Locale registered in the config.
Returns an error if no locale exists for the language or if the bundle cannot be rebuilt.
pub fn set_fallback_language(&mut self, id: LanguageIdentifier)
pub fn set_fallback_language(&mut self, id: LanguageIdentifier)
Update the fallback language, rebuilding the active Fluent bundle.
This is the panicking version of I18n::try_set_fallback_language.
§Panics
Panics if no locale exists for the language or if the bundle cannot be rebuilt.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for I18n
impl !RefUnwindSafe for I18n
impl !Send for I18n
impl !Sync for I18n
impl Unpin for I18n
impl UnsafeUnpin for I18n
impl !UnwindSafe for I18n
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more