pub struct YewI18n {
pub config: YewI18nConfig,
/* private fields */
}
Expand description
The YewI18n struct representing the state and methods for internationalization.
Fieldsยง
ยงconfig: YewI18nConfig
Configuration for YewI18n, specifying supported languages and translations.
Implementationsยง
Sourceยงimpl YewI18n
impl YewI18n
Sourcepub fn new(
config: YewI18nConfig,
translations: HashMap<String, Value>,
) -> Result<YewI18n, String>
pub fn new( config: YewI18nConfig, translations: HashMap<String, Value>, ) -> Result<YewI18n, String>
Creates a new instance of YewI18n.
ยงArguments
config
- Configuration for YewI18n.translations
- Translations for different languages.
ยงReturns
A Result containing the initialized YewI18n instance or an error message.
ยงExamples
use yew_i18n::{YewI18n, YewI18nConfig};
use std::collections::HashMap;
let supported_languages = vec!["en", "fr"];
let translations = HashMap::new();
let result = YewI18n::new(YewI18nConfig { supported_languages, translations: translations.clone()}, translations);
assert!(result.is_ok());
Sourcepub fn set_translation_language(&mut self, language: &str) -> Result<(), String>
pub fn set_translation_language(&mut self, language: &str) -> Result<(), String>
Sets the current language for translations.
ยงArguments
language
- The language code to set.
ยงReturns
A Result indicating success or an error message if the language is not supported.
ยงExamples
use yew_i18n::{YewI18n, YewI18nConfig};
use std::collections::HashMap;
let supported_languages = vec!["en", "fr"];
let translations = HashMap::new();
let mut i18n = YewI18n::new(YewI18nConfig { supported_languages, translations: translations.clone()}, translations).unwrap();
assert!(i18n.set_translation_language("fr").is_ok());
Sourcepub fn t(&self, key: &str) -> String
pub fn t(&self, key: &str) -> String
Retrieves a translated string for a given key.
ยงArguments
key
- The translation key.
ยงReturns
The translated string or an error message if the key is not found.
ยงExamples
use yew_i18n::{YewI18n, YewI18nConfig};
use std::collections::HashMap;
use serde_json::json;
let supported_languages = vec!["en", "fr"];
let mut translations = HashMap::new();
translations.insert("en".to_string(), json!({ "greeting": "Hello" }));
translations.insert("fr".to_string(), json!({ "greeting": "Bonjour" }));
let i18n = YewI18n::new(YewI18nConfig { supported_languages, translations: translations.clone()}, translations).unwrap();
assert_eq!(i18n.t("greeting"), "Hello");
Trait Implementationsยง
impl StructuralPartialEq for YewI18n
Auto Trait Implementationsยง
impl Freeze for YewI18n
impl RefUnwindSafe for YewI18n
impl Send for YewI18n
impl Sync for YewI18n
impl Unpin for YewI18n
impl UnwindSafe for YewI18n
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
Sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Sourceยงimpl<T> Instrument for T
impl<T> Instrument for T
Sourceยงfn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Sourceยงfn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Sourceยงimpl<T> IntoPropValue<Option<T>> for T
impl<T> IntoPropValue<Option<T>> for T
Sourceยงfn into_prop_value(self) -> Option<T>
fn into_prop_value(self) -> Option<T>
Convert
self
to a value of a Properties
struct.Sourceยงimpl<T> IntoPropValue<T> for T
impl<T> IntoPropValue<T> for T
Sourceยงfn into_prop_value(self) -> T
fn into_prop_value(self) -> T
Convert
self
to a value of a Properties
struct.