Skip to main content

i18n_toggle_type

Macro i18n_toggle_type 

Source
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 struct
  • true_string - The I18NString constant to use when the boolean is true
  • false_string - The I18NString constant to use when the boolean is false

§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 Deref to str for easy string access
  • The boolean value is stored in a tuple struct field
  • Language switching affects the string representation automatically