Languages
leptos-fluent follows a default strategy to generate the languages of the application. This strategy is based on the locales/ directory.
Giving the next directory structure:
.
└── 📁 locales
├── 📁 en
│ └── 📄 main.ftl
└── 📁 es-ES
└── 📄 main.ftl
The framework will generate something like the following languages array at compile time:
let LANGUAGES = [
leptos_fluent::Language {
id: "en",
name: "English",
dir: leptos_fluent::WritingDirection::Ltr,
flag: Some("🇬🇧"),
script: None,
},
leptos_fluent::Language {
id: "es-ES",
name: "Español (España)",
dir: leptos_fluent::WritingDirection::Ltr,
flag: Some("🇪🇸"),
script: None,
},
]
enis built with the name"English"because it's defined as an ISO 639 language code, without a region code.es-ESis built with the name"Español (España)"because it's defined as an ISO 639 language code and a ISO 3166 region code.
This enforces that a user will always be able to select their language in their own language, and not in the current language of the application.
Inferred language names
When not using a languages file, the language names are inferred from
the language codes used for locales directories following the next rules:
- If only a language code is provided, use "Language name" (see full-list).
- If a region code is provided but the language is not repeated, use "Language name".
- If a region code is provided and the language is repeated, use "Language name (region name)" (see full list).
The languages file
The languages array can be fully customized by defining a languages parameter
in the leptos_fluent! macro pointing to a languages file. This file must
be relative to the Cargo.toml file.
leptos_fluent! {
languages: "./locales/languages.json",
// ...
}
[
["en", "English"],
["es-ES", "Spanish (Spain)", "auto"]
]
Languages file format
In order to use a languages file, you must enable the feature for the file format you want to use in the Cargo.toml file:
[dependencies]
leptos-fluent = { version = "0.3", features = ["json5"] }
Available features for languages file formats are:
json: JSONyaml: YAMLjson5: JSON5
Languages file layout
The languages file must expose an array of arrays with the structure:
[
// Code, Name, "ltr"/"rtl"/"auto" (optional)
["code", "Language name", "Writing direction"],
]
The order of the languages in
leptos_fluent::I18n::languages
will be the same as in the file regardless of the alphabetical order
of the names.
If default_language parameter is not passed to leptos_fluent! macro, the
first one in the languages file or language code folder by their alphabetical
order will used as the initial of the user when no other initialization
language is discovered.
#[component]
pub fn App() -> impl IntoView {
leptos_fluent! {
languages: "./locales/languages.json5",
default_language: "en",
};
}
// ./locales/languages.json5
[
["en", "English"],
["es-ES", "Español (España)"],
]