Crates.io | fluent-static-codegen |
lib.rs | fluent-static-codegen |
version | 0.6.0 |
source | src |
created_at | 2024-05-24 10:19:57.367497 |
updated_at | 2024-10-23 17:07:19.352374 |
description | Automatically generate Rust functions from Fluent message files for streamlined localization in Rust applications. |
homepage | https://github.com/zaytsev/fluent-static |
repository | https://github.com/zaytsev/fluent-static |
max_upload_size | |
id | 1250903 |
size | 65,791 |
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.
Add fluent-static-codegen
to project's build-dependencies
section
[build-dependencies]
fluent-static-codegen = "*"
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");
}
TBD
This project is licensed under MIT license. Feel free to use, modify, and distribute it as per the license conditions.