Skip to content
Rust

Rust i18n

There is no Lefine crate. A Rust service reads the same CDN JSON every SDK reads — reqwest and serde_json are enough, and new copy ships without a rebuild.

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

An axum handler reading CDN messages

Messages live behind an RwLock and are refreshed on a timer.

01/Setup

Use Lefine from Rust

  1. 01

    Add the HTTP and JSON crates

    reqwest and serde_json are all you need — there is no Lefine crate to install.

    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"] }
  2. 02

    Fetch a namespace

    The CDN serves one JSON file per namespace and locale, and always answers 200.

    src/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)?)}
  3. 03

    Refresh in the background

    Cache the map in process and re-read it every 60 seconds, matching the CDN's max-age.

    src/main.rs
    use 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;        }    }});
02/Capabilities

What Rust services get

  • No SDK required — and no Rust crate exists, so there is nothing to wait for
  • Plain JSON over HTTPS, parsed with serde_json
  • Works with axum, actix-web, Rocket or a plain binary
  • New copy arrives in about 60 seconds without a rebuild
  • The REST API covers everything the CDN does not
  • Sits alongside rust-i18n if you already use it for local catalogs
Get started

Start free.

Localize your Rust service

Create a project, publish your first locale, and read it from Rust over HTTPS.