Skip to content
Rust

Rust i18n

There is no Better I18N 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.

Setup

Use Better I18N from Rust

Add the HTTP and JSON crates

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

Cargo.toml
[dependencies]
reqwest = { version = "0.12", features = ["json"] }
serde_json = "1"
tokio = { version = "1", features = ["full"] }

Fetch a namespace

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

src/i18n.rs
use std::collections::HashMap;

/// The CDN serves one JSON file per namespace, per locale:
///   https://cdn.better-i18n.com/{org}/{project}/{locale}/{namespace}.json
/// It always answers 200 — an unknown locale returns `{}` rather than an error.
pub async fn fetch_messages(
    locale: &str,
    namespace: &str,
) -> reqwest::Result<HashMap<String, serde_json::Value>> {
    let url = format!(
        "https://cdn.better-i18n.com/your-org/your-project/{locale}/{namespace}.json"
    );

    reqwest::get(&url).await?.json().await
}

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;

// Cache in process and re-read on an interval: the CDN sets
// `Cache-Control: max-age=60`, so a 60s refresh matches it exactly.
tokio::spawn(async move {
    let mut ticker = tokio::time::interval(Duration::from_secs(60));
    loop {
        ticker.tick().await;
        if let Ok(next) = fetch_messages("en", "common").await {
            *messages.write().await = next;
        }
    }
});
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
Example

An axum handler reading CDN messages

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

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.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(
        fetch_messages("en", "common").await.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();
}

Other frameworks

Get started

Localize your Rust service

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