macro_rules! i18n_toggle_type {
($type_name:ident, $true_str:expr, $false_str:expr) => { ... };
}Expand description
Macro to define a toggle type with internationalized string representations.
This macro creates a new struct that wraps a boolean value and provides language-aware string representations through dereferencing. The struct automatically selects the appropriate string based on the boolean value.
§Syntax
ⓘ
matrix_gui::i18n_toggle_type!(TypeName, true_string, false_string);§Arguments
TypeName- The identifier name for the new structtrue_string- TheI18NStringconstant to use when the boolean istruefalse_string- TheI18NStringconstant to use when the boolean isfalse
§Examples
use matrix_gui::prelude::*;
// First define the internationalized strings
matrix_gui::i18n_string!(TIP_ON, "开", "ON");
matrix_gui::i18n_string!(TIP_OFF, "关", "OFF");
// Define a toggle type
matrix_gui::i18n_toggle_type!(TipOnOff, TIP_ON, TIP_OFF);
// Use the toggle type
let toggle: &str = &TipOnOff(true);
println!("{}", toggle); // Prints "开" or "ON" based on current language§Generated Code
The macro generates a struct with the following structure:
ⓘ
pub struct TypeName(pub bool);
impl core::ops::Deref for TypeName {
type Target = str;
fn deref(&self) -> &Self::Target {
match self.0 {
true => &true_string,
false => &false_string,
}
}
}§Notes
- The generated struct implements
Dereftostrfor easy string access - The boolean value is stored in a tuple struct field
- Language switching affects the string representation automatically