Crates.io | fluent-i18n |
lib.rs | fluent-i18n |
version | 0.1.0-rc.1 |
created_at | 2025-08-04 08:48:23.283942+00 |
updated_at | 2025-08-04 08:59:33.252573+00 |
description | A declarative and ergonomic internationalization for Rust using Fluent |
homepage | https://gitlab.archlinux.org/orhun/fluent-i18n |
repository | https://gitlab.archlinux.org/orhun/fluent-i18n |
max_upload_size | |
id | 1780473 |
size | 95,328 |
A declarative and ergonomic internationalization for Rust using Fluent.
Built on top of fluent-templates
and inspired by the simplicity of rust-i18n
.
i18n!()
macrot!()
macro for inline translationsToFluentValue
traitset_locale()
and get_locale()
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.
Add fluent-i18n
to your Cargo.toml
:
cargo add fluent-i18n
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.
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.
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>
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())
}
}
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.
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!");
}
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!
This library was originally developed as part of the ALPM project and later extracted for general-purpose use.