1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//! Internationalization (i18n) module for matrix_gui.
//!
//! This module provides support for multiple languages in the GUI framework. It allows
//! runtime language switching and provides macros for defining internationalized strings
//! and toggle types.
//!
//! # Features
//!
//! - Runtime language selection using atomic operations
//! - Support for multiple languages (extensible)
//! - Macros for defining internationalized strings
//! - Toggle types with language-aware string representations
//!
//! # Example
//!
//! ```rust
//! use matrix_gui::prelude::*;
//!
//! // Define 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);
//!
//! // Switch language
//! Languages::switch_language();
//! ```
use ;
/// Current language selection stored atomically.
///
/// This static variable holds the index of the currently selected language.
/// It uses atomic operations to ensure thread-safe access without locks.
static SELECTION: AtomicU8 = new;
/// Total number of supported languages.
///
/// This constant is computed at compile time using the `enum_iterator` crate
/// to determine the cardinality of the `Languages` enum.
const LANG_COUNT: usize = ;
/// Supported languages in the GUI framework.
///
/// This enum defines the available languages for internationalization.
/// New languages can be added by extending this enum.
/// Conversion from `Languages` to a static string slice.
///
/// This implementation provides a human-readable name for each language.
/// For `LocalLang`, it attempts to use the `LOCAL_LANG_STRING` environment variable
/// at compile time, falling back to "中文" if not set.
/// Conversion from a byte index to `Languages`.
///
/// This implementation allows converting a language index (as stored in the
/// atomic `SELECTION` variable) back to a `Languages` enum variant.
/// An internationalized string that supports multiple languages.
///
/// This struct stores string variants for all supported languages and provides
/// automatic language selection based on the current global language setting.
/// It dereferences to a `&str` for convenient usage.
///
/// # Example
///
/// ```rust
/// use matrix_gui::prelude::*;
///
/// // Create an internationalized string
/// let greeting = I18NString::new(["你好", "Hello"]);
///
/// // Use it like a string
/// println!("{}", greeting.as_str());
/// ```
/// Allows `I18NString` to be used like a string slice.
///
/// This implementation enables automatic dereferencing to `&str`, allowing
/// `I18NString` instances to be used wherever a string slice is expected.
/// Macro to define an internationalized string constant.
///
/// This macro creates a public constant of type `I18NString` that contains
/// string variants for all supported languages. The strings are provided
/// in the same order as the `Languages` enum variants.
///
/// # Syntax
///
/// ```ignore
/// matrix_gui::i18n_string!(NAME, string_for_lang0, string_for_lang1);
/// ```
///
/// # Arguments
///
/// * `NAME` - The identifier name for the constant
/// * `string_for_langN` - The string for each language (must match LANG_COUNT)
///
/// # Examples
///
/// ```rust
/// use matrix_gui::prelude::*;
///
/// // Define internationalized strings for UI elements
/// matrix_gui::i18n_string!(TIP_ON, "开", "ON");
/// matrix_gui::i18n_string!(TIP_OFF, "关", "OFF");
/// matrix_gui::i18n_string!(MENU_SETTINGS, "设置", "Settings");
///
/// // Use the strings
/// println!("{}", TIP_ON.as_str());
/// ```
///
/// # Notes
///
/// - The number of string arguments must match the number of supported languages
/// - The order of strings must correspond to the `Languages` enum variants
/// - The generated constant is public and can be used across modules
/// 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
///
/// ```ignore
/// 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
///
/// ```rust
/// 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:
///
/// ```ignore
/// 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