pub fn is_chinese(lang: &str) -> bool {
matches!(
lang.to_lowercase().replace('_', "-").as_str(),
"zh" | "zh-cn" | "chinese"
)
}
pub fn t<'a>(lang: &'a str, zh: &'a str, en: &'a str) -> &'a str {
if is_chinese(lang) {
zh
} else {
en
}
}
pub fn t_owned(lang: &str, zh: &str, en: &str) -> String {
if is_chinese(lang) {
zh.to_string()
} else {
en.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_chinese_standard_variants() {
assert!(is_chinese("zh-CN"), "zh-CN should be Chinese");
assert!(is_chinese("zh"), "zh should be Chinese");
assert!(is_chinese("chinese"), "chinese string should be Chinese");
assert!(
is_chinese("zh_CN"),
"zh_CN (underscore) should be normalized to zh-CN"
);
}
#[test]
fn test_is_chinese_non_chinese() {
assert!(!is_chinese("en-US"), "en-US is not Chinese");
assert!(!is_chinese("en"), "en is not Chinese");
assert!(!is_chinese("ja"), "ja is not Chinese");
assert!(!is_chinese("fr"), "fr is not Chinese");
assert!(!is_chinese("de"), "de is not Chinese");
}
#[test]
fn test_is_chinese_case_insensitive() {
assert!(is_chinese("ZH"), "ZH uppercase should match");
assert!(is_chinese("Zh-cn"), "Zh-cn mixed case should match");
assert!(is_chinese("Chinese"), "Chinese capitalized should match");
}
#[test]
fn test_is_chinese_empty() {
assert!(!is_chinese(""), "empty string is not Chinese");
}
#[test]
fn test_t_basic() {
assert_eq!(
t("zh-CN", "中文", "English"),
"中文",
"zh-CN => Chinese text"
);
assert_eq!(
t("en-US", "中文", "English"),
"English",
"en-US => English text"
);
}
#[test]
fn test_t_fallback_for_unknown() {
assert_eq!(
t("klingon", "中文", "English"),
"English",
"unknown locale => English"
);
assert_eq!(
t("", "中文", "English"),
"English",
"empty locale => English"
);
}
#[test]
fn test_t_owned_basic() {
assert_eq!(
t_owned("zh", "中文", "English"),
"中文",
"zh => Chinese String"
);
assert_eq!(
t_owned("en", "中文", "English"),
"English",
"en => English String"
);
}
#[test]
fn test_t_owned_identical_strings() {
let result = t_owned("en", "same", "same");
assert_eq!(
result, "same",
"identical zh/en still returns correct value"
);
}
}