Usa Lefine desde Rust
- 01
Añade los crates de HTTP y JSON
Bastan reqwest y serde_json: no hay ningún crate de Lefine que instalar.
Cargo.toml# First: npx lefine strings pull# It writes ./locales/en.json, ./locales/de.json, ... from your published strings.[dependencies]serde_json = "1"tokio = { version = "1", features = ["full"] } - 02
Obtén un namespace
La CDN sirve un archivo JSON por namespace e idioma, y siempre responde 200.
src/i18n.rsuse std::collections::HashMap;/// `npx lefine strings pull` writes one JSON file per locale:/// ./locales/{locale}.json -> { "namespace": { "key": "text" } }pub fn load_messages( locale: &str,) -> std::io::Result<HashMap<String, serde_json::Value>> { let path = format!("locales/{locale}.json"); let raw = std::fs::read_to_string(path)?; Ok(serde_json::from_str(&raw)?)} - 03
Actualiza en segundo plano
Cachea el mapa en el proceso y vuélvelo a leer cada 60 segundos, igual que el max-age de la CDN.
src/main.rsuse std::time::Duration;// Keep the strings in process and re-read the files on an interval.// Run `npx lefine strings pull` on deploy (or on a schedule) to refresh them.tokio::spawn(async move { let mut ticker = tokio::time::interval(Duration::from_secs(60)); loop { ticker.tick().await; if let Ok(next) = load_messages("en") { *messages.write().await = next; } }});