fluent-i18n

Crates.iofluent-i18n
lib.rsfluent-i18n
version0.1.0-rc.1
created_at2025-08-04 08:48:23.283942+00
updated_at2025-08-04 08:59:33.252573+00
descriptionA declarative and ergonomic internationalization for Rust using Fluent
homepagehttps://gitlab.archlinux.org/orhun/fluent-i18n
repositoryhttps://gitlab.archlinux.org/orhun/fluent-i18n
max_upload_size
id1780473
size95,328
Orhun ParmaksΔ±z (orhun)

documentation

https://docs.rs/fluent-i18n/

README

fluent-i18n πŸ—£οΈπŸ”₯

License: MIT License: Apache-2.0 MSRV 1.85+

A declarative and ergonomic internationalization for Rust using Fluent.

Built on top of fluent-templates and inspired by the simplicity of rust-i18n.

Features

Example

use fluent_i18n::{i18n, t};

i18n!("locales", fallback = "en-US");

println!("{}", t!("greeting"));
println!("{}", t!("welcome", { "name" => "Orhun" }));

Place your localization files in locales/en-US/main.ftl:

greeting = Hello, world!
welcome = Welcome, { $name }!

See the Fluent syntax for more details about FTL files.

Usage

Installation

Add fluent-i18n to your Cargo.toml:

cargo add fluent-i18n

Initialization

At the entry point of your application (e.g., main.rs or lib.rs), invoke the i18n!() macro to initialize the localization system:

i18n!("locales");

Or with a fallback locale:

i18n!("locales", fallback = "en-US");

This will expose a static loader named LOCALES that will be used by the t!() macro for translations throughout your application.

You can also dynamically change the locale at runtime using the set_locale() function:

use fluent_i18n::{set_locale, get_locale};

set_locale(Some("tr"))?;

let current_locale = get_locale()?;

Running set_locale(None) will detect the system locale automatically.

Lookup

To look up a translation for a given key, use the t!() macro:

t!("greeting");

With parameters:

t!("count-items", { "count" => 3 })
t!("key", { "arg1" => value1, "arg2" => value2 })

The given parameters should be one of the supported types.

Supported Types

The t!() macro interpolates values into the message using the ToFluentValue trait.

The following types implement the ToFluentValue trait:

  • String, &'static str, Cow<'static, str>
  • Integer and float primitives (usize, u32, i64, etc.)
  • Path, PathBuf
  • Option<T> where T implements ToFluentValue

You can extend support for your own types by implementing this trait:

use fluent_i18n::{FluentValue, ToFluentValue};

impl ToFluentValue for MyType {
    fn to_fluent_value(&self) -> FluentValue<'static> {
        FluentValue::from(self.to_string())
    }
}

Locale Layout

You can organize .ftl files per locale, and shared files like core.ftl will be included in all locales.

locales/
β”œβ”€β”€ core.ftl
β”œβ”€β”€ en-US/
β”‚   └── main.ftl
β”œβ”€β”€ fr/
β”‚   └── main.ftl
β”œβ”€β”€ zh-CN/
β”‚   └── main.ftl
└── zh-TW/
    └── main.ftl

The directory names should adhere to the Unicode Language Identifier. It also respects any .gitignore or .ignore files present.

See the Fluent syntax for more details about FTL files.

Testing

In tests, you can access the translations as usual without reinitialization:

use fluent_i18n::t;

#[test]
fn test_translation() {
    assert_eq!(t!("greeting"), "Hello, world!");
}

License & Contributions

This project can be used under the terms of the Apache-2.0 or MIT. Contributions to this project, unless noted otherwise, are automatically licensed under the terms of both of those licenses.

πŸ¦€ γƒŽ( ΒΊ _ ΒΊ γƒŽ) - respect crables!

Feel free to open issues or PRs for improvements, bug fixes, or ideas!

Acknowledgements

This library was originally developed as part of the ALPM project and later extracted for general-purpose use.

Commit count: 0

cargo fmt