rustapi_validate/v2/i18n.rs
1use rust_i18n::t;
2
3/// Helper to translate a message.
4///
5/// Falls back to the message key if no translation is found.
6///
7/// # Arguments
8///
9/// * `key` - The message key (e.g. "validation.email.invalid")
10/// * `locale` - The locale to use (e.g. "en", "tr"). If None, uses default.
11pub fn translate(key: &str, locale: Option<&str>) -> String {
12 let result = if let Some(locale) = locale {
13 t!(key, locale = locale).to_string()
14 } else {
15 t!(key).to_string()
16 };
17
18 // Fallback to English if translation is missing (returns key)
19 if result == key {
20 t!(key, locale = "en").to_string()
21 } else {
22 result
23 }
24}
25
26/// Helper to translate with arguments.
27pub fn translate_with_args(key: &str, locale: Option<&str>, _args: &[(&str, &str)]) -> String {
28 if let Some(locale) = locale {
29 // rust-i18n t! macro doesn't support dynamic args easily in this wrapped form
30 // We might need to use the lower level API or just interpolate ourselves
31 // For now let's use the basic t! with variable interpolation if possible
32 // But t! requires string literals for keys mostly or known args.
33 // Let's stick to basic translation for now and use our existing interpolation
34 // in RuleError for variable replacement.
35 t!(key, locale = locale).to_string()
36 } else {
37 t!(key).to_string()
38 }
39}