use crate::error::GarrisonError;
use std::cell::RefCell;
use std::sync::OnceLock;
#[macro_export]
macro_rules! loc {
($key:expr, $fallback:expr $(, ($arg_k:expr, $arg_v:expr))*) => {{
let translated = $crate::i18n::translate_detail($key, &[$(($arg_k, $arg_v)),*]);
if translated == $key {
let fallback = $fallback.to_string();
if fallback.is_empty() {
translated
} else {
fallback
}
} else {
translated
}
}};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GarrisonLocale {
Zh,
#[default]
En,
}
impl GarrisonLocale {
fn as_language_tag(self) -> &'static str {
match self {
GarrisonLocale::Zh => "zh-CN",
GarrisonLocale::En => "en",
}
}
}
static DETECTED_LOCALE: OnceLock<GarrisonLocale> = OnceLock::new();
pub fn detect_locale() -> GarrisonLocale {
detect_from(
&|key| std::env::var(key).ok(),
sys_locale::get_locale().as_deref(),
)
}
fn detect_from(getenv: &dyn Fn(&str) -> Option<String>, sys: Option<&str>) -> GarrisonLocale {
detect_from_env(getenv)
.or_else(|| sys.and_then(normalize_lang))
.unwrap_or(GarrisonLocale::En)
}
fn detect_from_env(getenv: &dyn Fn(&str) -> Option<String>) -> Option<GarrisonLocale> {
const CHAIN: [&str; 4] = ["GARRISON_LANG", "LC_ALL", "LC_MESSAGES", "LANG"];
CHAIN.iter().find_map(|key| {
getenv(key)
.filter(|raw| !raw.trim().is_empty())
.and_then(|raw| normalize_lang(&raw))
})
}
fn normalize_lang(raw: &str) -> Option<GarrisonLocale> {
let tag = raw
.split('@')
.next()
.unwrap_or_default()
.split('.')
.next()
.unwrap_or_default()
.replace('_', "-");
if matches!(tag.as_str(), "C" | "POSIX") {
return None;
}
let lang = tag
.split('-')
.next()
.unwrap_or_default()
.to_ascii_lowercase();
match lang.as_str() {
"zh" => Some(GarrisonLocale::Zh),
"en" => Some(GarrisonLocale::En),
_ => None,
}
}
thread_local! {
static CURRENT_LOCALE_STACK: RefCell<Vec<GarrisonLocale>> = const { RefCell::new(Vec::new()) };
}
pub fn current_locale() -> GarrisonLocale {
CURRENT_LOCALE_STACK.with(|stack| match stack.borrow().last() {
Some(locale) => *locale,
None => *DETECTED_LOCALE.get_or_init(detect_locale),
})
}
pub fn set_locale(locale: GarrisonLocale) -> LocaleGuard {
CURRENT_LOCALE_STACK.with(|stack| stack.borrow_mut().push(locale));
LocaleGuard { _priv: () }
}
pub struct LocaleGuard {
_priv: (),
}
impl Drop for LocaleGuard {
fn drop(&mut self) {
CURRENT_LOCALE_STACK.with(|stack| {
stack.borrow_mut().pop();
});
}
}
static I18N_REGISTERED: OnceLock<()> = OnceLock::new();
fn flush_ftl_entry(
key: &mut Option<String>,
lines: &mut Vec<String>,
out: &mut Vec<(String, String)>,
) {
if let Some(key) = key.take() {
let had_inline = lines.first().is_some_and(|l| !l.is_empty());
if !had_inline && lines.first().is_some_and(|l| l.is_empty()) {
lines.remove(0);
}
while lines.last().is_some_and(|l| l.is_empty()) {
lines.pop();
}
let cont_start = usize::from(had_inline);
let common = lines
.iter()
.skip(cont_start)
.filter(|l| !l.trim().is_empty())
.map(|l| l.len() - l.trim_start().len())
.min()
.unwrap_or(0);
let prefix = " ".repeat(common);
let value = lines
.iter()
.enumerate()
.map(|(i, l)| {
let stripped = if i >= cont_start && common > 0 && l.starts_with(prefix.as_str()) {
&l[common..]
} else {
l
};
stripped.replace("{\"{\"}", "{").replace("{\"}\"}", "}")
})
.collect::<Vec<_>>()
.join("\n");
out.push((key, value));
lines.clear();
}
}
fn normalize_ftl_placeholders(ftl: &str) -> String {
if !ftl.contains("{$") {
return ftl.to_string();
}
let mut out = String::with_capacity(ftl.len());
let mut in_placeholder = false;
for ch in ftl.chars() {
match ch {
'{' => {
in_placeholder = true;
out.push(ch);
},
'}' => {
in_placeholder = false;
out.push(ch);
},
'\n' if in_placeholder => {},
_ => out.push(ch),
}
}
out
}
fn parse_ftl_entries(ftl: &str) -> Vec<(String, String)> {
let ftl = &normalize_ftl_placeholders(ftl);
let mut out: Vec<(String, String)> = Vec::new();
let mut cur_key: Option<String> = None;
let mut cur_lines: Vec<String> = Vec::new();
for line in ftl.lines() {
if cur_key.is_some() && (line.starts_with([' ', '\t']) || line.trim().is_empty()) {
cur_lines.push(line.to_string());
continue;
}
flush_ftl_entry(&mut cur_key, &mut cur_lines, &mut out);
if line.trim().is_empty() || line.trim_start().starts_with('#') {
continue;
}
if let Some((k, v)) = line.split_once('=') {
cur_key = Some(k.trim().to_string());
cur_lines.push(v.trim().to_string());
}
}
flush_ftl_entry(&mut cur_key, &mut cur_lines, &mut out);
out
}
fn ensure_registered() {
I18N_REGISTERED.get_or_init(|| {
for (tag, ftl) in [
("zh-CN", include_str!("../locales/zh.ftl")),
("en", include_str!("../locales/en.ftl")),
] {
for (key, value) in parse_ftl_entries(ftl) {
sdforge::i18n::register_translation(tag, &key, &value);
}
}
});
}
fn lookup(tag: &str, key: &str, args: &[(&str, &str)]) -> Option<String> {
ensure_registered();
let sd_args: Vec<(&str, String)> = args.iter().map(|(k, v)| (*k, (*v).to_string())).collect();
let rendered = sdforge::i18n::translate_for(tag, key, &sd_args);
if rendered == key || rendered.contains("{$") || rendered.contains("{ $") {
None
} else {
Some(rendered)
}
}
pub fn translate_error(err: &GarrisonError) -> String {
let tag = current_locale().as_language_tag();
let (key, args) = error_to_key_args(err);
let args_ref: Vec<(&str, &str)> = args.iter().map(|(k, v)| (*k, v.as_str())).collect();
match lookup(tag, key, &args_ref) {
Some(value) => value,
None => {
if tag != "en" {
if let Some(value) = lookup("en", key, &args_ref) {
return value;
}
}
fallback_display(err)
},
}
}
pub fn translate_detail(key: &str, args: &[(&str, &str)]) -> String {
let tag = current_locale().as_language_tag();
match lookup(tag, key, args) {
Some(value) => value,
None => {
if tag != "en" {
lookup("en", key, args).unwrap_or_else(|| key.to_string())
} else {
key.to_string()
}
},
}
}
fn parse_keyed_detail(s: &str) -> Option<(&'static str, Vec<(&'static str, String)>)> {
if !s.contains("::") {
return None;
}
let mut parts = s.splitn(3, "::");
let key = parts.next()?;
if key.is_empty() || !key.chars().next().unwrap().is_ascii_alphabetic() {
return None;
}
if key
.chars()
.any(|c| !(c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-'))
{
return None;
}
let static_key = intern_string(key);
let a0 = parts.next();
let a1 = parts.next();
match (a0, a1) {
(None, None) | (None, Some(_)) => Some((static_key, vec![])),
(Some(x), None) => Some((static_key, vec![("arg0", x.to_string())])),
(Some(x), Some(y)) => Some((
static_key,
vec![("arg0", x.to_string()), ("arg1", y.to_string())],
)),
}
}
fn intern_string(s: &str) -> &'static str {
use std::collections::HashMap;
use std::sync::Mutex;
static CACHE: Mutex<Option<HashMap<String, &'static str>>> = Mutex::new(None);
let mut guard = CACHE.lock().unwrap();
let map = guard.get_or_insert_with(HashMap::new);
if let Some(&cached) = map.get(s) {
return cached;
}
let leaked: &'static str = Box::leak(s.to_string().into_boxed_str());
map.insert(s.to_string(), leaked);
leaked
}
fn string_detail(
variant_key: &'static str,
s: &str,
) -> (&'static str, Vec<(&'static str, String)>) {
match parse_keyed_detail(s) {
Some((k, args)) => (k, args),
None => (variant_key, vec![("detail", s.to_string())]),
}
}
fn error_to_key_args(err: &GarrisonError) -> (&'static str, Vec<(&'static str, String)>) {
match err {
GarrisonError::NotLogin(s) => string_detail("not-login", s),
GarrisonError::NotPermission(s) => string_detail("not-permission", s),
GarrisonError::NotRole(s) => string_detail("not-role", s),
GarrisonError::InvalidToken(s) => string_detail("invalid-token", s),
GarrisonError::TokenRevoked(s) => string_detail("token-revoked", s),
GarrisonError::ExpiredToken(s) => string_detail("expired-token", s),
GarrisonError::Dao(s) => string_detail("dao", s),
GarrisonError::Config(s) => string_detail("config", s),
GarrisonError::Internal(s) => string_detail("internal", s),
GarrisonError::Session(s) => string_detail("session", s),
GarrisonError::Annotation(s) => string_detail("annotation", s),
GarrisonError::Context(s) => string_detail("context", s),
GarrisonError::OAuth2(s) => string_detail("oauth2", s),
GarrisonError::Network(s) => string_detail("network", s),
GarrisonError::InvalidResponse(s) => string_detail("invalid-response", s),
GarrisonError::InvalidParam(s) => string_detail("invalid-param", s),
GarrisonError::NotImplemented(s) => string_detail("not-implemented", s),
GarrisonError::FirewallBlocked(s) => string_detail("firewall-blocked", s),
GarrisonError::DisableService { service, until } => (
"disable-service",
vec![
("service", service.clone()),
("until", format!("{:?}", until)),
],
),
GarrisonError::NotSafe { reason } => ("not-safe", vec![("reason", reason.clone())]),
GarrisonError::InvalidStateTransition { from, to } => (
"invalid-state-transition",
vec![("from", from.clone()), ("to", to.clone())],
),
GarrisonError::SmsRateLimitExceeded { window } => {
("sms-rate-limit-exceeded", vec![("window", window.clone())])
},
GarrisonError::SmsVerifyMaxAttempts => ("sms-verify-max-attempts", vec![]),
GarrisonError::SmsCodeNotFound => ("sms-code-not-found", vec![]),
GarrisonError::SmsChannelRecycled => ("sms-channel-recycled", vec![]),
GarrisonError::RateLimited { .. } => ("rate-limited", vec![]),
#[cfg(feature = "email-verification")]
GarrisonError::EmailRateLimitExceeded { window } => (
"email-rate-limit-exceeded",
vec![("window", window.clone())],
),
#[cfg(feature = "email-verification")]
GarrisonError::EmailVerifyMaxAttempts => ("email-verify-max-attempts", vec![]),
#[cfg(feature = "email-verification")]
GarrisonError::EmailCodeNotFound => ("email-code-not-found", vec![]),
#[cfg(feature = "email-verification")]
GarrisonError::EmailChannelRecycled => ("email-channel-recycled", vec![]),
#[cfg(feature = "credit-metering")]
GarrisonError::CreditInsufficient {
tenant_id,
requested,
remaining,
} => (
"credit-insufficient",
vec![
("tenant_id", tenant_id.to_string()),
("requested", requested.to_string()),
("remaining", remaining.to_string()),
],
),
GarrisonError::Exception(ex) => (
"exception",
vec![
("code", ex.code.to_string()),
("detail", ex.message.clone()),
],
),
}
}
fn fallback_display(err: &GarrisonError) -> String {
match err {
GarrisonError::NotLogin(s) => format!("Not logged in: {}", s),
GarrisonError::NotPermission(s) => format!("Permission denied: {}", s),
GarrisonError::NotRole(s) => format!("Role denied: {}", s),
GarrisonError::InvalidToken(s) => format!("Invalid token: {}", s),
GarrisonError::TokenRevoked(s) => format!("Token revoked: {}", s),
GarrisonError::ExpiredToken(s) => format!("Token expired: {}", s),
GarrisonError::Dao(s) => format!("DAO error: {}", s),
GarrisonError::Config(s) => format!("Configuration error: {}", s),
GarrisonError::Internal(s) => format!("Internal error: {}", s),
GarrisonError::Session(s) => format!("Session error: {}", s),
GarrisonError::Annotation(s) => format!("Annotation error: {}", s),
GarrisonError::Context(s) => format!("Context error: {}", s),
GarrisonError::OAuth2(s) => format!("OAuth2 error: {}", s),
GarrisonError::Network(s) => format!("Network error: {}", s),
GarrisonError::InvalidResponse(s) => format!("Invalid upstream response: {}", s),
GarrisonError::InvalidParam(s) => format!("Invalid parameter: {}", s),
GarrisonError::NotImplemented(s) => format!("Not implemented: {}", s),
GarrisonError::FirewallBlocked(s) => format!("Firewall blocked: {}", s),
GarrisonError::DisableService { service, until } => {
format!("Account disabled: service={}, until={:?}", service, until)
},
GarrisonError::NotSafe { reason } => {
format!("Second factor authentication required: {}", reason)
},
GarrisonError::InvalidStateTransition { from, to } => {
format!("Invalid state transition: {} -> {}", from, to)
},
GarrisonError::SmsRateLimitExceeded { window } => {
format!("SMS rate limit exceeded: {} window", window)
},
GarrisonError::SmsVerifyMaxAttempts => "SMS verification max attempts exceeded".to_string(),
GarrisonError::SmsCodeNotFound => "SMS verification code not found".to_string(),
GarrisonError::SmsChannelRecycled => "SMS channel recycled".to_string(),
GarrisonError::RateLimited { .. } => "Rate limited".to_string(),
#[cfg(feature = "email-verification")]
GarrisonError::EmailRateLimitExceeded { window } => {
format!("Email rate limit exceeded: {} window", window)
},
#[cfg(feature = "email-verification")]
GarrisonError::EmailVerifyMaxAttempts => {
"Email verification max attempts exceeded".to_string()
},
#[cfg(feature = "email-verification")]
GarrisonError::EmailCodeNotFound => "Email verification code not found".to_string(),
#[cfg(feature = "email-verification")]
GarrisonError::EmailChannelRecycled => "Email channel recycled".to_string(),
#[cfg(feature = "credit-metering")]
GarrisonError::CreditInsufficient {
tenant_id,
requested,
remaining,
} => {
format!(
"Credit insufficient: tenant={}, requested={}, remaining={}",
tenant_id, requested, remaining
)
},
GarrisonError::Exception(ex) => format!("Business exception[{}]: {}", ex.code, ex.message),
}
}
#[cfg(feature = "i18n-icu")]
pub mod icu_enhanced {
use crate::i18n::{current_locale, GarrisonLocale};
use chrono::{Datelike, Timelike};
fn formatter() -> trait_kit::i18n::I18nFormatter {
let tag = match current_locale() {
GarrisonLocale::Zh => "zh-CN",
GarrisonLocale::En => "en",
};
trait_kit::i18n::I18nFormatter::new(tag)
.expect("trait-kit I18nFormatter 对 zh-CN/en 恒可构造;若失败请报告 bug")
}
fn plural_to_str(debug: &str) -> &'static str {
match debug {
"Zero" => "zero",
"One" => "one",
"Two" => "two",
"Few" => "few",
"Many" => "many",
_ => "other",
}
}
pub fn plural_category(count: usize) -> &'static str {
match formatter().plural_category(count as u64) {
Ok(cat) => plural_to_str(&format!("{cat:?}")),
Err(_) => "other",
}
}
pub fn format_datetime_locale(dt: &chrono::DateTime<chrono::Utc>) -> String {
let date = formatter()
.format_date(dt.year(), dt.month() as u8, dt.day() as u8)
.expect("trait-kit format_date 对合法日历日期不应失败");
format!(
"{date} {:02}:{:02}:{:02}",
dt.hour(),
dt.minute(),
dt.second()
)
}
pub fn format_number_locale(n: i64) -> String {
formatter()
.format_number(n as f64)
.expect("trait-kit format_number 不应失败")
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::GarrisonError;
use crate::exception::GarrisonException;
#[test]
fn default_locale_is_en() {
let locale = GarrisonLocale::default();
assert_eq!(locale, GarrisonLocale::En);
}
#[test]
fn as_language_tag_returns_correct_tag() {
assert_eq!(GarrisonLocale::Zh.as_language_tag(), "zh-CN");
assert_eq!(GarrisonLocale::En.as_language_tag(), "en");
}
#[test]
fn current_locale_defaults_to_detected_locale_when_not_set() {
assert_eq!(current_locale(), detect_locale());
}
#[test]
fn set_locale_changes_current_and_restores_on_drop() {
let original = current_locale();
{
let _guard = set_locale(GarrisonLocale::En);
assert_eq!(current_locale(), GarrisonLocale::En);
}
assert_eq!(current_locale(), original);
}
#[test]
fn set_locale_supports_nesting() {
let original = current_locale();
{
let _g1 = set_locale(GarrisonLocale::En);
assert_eq!(current_locale(), GarrisonLocale::En);
{
let _g2 = set_locale(GarrisonLocale::Zh);
assert_eq!(current_locale(), GarrisonLocale::Zh);
}
assert_eq!(current_locale(), GarrisonLocale::En);
}
assert_eq!(current_locale(), original);
}
#[test]
fn set_locale_overrides_detection_and_pop_restores_detected() {
let detected = current_locale(); {
let _guard = set_locale(GarrisonLocale::En);
assert_eq!(current_locale(), GarrisonLocale::En);
}
assert_eq!(current_locale(), detected);
{
let _guard = set_locale(GarrisonLocale::Zh);
assert_eq!(current_locale(), GarrisonLocale::Zh);
}
assert_eq!(current_locale(), detected);
}
fn env_of<const N: usize>(
vars: [(&'static str, &'static str); N],
) -> impl Fn(&str) -> Option<String> {
move |key| {
vars.iter()
.find(|(k, _)| *k == key)
.map(|(_, v)| v.to_string())
}
}
#[test]
fn detect_chain_zh_cn_utf8_yields_zh() {
let getenv = env_of([("LANG", "zh_CN.UTF-8")]);
assert_eq!(detect_from(&getenv, None), GarrisonLocale::Zh);
}
#[test]
fn detect_chain_fr_fr_falls_back_to_en() {
let getenv = env_of([("LANG", "fr_FR.UTF-8")]);
assert_eq!(detect_from(&getenv, None), GarrisonLocale::En);
}
#[test]
fn detect_chain_c_and_posix_fall_back_to_en() {
let c = env_of([("LANG", "C")]);
assert_eq!(detect_from(&c, None), GarrisonLocale::En);
let posix = env_of([("LC_ALL", "POSIX")]);
assert_eq!(detect_from(&posix, None), GarrisonLocale::En);
let c_utf8 = env_of([("LANG", "C.UTF-8")]);
assert_eq!(detect_from(&c_utf8, None), GarrisonLocale::En);
}
#[test]
fn detect_chain_garrison_lang_takes_precedence() {
let en_overrides_zh = env_of([("GARRISON_LANG", "en_US"), ("LC_ALL", "zh_CN.UTF-8")]);
assert_eq!(detect_from(&en_overrides_zh, None), GarrisonLocale::En);
let zh_overrides_en = env_of([("GARRISON_LANG", "zh_CN"), ("LC_ALL", "en_US.UTF-8")]);
assert_eq!(detect_from(&zh_overrides_en, None), GarrisonLocale::Zh);
}
#[test]
fn detect_chain_lc_all_precedes_lang() {
let getenv = env_of([("LC_ALL", "zh_CN.UTF-8"), ("LANG", "en_US.UTF-8")]);
assert_eq!(detect_from(&getenv, None), GarrisonLocale::Zh);
}
#[test]
fn detect_chain_empty_value_skipped() {
let getenv = env_of([("GARRISON_LANG", " "), ("LANG", "zh_CN")]);
assert_eq!(detect_from(&getenv, None), GarrisonLocale::Zh);
}
#[test]
fn detect_chain_empty_falls_back_to_en() {
let none: fn(&str) -> Option<String> = |_key| None;
assert_eq!(detect_from(&none, None), GarrisonLocale::En);
}
#[test]
fn detect_chain_sys_locale_fallback() {
let none: fn(&str) -> Option<String> = |_key| None;
assert_eq!(detect_from(&none, Some("zh-Hans-CN")), GarrisonLocale::Zh);
assert_eq!(detect_from(&none, Some("en-US")), GarrisonLocale::En);
assert_eq!(detect_from(&none, Some("fr-FR")), GarrisonLocale::En);
let getenv = env_of([("LANG", "en_US.UTF-8")]);
assert_eq!(detect_from(&getenv, Some("zh-CN")), GarrisonLocale::En);
}
#[test]
fn detect_chain_normalizes_variants() {
let none: fn(&str) -> Option<String> = |_key| None;
for raw in ["zh", "zh_TW", "zh-Hant", "ZH-CN", "zh_SG@pinyin"] {
assert_eq!(
detect_from(&none, Some(raw)),
GarrisonLocale::Zh,
"raw={raw}"
);
}
for raw in ["en", "en_GB.UTF-8", "EN-us"] {
assert_eq!(
detect_from(&none, Some(raw)),
GarrisonLocale::En,
"raw={raw}"
);
}
}
#[test]
fn translate_error_zh_returns_chinese_message() {
let _guard = set_locale(GarrisonLocale::Zh);
let err = GarrisonError::NotLogin("请先登录".to_string());
let translated = translate_error(&err);
assert_eq!(translated, "未登录: 请先登录");
}
#[test]
fn translate_error_en_returns_english_message() {
let _guard = set_locale(GarrisonLocale::En);
let err = GarrisonError::NotLogin("please login first".to_string());
let translated = translate_error(&err);
assert_eq!(translated, "Not logged in: please login first");
}
#[test]
fn translate_error_zh_all_variants_match_hardcoded() {
let _guard = set_locale(GarrisonLocale::Zh);
let cases = vec![
(GarrisonError::NotLogin("a".into()), "未登录: a"),
(GarrisonError::NotPermission("a".into()), "无权限: a"),
(GarrisonError::NotRole("a".into()), "无角色: a"),
(GarrisonError::InvalidToken("a".into()), "Token 无效: a"),
(GarrisonError::ExpiredToken("a".into()), "Token 已过期: a"),
(GarrisonError::Dao("a".into()), "DAO 错误: a"),
(GarrisonError::Config("a".into()), "配置错误: a"),
(GarrisonError::Internal("a".into()), "内部错误: a"),
(GarrisonError::Session("a".into()), "会话错误: a"),
(GarrisonError::Annotation("a".into()), "注解错误: a"),
(GarrisonError::Context("a".into()), "上下文错误: a"),
(GarrisonError::OAuth2("a".into()), "OAuth2 错误: a"),
(GarrisonError::Network("a".into()), "网络错误: a"),
(GarrisonError::InvalidParam("a".into()), "参数无效: a"),
(GarrisonError::NotImplemented("a".into()), "未实现: a"),
];
for (err, expected) in cases {
assert_eq!(translate_error(&err), expected, "mismatch for {:?}", err);
}
}
#[test]
fn translate_error_en_all_variants_english() {
let _guard = set_locale(GarrisonLocale::En);
let cases = vec![
(GarrisonError::NotLogin("a".into()), "Not logged in: a"),
(
GarrisonError::NotPermission("a".into()),
"Permission denied: a",
),
(GarrisonError::NotRole("a".into()), "Role denied: a"),
(GarrisonError::InvalidToken("a".into()), "Invalid token: a"),
(GarrisonError::ExpiredToken("a".into()), "Token expired: a"),
(GarrisonError::Dao("a".into()), "DAO error: a"),
(GarrisonError::Config("a".into()), "Configuration error: a"),
(GarrisonError::Internal("a".into()), "Internal error: a"),
(GarrisonError::Session("a".into()), "Session error: a"),
(GarrisonError::Annotation("a".into()), "Annotation error: a"),
(GarrisonError::Context("a".into()), "Context error: a"),
(GarrisonError::OAuth2("a".into()), "OAuth2 error: a"),
(GarrisonError::Network("a".into()), "Network error: a"),
(
GarrisonError::InvalidParam("a".into()),
"Invalid parameter: a",
),
(
GarrisonError::NotImplemented("a".into()),
"Not implemented: a",
),
];
for (err, expected) in cases {
assert_eq!(translate_error(&err), expected, "mismatch for {:?}", err);
}
}
#[test]
fn translate_error_zh_exception_variant() {
let _guard = set_locale(GarrisonLocale::Zh);
let err = GarrisonError::Exception(Box::new(GarrisonException::new(-1, "请先登录")));
assert_eq!(translate_error(&err), "业务异常[-1]: 请先登录");
}
#[test]
fn translate_error_en_exception_variant() {
let _guard = set_locale(GarrisonLocale::En);
let err = GarrisonError::Exception(Box::new(GarrisonException::new(-1, "please login")));
assert_eq!(
translate_error(&err),
"Business exception[-1]: please login"
);
}
#[test]
fn locale_switch_is_isolated_per_scope() {
let original = current_locale();
{
let _g = set_locale(GarrisonLocale::En);
let err = GarrisonError::Dao("err".to_string());
assert_eq!(translate_error(&err), "DAO error: err");
}
let err = GarrisonError::Dao("err".to_string());
if original == GarrisonLocale::En {
assert_eq!(translate_error(&err), "DAO error: err");
} else {
assert_eq!(translate_error(&err), "DAO 错误: err");
}
}
#[test]
fn fallback_display_matches_hardcoded_english() {
let err = GarrisonError::NotLogin("test".to_string());
assert_eq!(fallback_display(&err), "Not logged in: test");
}
#[test]
fn fallback_display_all_variants() {
let cases: Vec<(GarrisonError, &str)> = vec![
(GarrisonError::NotLogin("x".into()), "Not logged in: x"),
(
GarrisonError::NotPermission("x".into()),
"Permission denied: x",
),
(GarrisonError::NotRole("x".into()), "Role denied: x"),
(GarrisonError::InvalidToken("x".into()), "Invalid token: x"),
(GarrisonError::ExpiredToken("x".into()), "Token expired: x"),
(GarrisonError::Dao("x".into()), "DAO error: x"),
(GarrisonError::Config("x".into()), "Configuration error: x"),
(GarrisonError::Internal("x".into()), "Internal error: x"),
(GarrisonError::Session("x".into()), "Session error: x"),
(GarrisonError::Annotation("x".into()), "Annotation error: x"),
(GarrisonError::Context("x".into()), "Context error: x"),
(GarrisonError::OAuth2("x".into()), "OAuth2 error: x"),
(GarrisonError::Network("x".into()), "Network error: x"),
(
GarrisonError::InvalidParam("x".into()),
"Invalid parameter: x",
),
(
GarrisonError::NotImplemented("x".into()),
"Not implemented: x",
),
(
GarrisonError::Exception(Box::new(GarrisonException::new(-1, "msg"))),
"Business exception[-1]: msg",
),
];
for (err, expected) in cases {
assert_eq!(fallback_display(&err), expected, "mismatch for {:?}", err);
}
}
#[test]
fn ensure_registered_is_idempotent_and_populates_registry() {
ensure_registered();
ensure_registered();
assert!(
lookup("zh-CN", "not-login", &[("detail", "x")]).is_some(),
"zh 注册后 lookup 应命中"
);
}
#[test]
fn translate_detail_zh_returns_translated_message() {
let _guard = set_locale(GarrisonLocale::Zh);
let msg = translate_detail("not-login", &[("detail", "请先登录")]);
assert_eq!(msg, "未登录: 请先登录");
}
#[test]
fn translate_detail_en_returns_translated_message() {
let _guard = set_locale(GarrisonLocale::En);
let msg = translate_detail("not-login", &[("detail", "please login")]);
assert_eq!(msg, "Not logged in: please login");
}
#[test]
fn translate_detail_missing_key_returns_key() {
let _guard = set_locale(GarrisonLocale::Zh);
let msg = translate_detail("nonexistent-key-xyz", &[]);
assert_eq!(msg, "nonexistent-key-xyz");
}
#[test]
fn loc_macro_uses_fallback_when_key_missing() {
let _guard = set_locale(GarrisonLocale::Zh);
let msg = loc!("nonexistent-key-xyz", "human readable fallback");
assert_eq!(msg, "human readable fallback");
}
#[test]
fn loc_macro_uses_string_fallback_when_key_missing() {
let _guard = set_locale(GarrisonLocale::Zh);
let msg = loc!("nonexistent-key-xyz", "owned fallback".to_string());
assert_eq!(msg, "owned fallback");
}
#[test]
fn loc_macro_empty_fallback_keeps_key() {
let _guard = set_locale(GarrisonLocale::Zh);
let msg = loc!("nonexistent-key-xyz", "");
assert_eq!(msg, "nonexistent-key-xyz");
}
#[test]
fn loc_macro_translated_key_ignores_fallback() {
let _guard = set_locale(GarrisonLocale::Zh);
let msg = loc!("sms-verify-max-attempts", "unused fallback");
assert_eq!(msg, "SMS 验证码尝试次数超限");
}
#[test]
fn translate_detail_no_args_translates_successfully() {
let _guard = set_locale(GarrisonLocale::Zh);
let msg = translate_detail("sms-verify-max-attempts", &[]);
assert_eq!(msg, "SMS 验证码尝试次数超限");
}
#[test]
fn translate_detail_multiple_args_zh() {
let _guard = set_locale(GarrisonLocale::Zh);
let msg = translate_detail(
"disable-service",
&[
("service", "default"),
("until", "Some(2026-01-01T00:00:00Z)"),
],
);
assert!(msg.contains("service=default"));
assert!(msg.contains("账号已被封禁"));
}
#[test]
fn translate_detail_multiple_args_en() {
let _guard = set_locale(GarrisonLocale::En);
let msg = translate_detail(
"invalid-state-transition",
&[("from", "Active"), ("to", "Closed")],
);
assert_eq!(msg, "Invalid state transition: Active -> Closed");
}
#[test]
fn translate_error_zh_token_revoked() {
let _guard = set_locale(GarrisonLocale::Zh);
let err = GarrisonError::TokenRevoked("reuse detected".to_string());
let translated = translate_error(&err);
assert_eq!(translated, "Token 已吊销: reuse detected");
}
#[test]
fn translate_error_en_token_revoked() {
let _guard = set_locale(GarrisonLocale::En);
let err = GarrisonError::TokenRevoked("reuse detected".to_string());
let translated = translate_error(&err);
assert_eq!(translated, "Token revoked: reuse detected");
}
#[test]
fn translate_error_zh_firewall_blocked() {
let _guard = set_locale(GarrisonLocale::Zh);
let err = GarrisonError::FirewallBlocked("black_path: /admin".to_string());
let translated = translate_error(&err);
assert_eq!(translated, "防火墙拦截: black_path: /admin");
}
#[test]
fn translate_error_en_firewall_blocked() {
let _guard = set_locale(GarrisonLocale::En);
let err = GarrisonError::FirewallBlocked("black_path: /admin".to_string());
let translated = translate_error(&err);
assert_eq!(translated, "Firewall blocked: black_path: /admin");
}
#[test]
fn translate_error_zh_disable_service() {
let _guard = set_locale(GarrisonLocale::Zh);
let until = chrono::DateTime::parse_from_rfc3339("2026-01-01T00:00:00Z")
.unwrap()
.with_timezone(&chrono::Utc);
let err = GarrisonError::DisableService {
service: "default".to_string(),
until: Some(until),
};
let translated = translate_error(&err);
assert!(translated.contains("service=default"));
assert!(translated.contains("账号已被封禁"));
}
#[test]
fn translate_error_en_disable_service() {
let _guard = set_locale(GarrisonLocale::En);
let err = GarrisonError::DisableService {
service: "oidc".to_string(),
until: None,
};
let translated = translate_error(&err);
assert!(translated.contains("service=oidc"));
assert!(translated.contains("Account disabled"));
}
#[test]
fn translate_error_zh_not_safe() {
let _guard = set_locale(GarrisonLocale::Zh);
let err = GarrisonError::NotSafe {
reason: "MFA_TOTP_REQUIRED".to_string(),
};
assert_eq!(translate_error(&err), "未完成二次认证:MFA_TOTP_REQUIRED");
}
#[test]
fn translate_error_en_not_safe() {
let _guard = set_locale(GarrisonLocale::En);
let err = GarrisonError::NotSafe {
reason: "WEBAUTHN_REQUIRED".to_string(),
};
assert_eq!(
translate_error(&err),
"Second factor authentication required: WEBAUTHN_REQUIRED"
);
}
#[test]
fn translate_error_zh_invalid_state_transition() {
let _guard = set_locale(GarrisonLocale::Zh);
let err = GarrisonError::InvalidStateTransition {
from: "Active".to_string(),
to: "Closed".to_string(),
};
assert_eq!(translate_error(&err), "非法状态转换:Active -> Closed");
}
#[test]
fn translate_error_en_invalid_state_transition() {
let _guard = set_locale(GarrisonLocale::En);
let err = GarrisonError::InvalidStateTransition {
from: "Pending".to_string(),
to: "Active".to_string(),
};
assert_eq!(
translate_error(&err),
"Invalid state transition: Pending -> Active"
);
}
#[test]
fn translate_error_zh_sms_rate_limit_exceeded() {
let _guard = set_locale(GarrisonLocale::Zh);
let err = GarrisonError::SmsRateLimitExceeded {
window: "hourly".to_string(),
};
assert_eq!(translate_error(&err), "SMS 限速超出: hourly 窗口");
}
#[test]
fn translate_error_en_sms_rate_limit_exceeded() {
let _guard = set_locale(GarrisonLocale::En);
let err = GarrisonError::SmsRateLimitExceeded {
window: "daily".to_string(),
};
assert_eq!(
translate_error(&err),
"SMS rate limit exceeded: daily window"
);
}
#[test]
fn translate_error_zh_sms_verify_max_attempts() {
let _guard = set_locale(GarrisonLocale::Zh);
let err = GarrisonError::SmsVerifyMaxAttempts;
assert_eq!(translate_error(&err), "SMS 验证码尝试次数超限");
}
#[test]
fn translate_error_en_sms_verify_max_attempts() {
let _guard = set_locale(GarrisonLocale::En);
let err = GarrisonError::SmsVerifyMaxAttempts;
assert_eq!(
translate_error(&err),
"SMS verification max attempts exceeded"
);
}
#[test]
fn translate_error_zh_sms_code_not_found() {
let _guard = set_locale(GarrisonLocale::Zh);
let err = GarrisonError::SmsCodeNotFound;
assert_eq!(translate_error(&err), "SMS 验证码不存在");
}
#[test]
fn translate_error_en_sms_code_not_found() {
let _guard = set_locale(GarrisonLocale::En);
let err = GarrisonError::SmsCodeNotFound;
assert_eq!(translate_error(&err), "SMS verification code not found");
}
#[test]
fn translate_error_zh_sms_channel_recycled() {
let _guard = set_locale(GarrisonLocale::Zh);
let err = GarrisonError::SmsChannelRecycled;
assert_eq!(translate_error(&err), "SMS 通道已回收");
}
#[test]
fn translate_error_en_sms_channel_recycled() {
let _guard = set_locale(GarrisonLocale::En);
let err = GarrisonError::SmsChannelRecycled;
assert_eq!(translate_error(&err), "SMS channel recycled");
}
#[test]
fn fallback_display_new_variants() {
let until = chrono::DateTime::parse_from_rfc3339("2026-01-01T00:00:00Z")
.unwrap()
.with_timezone(&chrono::Utc);
let cases: Vec<(GarrisonError, String)> = vec![
(
GarrisonError::TokenRevoked("x".into()),
"Token revoked: x".to_string(),
),
(
GarrisonError::FirewallBlocked("x".into()),
"Firewall blocked: x".to_string(),
),
(
GarrisonError::DisableService {
service: "default".into(),
until: Some(until),
},
format!("Account disabled: service=default, until={:?}", Some(until)),
),
(
GarrisonError::DisableService {
service: "oidc".into(),
until: None,
},
"Account disabled: service=oidc, until=None".to_string(),
),
(
GarrisonError::NotSafe { reason: "r".into() },
"Second factor authentication required: r".to_string(),
),
(
GarrisonError::InvalidStateTransition {
from: "A".into(),
to: "B".into(),
},
"Invalid state transition: A -> B".to_string(),
),
(
GarrisonError::SmsRateLimitExceeded { window: "w".into() },
"SMS rate limit exceeded: w window".to_string(),
),
(
GarrisonError::SmsVerifyMaxAttempts,
"SMS verification max attempts exceeded".to_string(),
),
(
GarrisonError::SmsCodeNotFound,
"SMS verification code not found".to_string(),
),
(
GarrisonError::SmsChannelRecycled,
"SMS channel recycled".to_string(),
),
];
for (err, expected) in cases {
assert_eq!(fallback_display(&err), expected, "mismatch for {:?}", err);
}
}
#[test]
fn error_to_key_args_all_variants() {
let until = chrono::Utc::now();
#[cfg_attr(
not(any(feature = "email-verification", feature = "credit-metering")),
allow(unused_mut)
)]
let mut cases: Vec<GarrisonError> = vec![
GarrisonError::NotLogin("a".into()),
GarrisonError::NotPermission("a".into()),
GarrisonError::NotRole("a".into()),
GarrisonError::InvalidToken("a".into()),
GarrisonError::TokenRevoked("a".into()),
GarrisonError::ExpiredToken("a".into()),
GarrisonError::Dao("a".into()),
GarrisonError::Config("a".into()),
GarrisonError::Internal("a".into()),
GarrisonError::Session("a".into()),
GarrisonError::Annotation("a".into()),
GarrisonError::Context("a".into()),
GarrisonError::OAuth2("a".into()),
GarrisonError::Network("a".into()),
GarrisonError::InvalidResponse("a".into()),
GarrisonError::InvalidParam("a".into()),
GarrisonError::NotImplemented("a".into()),
GarrisonError::FirewallBlocked("a".into()),
GarrisonError::DisableService {
service: "s".into(),
until: Some(until),
},
GarrisonError::NotSafe { reason: "r".into() },
GarrisonError::InvalidStateTransition {
from: "f".into(),
to: "t".into(),
},
GarrisonError::SmsRateLimitExceeded { window: "w".into() },
GarrisonError::SmsVerifyMaxAttempts,
GarrisonError::SmsCodeNotFound,
GarrisonError::SmsChannelRecycled,
GarrisonError::RateLimited {
retry_after_secs: 30,
},
GarrisonError::Exception(Box::new(GarrisonException::new(-1, "msg"))),
];
#[cfg(feature = "email-verification")]
cases.extend([
GarrisonError::EmailRateLimitExceeded { window: "w".into() },
GarrisonError::EmailVerifyMaxAttempts,
GarrisonError::EmailCodeNotFound,
GarrisonError::EmailChannelRecycled,
]);
#[cfg(feature = "credit-metering")]
cases.push(GarrisonError::CreditInsufficient {
tenant_id: 1,
requested: 2,
remaining: 3,
});
for err in cases {
let (key, _args) = error_to_key_args(&err);
assert!(!key.is_empty(), "key 不应为空: {:?}", err);
}
}
#[test]
#[cfg_attr(
not(any(feature = "email-verification", feature = "credit-metering")),
allow(unused_mut)
)]
fn error_to_key_args_feature_variants_hit_ftl_keys() {
let _guard = set_locale(GarrisonLocale::En);
let mut cases: Vec<(GarrisonError, &str)> = vec![(
GarrisonError::RateLimited {
retry_after_secs: 30,
},
"rate-limited",
)];
#[cfg(feature = "email-verification")]
cases.extend([
(
GarrisonError::EmailRateLimitExceeded {
window: "hourly".into(),
},
"email-rate-limit-exceeded",
),
(
GarrisonError::EmailVerifyMaxAttempts,
"email-verify-max-attempts",
),
(GarrisonError::EmailCodeNotFound, "email-code-not-found"),
(
GarrisonError::EmailChannelRecycled,
"email-channel-recycled",
),
]);
#[cfg(feature = "credit-metering")]
cases.push((
GarrisonError::CreditInsufficient {
tenant_id: 1,
requested: 2,
remaining: 3,
},
"credit-insufficient",
));
for (err, expected_key) in &cases {
let (key, _args) = error_to_key_args(err);
assert_eq!(key, *expected_key, "FTL 键失配: {err:?}");
let translated = translate_error(err);
assert_ne!(
translated, *expected_key,
"应命中 FTL 翻译而非回退 key 本身: {err:?}"
);
assert!(!translated.is_empty());
}
}
#[test]
fn fallback_display_rate_limited_and_feature_variants() {
assert_eq!(
fallback_display(&GarrisonError::RateLimited {
retry_after_secs: 30
}),
"Rate limited"
);
#[cfg(feature = "email-verification")]
{
assert_eq!(
fallback_display(&GarrisonError::EmailRateLimitExceeded {
window: "hourly".into()
}),
"Email rate limit exceeded: hourly window"
);
assert_eq!(
fallback_display(&GarrisonError::EmailVerifyMaxAttempts),
"Email verification max attempts exceeded"
);
assert_eq!(
fallback_display(&GarrisonError::EmailCodeNotFound),
"Email verification code not found"
);
assert_eq!(
fallback_display(&GarrisonError::EmailChannelRecycled),
"Email channel recycled"
);
}
#[cfg(feature = "credit-metering")]
assert_eq!(
fallback_display(&GarrisonError::CreditInsufficient {
tenant_id: 1,
requested: 2,
remaining: 3
}),
"Credit insufficient: tenant=1, requested=2, remaining=3"
);
}
#[test]
fn translate_error_zh_invalid_response() {
let _guard = set_locale(GarrisonLocale::Zh);
let err = GarrisonError::InvalidResponse("JSON 解析失败".to_string());
assert_eq!(translate_error(&err), "上游响应无效: JSON 解析失败");
}
#[test]
fn translate_error_en_invalid_response() {
let _guard = set_locale(GarrisonLocale::En);
let err = GarrisonError::InvalidResponse("missing access_token field".to_string());
assert_eq!(
translate_error(&err),
"Invalid upstream response: missing access_token field"
);
}
#[test]
fn fallback_display_invalid_response() {
let err = GarrisonError::InvalidResponse("parse error".into());
assert_eq!(
fallback_display(&err),
"Invalid upstream response: parse error"
);
}
#[test]
fn lookup_en_returns_registered_entry() {
ensure_registered();
assert!(
lookup("en", "not-login", &[("detail", "x")]).is_some(),
"en 注册后 lookup 应命中"
);
}
#[test]
fn translate_detail_known_key_returns_translation() {
let _guard = set_locale(GarrisonLocale::En);
let result = translate_detail("not-login", &[("detail", "test")]);
assert!(result.contains("test"), "应包含参数值: {}", result);
}
#[test]
fn translate_detail_unknown_key_returns_key() {
let _guard = set_locale(GarrisonLocale::En);
let result = translate_detail("nonexistent-key-xyz", &[]);
assert_eq!(result, "nonexistent-key-xyz");
}
#[test]
fn translate_detail_known_key_no_args() {
let _guard = set_locale(GarrisonLocale::Zh);
let result = translate_detail("sms-verify-max-attempts", &[]);
assert!(!result.is_empty());
assert_ne!(result, "sms-verify-max-attempts", "应返回翻译而非 key 本身");
}
#[cfg(feature = "i18n-icu")]
#[test]
fn icu_plural_category_en_one() {
let _guard = set_locale(GarrisonLocale::En);
let cat = icu_enhanced::plural_category(1);
assert_eq!(cat, "one");
}
#[cfg(feature = "i18n-icu")]
#[test]
fn icu_plural_category_en_other() {
let _guard = set_locale(GarrisonLocale::En);
let cat = icu_enhanced::plural_category(2);
assert_eq!(cat, "other");
}
#[cfg(feature = "i18n-icu")]
#[test]
fn icu_format_number_en() {
let _guard = set_locale(GarrisonLocale::En);
let formatted = icu_enhanced::format_number_locale(1_000_000);
assert!(formatted.contains("1"), "应包含数字 1: {}", formatted);
}
#[cfg(feature = "i18n-icu")]
#[test]
fn icu_format_datetime_en() {
let _guard = set_locale(GarrisonLocale::En);
let dt = chrono::DateTime::parse_from_rfc3339("2026-01-01T00:00:00Z")
.unwrap()
.with_timezone(&chrono::Utc);
let formatted = icu_enhanced::format_datetime_locale(&dt);
assert!(!formatted.is_empty(), "日期格式化不应为空");
assert!(formatted.contains("2026"), "应包含年份: {}", formatted);
}
#[test]
fn parse_keyed_detail_accepts_digit_keys() {
let (key, args) = parse_keyed_detail("config-password-hash-argon2-param-invalid::t_cost")
.expect("含数字键应被解析");
assert_eq!(key, "config-password-hash-argon2-param-invalid");
assert_eq!(args, [("arg0", "t_cost".to_string())]);
assert!(
parse_keyed_detail("cache-l1-init::").is_some(),
"纯数字段也应可解析"
);
}
#[test]
fn parse_keyed_detail_still_rejects_prose() {
assert!(parse_keyed_detail("这是一句中文::包含分隔符").is_none());
assert!(parse_keyed_detail("not a key::with spaces").is_none());
assert!(parse_keyed_detail("MixedCase::value").is_none());
}
#[test]
fn translate_detail_renders_digit_key_from_catalog() {
let rendered = translate_detail(
"config-password-hash-argon2-param-invalid",
&[("arg0", "t_cost")],
);
assert!(rendered.contains("t_cost"), "应渲染参数: {rendered}");
assert!(
!rendered.contains("config-password-hash"),
"不得回退原始 key: {rendered}"
);
}
}