[−][src]Macro i18n_embed_fl::fl
fl!() { /* proc-macro */ }
A macro to obtain localized messages, and check the message_id
and arguments at compile time.
Compile time checks are performed using the fallback_language
specified in the current crate's i18n.toml
confiration file.
This macro supports three different calling syntaxes which are explained in the following sections.
No Arguments
fl!(loader: FluentLanguageLoader, "message_id")
This is the simplest form of the fl!()
macro, just obtaining a
message with no arguments. The message_id
should be specified as
a literal string, and is checked at compile time.
Example
use i18n_embed::{ fluent::{fluent_language_loader, FluentLanguageLoader}, LanguageLoader, }; use i18n_embed_fl::fl; use rust_embed::RustEmbed; #[derive(RustEmbed)] #[folder = "i18n/"] struct Localizations; let loader: FluentLanguageLoader = fluent_language_loader!(); loader .load_languages(&Localizations, &[loader.fallback_language()]) .unwrap(); // Invoke the fl!() macro to obtain the translated message, and // check the message id compile time. assert_eq!("Hello World!", fl!(loader, "hello-world"));
Individual Arguments
fl!( loader: FluentLanguageLoader, "message_id", arg1 = value, arg2 = "value", arg3 = function(), ... )
This form of the fl!()
macro allows individual arguments to be
specified in the form key = value
after the message_id
. key
needs to be a valid literal argument name, and value
can be any
expression that resolves to a type that implements
Into<FluentValue>
. The key
s will be checked at compile time to
ensure that they match the arguments specified in original fluent
message.
Example
let calc_james = || "James".to_string(); pretty_assertions::assert_eq!( "Hello \u{2068}Bob\u{2069} and \u{2068}James\u{2069}!", // Invoke the fl!() macro to obtain the translated message, and // check the message id, and arguments at compile time. fl!(loader, "hello-arg-2", name1 = "Bob", name2 = calc_james()) );
Arguments Hashmap
fl!( loader: FluentLanguageLoader, "message_id", args: HashMap< S where S: Into<Cow<'a, str>> + Clone, T where T: Into<FluentValue>> + Clone> )
With this form of the fl!()
macro, arguments can be specified at
runtime using a HashMap, using the
same signature as in
FluentLanguageLoader::get_args().
When using this method of specifying argments, they are not
checked at compile time.
Example
use std::collections::HashMap; let mut args: HashMap<&str, &str> = HashMap::new(); args.insert("name", "Bob"); assert_eq!("Hello \u{2068}Bob\u{2069}!", fl!(loader, "hello-arg", args));