跳转至主要内容
Rust

Rust 国际化

没有 Lefine 的 crate。Rust 服务读取的是与所有 SDK 相同的 CDN JSON——reqwest 和 serde_json 就够了,新文案无需重新构建。

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

一个读取 CDN 消息的 axum 处理函数

消息保存在 RwLock 后面,并由定时器刷新。

01/Setup

在 Rust 中使用 Lefine

  1. 01

    添加 HTTP 和 JSON crate

    只需 reqwest 和 serde_json——没有需要安装的 Lefine crate。

    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

    获取一个命名空间

    CDN 为每个命名空间和语言提供一个 JSON 文件,并且总是返回 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

    在后台刷新

    在进程内缓存该映射,并每 60 秒重新读取一次,与 CDN 的 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

Rust 服务能得到什么

  • 无需 SDK——也不存在 Rust crate,因此无需等待
  • 通过 HTTPS 传输的普通 JSON,用 serde_json 解析
  • 适用于 axum、actix-web、Rocket 或普通二进制程序
  • 新文案约 60 秒内到达,无需重新构建
  • REST API 覆盖 CDN 不提供的一切
  • 如果你已用 rust-i18n 管理本地目录,两者可以共存
Get started

免费开始。

为你的 Rust 服务做本地化

创建项目、发布第一个语言,然后在 Rust 中通过 HTTPS 读取。