freya_i18n/
i18n_macro.rs

1//! Key translation macros.
2//!
3//! Using file:
4//!
5//! ```ftl
6//! # en-US.ftl
7//! #
8//! hello = Hello, {$name}!
9//! ```
10
11/// Translate message from key, returning [`crate::prelude::DioxusI18nError`] if id not found...
12///
13/// ```rust
14/// # use freya::prelude::*;
15/// # use freya_i18n::prelude::*;
16/// # use unic_langid::langid;
17/// # fn example() -> impl IntoElement {
18/// #   let lang = langid!("en-US");
19/// #   let config = I18nConfig::new(lang.clone()).with_locale((lang.clone(), "hello = Hello, {$name}")).with_fallback(lang.clone());
20/// #   let mut i18n = use_init_i18n(|| config);
21/// let name = "Avery Gigglesworth";
22/// let hi = te!("hello", name: {name}).expect("message id 'name' should be present");
23/// assert_eq!(hi, "Hello, Avery Gigglesworth");
24/// #   rect()
25/// # }
26/// ```
27#[macro_export]
28macro_rules! te {
29    ($id:expr, $( $name:ident : $value:expr ),* ) => {
30        {
31            let mut params_map = freya_i18n::fluent::FluentArgs::new();
32            $(
33                params_map.set(stringify!($name), $value);
34            )*
35            freya_i18n::prelude::I18n::get().try_translate_with_args($id, Some(&params_map))
36        }
37    };
38
39    ($id:expr ) => {{
40            freya_i18n::prelude::I18n::get().try_translate($id)
41    }};
42}
43
44/// Translate message from key, panic! if id not found...
45///
46/// ```rust
47/// # use freya::prelude::*;
48/// # use freya_i18n::prelude::*;
49/// # use unic_langid::langid;
50/// # fn example() -> impl IntoElement {
51/// #   let lang = langid!("en-US");
52/// #   let config = I18nConfig::new(lang.clone()).with_locale((lang.clone(), "hello = Hello, {$name}")).with_fallback(lang.clone());
53/// #   let mut i18n = use_init_i18n(|| config);
54/// let name = "Avery Gigglesworth";
55/// let hi = t!("hello", name: {name});
56/// assert_eq!(hi, "Hello, Avery Gigglesworth");
57/// #   rect()
58/// # }
59/// ```
60#[macro_export]
61macro_rules! t {
62    ($id:expr, $( $name:ident : $value:expr ),* ) => {
63        freya_i18n::te!($id, $( $name : $value ),*).unwrap_or_else(|e| panic!("{}", e.to_string()))
64    };
65
66    ($id:expr ) => {{
67        freya_i18n::te!($id).unwrap_or_else(|e| panic!("{}", e.to_string()))
68    }};
69}
70
71/// Translate message from key, return id if no translation found...
72///
73/// ```rust
74/// # use freya::prelude::*;
75/// # use freya_i18n::{tid, prelude::*};
76/// # use unic_langid::langid;
77/// # fn example() -> impl IntoElement {
78/// #   let lang = langid!("en-US");
79/// #   let config = I18nConfig::new(lang.clone()).with_locale((lang.clone(), "hello = Hello, {$name}")).with_fallback(lang.clone());
80/// #   let mut i18n = use_init_i18n(|| config);
81/// let message = tid!("no-key");
82/// assert_eq!(message, "message-id: no-key should be translated");
83/// #   rect()
84/// # }
85/// ```
86#[macro_export]
87macro_rules! tid {
88    ($id:expr, $( $name:ident : $value:expr ),* ) => {
89        freya_i18n::te!($id, $( $name : $value ),*).unwrap_or_else(|e| e.to_string())
90    };
91
92    ($id:expr ) => {{
93        freya_i18n::te!($id).unwrap_or_else(|e| e.to_string())
94    }};
95}