i18n-runtime** ๐
๐ i18n-runtime
Effortless, fast, and type-safe internationalization (i18n) for Rust apps.
Supports both runtime JSON loading and compile-time PHF maps (when used with i18n-gen).
โจ Features
- โ
Runtime JSON mode โ load translations directly from
locales/*.json - โก Compile-time PHF mode โ use generated Rust maps for O(1) lookups
- ๐ Fallback logic โ handles tags like
en-IN-BR โ en-IN โ en - ๐ Type-safe keys โ auto-generated
MessageKeyenum - ๐ Zero-cost lookups โ powered by
phf
๐ฆ Installation
Add to your Cargo.toml:
[]
= "0.1"
# Only required if you use generated PHF mode
= "0.11"
๐ Quick Start
Runtime JSON mode
- Define
messages.schema.json:
- Add a locale file
locales/en.json:
- Use it in code:
use ;
Output:
en => Welcome!
hi-IN => เคธเฅเคตเคพเคเคค เคนเฅ!
fr => Welcome!
Generated PHF mode
- Install i18n-gen:
- Run the generator:
This creates:
src/generated_i18n/generated_keys.rs # MessageKey enum
src/generated_i18n/locales/EN.rs # en.json โ phf::Map
src/generated_i18n/locales/HI_IN.rs # hi-IN.json โ phf::Map
src/generated_i18n/mod.rs # registry
- Use in code:
// include generated files
include!;
use ;
๐ Fallback Logic
en-IN-BR โ en-IN โ en โ fallback locale.
๐งฉ Ecosystem
- i18n-runtime โ runtime API (this crate)
- i18n-gen โ generator CLI for compile-time PHF maps
๐ License
MIT