About this Crate
This crate offers some localization tools to be used with Axum.
Basic usage
You can use this crate to extract a language identifier (ex en-US) from a request.
If the mode is set to RedirectMode::NoRedirect, the Accept-Language header is used to find the users preferred language. If the mode is set to either RedirectMode::RedirectToFullLocaleSubPath or RedirectMode::RedirectToLanguageSubPath, the user will be redirected to a sub-path based on their Accept-Language headers, to to the default language if not supported.
use ;
use Extension;
pub const ENGLISH: LanguageIdentifier = langid!;
pub const JAPANESE: LanguageIdentifier = langid!;
let router = new
.route
.layer;
For RedirectMode::RedirectToFullLocaleSubPath or RedirectMode::RedirectToLanguageSubPath, you must wrap this service/middleware around the entire
axum app, as explained here.
When using the subpath redirect modes, you may want to exclude some folders from the redirect as below:
let l10n_middleware = new
.excluded_paths;
Features
tracing
Allows use of tracing for logs.
fluent
Enabling fluent allows you to use the fluent Localizer to add bundles for translation.
See fluent-rs for details about fluent and rust.
Usage
use ;
use Localizer;
pub const ENGLISH: LanguageIdentifier = langid!;
pub const JAPANESE: LanguageIdentifier = langid!;
let mut localizer = new;
localizer
.add_bundle
.unwrap;
localizer
.add_bundle
.unwrap;
let message = localizer.format_message;
assert_eq!;
tera
Enabling the tera feature allows you to use the fluent translations inside tera templates.
See tera for more information on tera.
Usage
Initialization:
use Tera;
let mut tera = new.expect;
let mut localizer = new;
localizer
.add_bundle
.unwrap;
tera.register_function;
Axum handler:
async
In tera template:
{{ fluent(key="list-family", lang=lang) }}
{% for family in families %}
{{ family.family_name }}
{% endfor %}