#localization #fluent #generate-static

fluent-static-codegen

Automatically generate Rust functions from Fluent message files for streamlined localization in Rust applications

13 releases (5 breaking)

0.6.0 Oct 23, 2024
0.5.0 Aug 19, 2024
0.4.1 Aug 16, 2024
0.3.2 Aug 8, 2024
0.1.0 May 24, 2024

#389 in Internationalization (i18n)

Download history 458/week @ 2024-08-02 191/week @ 2024-08-09 322/week @ 2024-08-16 53/week @ 2024-08-23 20/week @ 2024-08-30 14/week @ 2024-09-06 66/week @ 2024-09-13 69/week @ 2024-09-20 66/week @ 2024-09-27 73/week @ 2024-10-04 65/week @ 2024-10-11 153/week @ 2024-10-18 38/week @ 2024-10-25 32/week @ 2024-11-01 36/week @ 2024-11-08

261 downloads per month
Used in 2 crates (via fluent-static-macros)

MIT license

94KB
2.5K SLoC

fluent-static-codegen

Latest version

Part of fluent-static library providing simple to use, yet efficient way to add localization to Rust projects with Fluent Localization System.

fluent-static is inspired by and partially based on awesome Fluent-rs project.

Usage

Cargo dependencies

Add fluent-static-codegen to project's build-dependencies section

[build-dependencies]
fluent-static-codegen = "*"

Build script

The fluent-static-codegen requires Cargo Build Script to operate.



use std::{env, fs, path::PathBuf};

use fluent_static_codegen::MessageBundleBuilder;

fn resources_base_dir() -> PathBuf {
    PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("'CARGO_MANIFEST_DIR' not set"))
        .join("l10n")
}

fn output_dir() -> PathBuf {
    let out =
        PathBuf::from(env::var_os("OUT_DIR").expect("'OUT_DIR' not set")).join("generated/fluent");
    if !out.exists() {
        fs::create_dir_all(&out).unwrap();
    }
    out
}

pub fn main() {
    println!("cargo::rerun-if-changed=l10n/");

    let bundle = MessageBundleBuilder::new("Messages")
        .set_default_language("en")
        .expect("Default language should be valid language identifier")
        .set_resources_dir(resources_base_dir())
        .add_resource("en", "messages-en.ftl")
        .expect("Resource file should be valid Fluent resource")
        .add_resource("it", "messages-it.ftl")
        .expect("Resource file should be valid Fluent resource")
        .build()
        .unwrap();

    bundle
        .write_to_file(output_dir().join("messages.rs"))
        .expect("Output directory should exist and be writeable to save generated code");
}
  

Registering Custom Fluent Functions

TBD

License

This project is licensed under MIT license. Feel free to use, modify, and distribute it as per the license conditions.


Dependencies

~1.5–2MB
~41K SLoC