diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-17 01:05:24 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-17 01:05:24 +0800 |
| commit | 1957ba706f62a82a9f60df1f28e23de5fa8bb319 (patch) | |
| tree | fae07ade0aba30a1d0ad66344e4d1f61116f2074 /shakehand-test | |
| parent | c9685671512b05ec9b4caba2b7079382120cce1c (diff) | |
Implement fallback chain resolution for translation keys
Diffstat (limited to 'shakehand-test')
| -rw-r--r-- | shakehand-test/Cargo.toml | 15 | ||||
| -rw-r--r-- | shakehand-test/locale/test_fallback.toml | 13 | ||||
| -rw-r--r-- | shakehand-test/src/lib.rs | 80 |
3 files changed, 107 insertions, 1 deletions
diff --git a/shakehand-test/Cargo.toml b/shakehand-test/Cargo.toml index 3f20603..98d28f8 100644 --- a/shakehand-test/Cargo.toml +++ b/shakehand-test/Cargo.toml @@ -5,3 +5,18 @@ edition = "2024" [dependencies] shakehand.workspace = true + +[package.metadata.shakehand] +# Fallback chain: when the specified language does not exist, how to fall back the language +fallback.other = "en" +fallback.zh_HK = "zh_CN" # Hong Kong Traditional -> Simplified Chinese +fallback.zh_TW = "zh_CN" # Taiwan Traditional -> Simplified Chinese +fallback.zh_CN = "en" # Simplified Chinese -> English +fallback.vi = "zh_CN" # Vietnamese -> Simplified Chinese +fallback.ms = "id" # Malay -> Indonesian +fallback.it = "fr" # Italian -> French +fallback.pt = "es" # Portuguese -> Spanish +fallback.sv = "no" # Swedish -> Norwegian +fallback.no = "da" # Norwegian -> Danish +fallback.da = "sv" # Danish -> Swedish +fallback.tr = "ar" # Turkish -> Arabic diff --git a/shakehand-test/locale/test_fallback.toml b/shakehand-test/locale/test_fallback.toml new file mode 100644 index 0000000..ddcf627 --- /dev/null +++ b/shakehand-test/locale/test_fallback.toml @@ -0,0 +1,13 @@ +[en] +hello = "Hello" +goodbye = "Goodbye" + +[zh_CN] +hello = "你好" + +[fr] +hello = "Bonjour" +goodbye = "Au revoir" + +[it] +goodbye = "Arrivederci" diff --git a/shakehand-test/src/lib.rs b/shakehand-test/src/lib.rs index c290533..8c60441 100644 --- a/shakehand-test/src/lib.rs +++ b/shakehand-test/src/lib.rs @@ -1,9 +1,18 @@ #[cfg(test)] mod test { use crate::locale::{Global, Languages, set_lang}; + use std::sync::{Mutex, MutexGuard}; + + /// Global lock to serialize tests that mutate `__SHAKE_HAND_LANG`. + /// Without this, parallel test execution causes races on the shared global atomic state. + static LANG_LOCK: Mutex<()> = Mutex::new(()); + fn lock_lang() -> MutexGuard<'static, ()> { + LANG_LOCK.lock().unwrap() + } #[test] fn test_english() { + let _lock = lock_lang(); set_lang(Languages::en); assert_eq!(Global::world(), "world"); assert_eq!(Global::greeting("Alice"), "Hello, Alice!"); @@ -13,6 +22,7 @@ mod test { #[test] fn test_chinese() { + let _lock = lock_lang(); set_lang(Languages::zh_CN); assert_eq!(Global::world(), "世界"); assert_eq!(Global::greeting("小明"), "你好,小明!"); @@ -22,6 +32,7 @@ mod test { #[test] fn test_japanese() { + let _lock = lock_lang(); set_lang(Languages::ja); assert_eq!(Global::world(), "世界"); assert_eq!(Global::greeting("田中"), "こんにちは、田中!"); @@ -31,6 +42,7 @@ mod test { #[test] fn test_korean() { + let _lock = lock_lang(); set_lang(Languages::ko); assert_eq!(Global::world(), "세계"); assert_eq!(Global::greeting("철수"), "안녕하세요, 철수님!"); @@ -40,6 +52,7 @@ mod test { #[test] fn test_french() { + let _lock = lock_lang(); set_lang(Languages::fr); assert_eq!(Global::world(), "monde"); assert_eq!(Global::greeting("Marie"), "Bonjour, Marie !"); @@ -49,6 +62,7 @@ mod test { #[test] fn test_german() { + let _lock = lock_lang(); set_lang(Languages::de); assert_eq!(Global::world(), "Welt"); assert_eq!(Global::greeting("Hans"), "Hallo, Hans!"); @@ -58,6 +72,7 @@ mod test { #[test] fn test_spanish() { + let _lock = lock_lang(); set_lang(Languages::es); assert_eq!(Global::world(), "mundo"); assert_eq!(Global::greeting("Carlos"), "¡Hola, Carlos!"); @@ -67,6 +82,7 @@ mod test { #[test] fn test_russian() { + let _lock = lock_lang(); set_lang(Languages::ru); assert_eq!(Global::world(), "мир"); assert_eq!(Global::greeting("Анна"), "Привет, Анна!"); @@ -76,6 +92,7 @@ mod test { #[test] fn test_arabic() { + let _lock = lock_lang(); set_lang(Languages::ar); assert_eq!(Global::world(), "عالم"); assert_eq!(Global::greeting("أحمد"), "مرحبًا، أحمد!"); @@ -85,6 +102,7 @@ mod test { #[test] fn test_portuguese() { + let _lock = lock_lang(); set_lang(Languages::pt); assert_eq!(Global::world(), "mundo"); assert_eq!(Global::greeting("João"), "Olá, João!"); @@ -93,7 +111,67 @@ mod test { } #[test] - fn test_parameterless_translation_as_param() { + fn test_fallback_zh_hk_to_zh_cn() { + let _lock = lock_lang(); + set_lang(Languages::zh_HK); + assert_eq!(Global::world(), "世界"); + assert_eq!(Global::greeting("小明"), "你好,小明!"); + assert_eq!(Global::farewell("小红"), "再见,小红!"); + assert_eq!(Global::thanks(), "谢谢!"); + } + + #[test] + fn test_fallback_zh_tw_to_zh_cn() { + let _lock = lock_lang(); + set_lang(Languages::zh_TW); + assert_eq!(Global::world(), "世界"); + assert_eq!(Global::greeting("小明"), "你好,小明!"); + } + + #[test] + fn test_fallback_vi_to_zh_cn() { + let _lock = lock_lang(); + set_lang(Languages::vi); + assert_eq!(Global::world(), "世界"); + assert_eq!(Global::greeting("小明"), "你好,小明!"); + } + + #[test] + fn test_fallback_ms_to_id_to_en() { + let _lock = lock_lang(); + set_lang(Languages::ms); + assert_eq!(Global::world(), "world"); + assert_eq!(Global::greeting("Alice"), "Hello, Alice!"); + } + + #[test] + fn test_fallback_it_to_fr() { + let _lock = lock_lang(); + set_lang(Languages::it); + assert_eq!(Global::world(), "monde"); + assert_eq!(Global::greeting("Marie"), "Bonjour, Marie !"); + } + + #[test] + fn test_fallback_pt_to_es() { + let _lock = lock_lang(); + // pt has direct translations, so fallback should not be triggered + set_lang(Languages::pt); + assert_eq!(Global::world(), "mundo"); + assert_eq!(Global::greeting("João"), "Olá, João!"); + } + + #[test] + fn test_fallback_tr_to_ar() { + let _lock = lock_lang(); + set_lang(Languages::tr); + assert_eq!(Global::world(), "عالم"); + assert_eq!(Global::greeting("أحمد"), "مرحبًا، أحمد!"); + } + + #[test] + fn test_fallback_parameterless_translation_as_param() { + let _lock = lock_lang(); set_lang(Languages::en); let greeting = Global::greeting(Global::world()); assert_eq!(greeting, "Hello, world!"); |
