#localization #fluent #generate-static

fluent-static

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

11 releases (4 breaking)

0.5.0 Oct 23, 2024
0.4.0 Aug 8, 2024
0.3.2 Aug 8, 2024
0.2.4 Jun 6, 2024
0.1.0 May 24, 2024

#52 in Internationalization (i18n)

Download history 5/week @ 2024-07-26 485/week @ 2024-08-02 198/week @ 2024-08-09 22/week @ 2024-08-16 35/week @ 2024-08-23 19/week @ 2024-08-30 12/week @ 2024-09-06 56/week @ 2024-09-13 66/week @ 2024-09-20 61/week @ 2024-09-27 68/week @ 2024-10-04 62/week @ 2024-10-11 146/week @ 2024-10-18 30/week @ 2024-10-25 23/week @ 2024-11-01 34/week @ 2024-11-08

235 downloads per month

MIT license

52KB
1K SLoC

fluent-static

Latest version

fluent-static provides 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.

Features

  • Compile-time Validation: no chance to to make a typo in l10n message name or use it with the wrong number of arguments
  • Ergonomic API: Just a method call my_l10n.my_message() to get l10n message
  • Minimal Runtime Overhead: Fluent messages are translated into Rust code, no loading and parsing l10n resources at runtime required
  • Advanced Formatters: Use (optionally) Rust ICU bindings to apply locale-specific formatting rules to currencies, measurement units values

Usage

Cargo dependencies

[dependencies]
fluent-static = "*"

Create Fluent resource

# <project root>/l10n/messages.ftl

say-hello = Hello, { $name }
    

Declare message bundle

use fluent_static::message_bundle;

#[message_bundle(
    resources = [
        ("l10n/messages.ftl", "en"),
        // add more Fluent resources
        // ("i10n/errors.ftl", "en")
        // ("i10n/messages-fr.ftl", "fr")
    ],
    default_language = "en"
)]
pub struct Messages;

Use the l10n messages

use fluent_static::MessageBundle;

pub fn main() {
    let lang = "en";
    let messages = Messagess::get(lang).unwrap_or_default();

    println!(messages.say_hello("World"));
}
    

Notes

  1. Language ID must be valid Unicode Language Identifier
  2. Message names are converted to snake_case
  3. Function parameters are defined in the same exact order as they appear in a Fluent message defined in default_language bundle
  4. Message must be defined for each supported language
  5. Messages with arguments must have the same number and names of arguments (order doesn't matter) for each supported language
  6. Messages and terms must be defined before they could be referenced

A bit more advanced usage

  • Use codegen in custom build scripts
  • More customizations to message_bundle proc macro for custom functions and formmaters

Crate features

  • icu enables different style of number formatting according to locale/language specific rules, requires native ICU libraries to be installed, see example
  • axum provides configurable value extractor to retrieve l10n bundle according to cookie or Accept-Language header value, see example
  • maud adds support for Maud Rendere to l10n Message value, see example

Contributing

Contributions are welcome! Please feel free to submit pull requests, report bugs, and suggest features via the issue tracker.

License

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


Dependencies

~1.6–3MB
~64K SLoC