cargo-i18n

The preset for cargo-i18n with Fluent in Rust. It reads i18n.toml, the .ftl files and the fl! macro of i18n-embed-fl.

The cargo-i18n preset supports cargo-i18n with the Fluent system: the i18n-embed and i18n-embed-fl crates.

Detection

The preset applies when Cargo.toml has the dependency i18n-embed-fl or i18n-embed. x18n reads [dependencies], [dev-dependencies], [build-dependencies], [workspace.dependencies] and the target dependency tables. For a renamed dependency, such as fl = { package = "i18n-embed-fl", version = "0.10" }, x18n sees the alias fl and does not detect the preset, so list it.

x18n.config.ts
import { defineConfig } from '@x18n/core'

export default defineConfig({
  presets: ['cargo-i18n'],
})

Locale files

The preset reads i18n.toml in the project root:

i18n.toml
fallback_language = "en-US"

[fluent]
assets_dir = "i18n"

fallback_language is the source locale. If your config has no resources, the preset uses {assets_dir}/{locale}/*.ftl, so i18n/en-US/my_crate.ftl. Without assets_dir, the folder is i18n.

Supported syntax

In Rust files, the preset reads the fl! macro. The first argument can be the loader, or it can be absent when you use a wrapper macro:

src/main.rs
fl!(LANGUAGE_LOADER, "hello-arg", name = "Bob");
fl!(loader, "login", "title");
fl!("hello-world");

These usages are hello-arg, login and hello-world. A used message also uses its attributes, so login.title is not unused.

Placeholders

Fluent: { $name }.

Extraction templates

KindsTemplate
anyfl!(LANGUAGE_LOADER, "{key}")

Limitations

  • The gettext system of cargo-i18n (the tr! macro and .po files) is not supported.
  • x18n reads only the i18n.toml in the project root. In a workspace with one i18n.toml per crate, set resources yourself.