Lefine gebruiken vanuit Rust
- 01
Voeg de HTTP- en JSON-crates toe
reqwest en serde_json zijn genoeg — er is geen Lefine-crate om te installeren.
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
Haal een namespace op
De CDN levert één JSON-bestand per namespace en taal, en antwoordt altijd met 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
Ververs op de achtergrond
Cache de map in het proces en lees hem elke 60 seconden opnieuw, gelijk aan de max-age van de 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; } }});