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§
source§impl PartialEq for YewI18n
impl PartialEq for YewI18n
impl StructuralPartialEq for YewI18n
Auto Trait Implementations§
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
§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> 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.