#localization #fluent #generate-static

macro fluent-static-macros

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

1 unstable release

0.1.0 Oct 23, 2024

#32 in #generate-static

Download history 79/week @ 2024-10-17 60/week @ 2024-10-24 18/week @ 2024-10-31 6/week @ 2024-11-07

163 downloads per month
Used in fluent-static

MIT license

78KB
2K SLoC

fluent-static-macros

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 example

use fluent_static::value::Value;
use fluent_static::message_bundle;

#[message_bundle(
    resources = [
        ("l10n/simple-en.ftl", "en"),
        ("l10n/extra-en.ftl", "en"),
    ], 
    default_language = "en",
    // Optional mapping of custom Fluent functions to Rust implementations
    functions = (
        "REVERSE" = reverse, // 'REVERSE' is mapped to Self::reverse function 
        // more custom functions
    ),
    // Optional custom value formatter function
    formatter = "custom_formatter"
)]
struct Messages;
    
fn custom_formatter(language_id: &str, value: &Value, out: &mut impl std::fmt::Write) -> std::fmt::Result {
    out.write_char('|')?;
    match value {
        Value::String(s) => out.write_str(s),
        Value::Number{ value, ..} => write!(out, "{:?}", value),
        _ => Ok(())
    }?;
    out.write_char('|')
}

impl Messages {
    fn reverse<'a, 'b>(
        positional_args: &'a [Value<'a>],
        named_args: &'a [(&'a str, Value<'a>)],
    ) -> Value<'b> {
        if let Some(Value::String(s)) = positional_args.get(0) {
            // not unicode-proof
            let reversed = s.chars().rev().collect::<String>();
            Value::from(reversed)
        } else {
            Value::Error
        }
    }
}

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