From 1957ba706f62a82a9f60df1f28e23de5fa8bb319 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 17 Jun 2026 01:05:24 +0800 Subject: Implement fallback chain resolution for translation keys --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'README.md') diff --git a/README.md b/README.md index e4ea242..628daed 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,31 @@ You can also pass the return value of another translation as a parameter, since let greeting = Global::greeting(Global::world()); ``` +## 5. Fallback Chain (Optional) + +You can configure a **fallback chain** in your `Cargo.toml` under `[package.metadata.shakehand]`. + +When a language has no translation for a key, the system will automatically walk the chain + to find the nearest language that has one. + +```toml +[package.metadata.shakehand] +# For any language not explicitly listed, fall back to English +fallback.other = "en" + +# Specific fallback rules +fallback.zh_HK = "zh_CN" # Hong Kong Trad -> Simplified Chinese +fallback.zh_TW = "zh_CN" # Taiwan Trad -> Simplified Chinese +fallback.zh_CN = "en" # Simplified Chinese -> English +fallback.it = "fr" # Italian -> French +``` + +The chain resolution is **at runtime via a generated `FallbackSolver`**: +- Each language has a `try_fallback_once()` step defined in a compile-time generated match. +- Translation functions loop through the chain until a language with the value is found. +- `fallback.other` is the ultimate fallback (falls back to `locale!`'s `fallback` parameter if unset). +- All languages in the chain are automatically added to the `Languages` enum, even if they + have no translations in the locale files. # Contributing -- cgit