Un servizio Rust legge lo stesso JSON dalla CDN che leggono tutti gli SDK: bastano reqwest e serde_json, e i nuovi testi arrivano senza ricompilare.
Rust
- SDK
- None needed
- Source
- Pulled JSON files
lefine · serde_json
01src/i18n.rs
use 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)?)}02src/main.rs
use axum::{extract::State, routing::get, Json, Router};use std::{collections::HashMap, sync::Arc};use tokio::sync::RwLock;type Messages = Arc<RwLock<HashMap<String, serde_json::Value>>>;async fn greeting(State(messages): State<Messages>) -> Json<serde_json::Value> { let messages = messages.read().await; let hello = messages .get("greeting") .and_then(|ns| ns.get("hello")) .and_then(|v| v.as_str()) .unwrap_or("Hello"); Json(serde_json::json!({ "message": hello }))}#[tokio::main]async fn main() { let messages: Messages = Arc::new(RwLock::new( load_messages("en").unwrap_or_default(), )); let app = Router::new() .route("/greeting", get(greeting)) .with_state(messages); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.unwrap();}ns.get("hello")Hello
Un handler axum che legge i messaggi dalla CDN
I messaggi stanno dietro un RwLock e vengono aggiornati da un timer.
Usa Lefine da Rust
- 01
Aggiungi i crate HTTP e JSON
Bastano reqwest e serde_json: non c'è nessun crate Lefine da installare.
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
Recupera un namespace
La CDN serve un file JSON per namespace e lingua e risponde sempre 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
Aggiorna in background
Metti la mappa in cache nel processo e rileggila ogni 60 secondi, in linea con il max-age della 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; } }});
Cosa ottengono i servizi Rust
- Nessun SDK richiesto — e non esiste un crate Rust, quindi non c'è nulla da aspettare
- JSON semplice su HTTPS, analizzato con serde_json
- Funziona con axum, actix-web, Rocket o un semplice binario
- I nuovi testi arrivano in circa 60 secondi senza ricompilare
- L'API REST copre tutto ciò che la CDN non copre
- Convive con rust-i18n se lo usi già per i cataloghi locali
Get started
Inizia gratis.
Localizza il tuo servizio Rust
Crea un progetto, pubblica la prima lingua e leggila da Rust via HTTPS.