maomi
Introduction
Guide
Documents

I18n Support

Enable i18n support

Maomi can generate different binaries for different languages, a.k.a. the i18n support.

By default, i18n support is disable. To enable it, specify the locale in "MAOMI_I18N_LOCALE" environment variable while compilation.

MAOMI_I18N_LOCALE=en_US

When this environment variable often changes, it is recommended to add this line to build.rs:

fn main() {
println!("cargo:rerun-if-env-changed=MAOMI_I18N_LOCALE");
}

Translation files

When i18n support enabled, maomi requires every text node in the template to be translated.

Every string without translation will be marked a compilation error.

#[component(Backend = DomBackend)]
struct MyWebsite {
template: template! {
<div> "My Website" </div>
}
}

To translate this string, create a "i18n/[LOCALE].toml" file. Use "i18n/zh_CN.toml" as an example:

[translation]
"My Website" = "我的网站"

The string will be replaced according to this file when compiling the "MAOMI_I18N_LOCALE=zh_CN" version.

Dynamic string translation

If the text node is an expression, the value must be a "LocaleString" or "LocaleStaticStr". Normal strings are not accepted.

#[component(Backend = DomBackend)]
struct MyWebsite {
template: template! {
<div> { "My Website" } </div>
}
}

If the string is static and can be translated through translation files, the `i18n!` macro can be used to translate it.

#[component(Backend = DomBackend)]
struct MyWebsite {
template: template! {
<div> { i18n!("My Website") } </div>
}
}

If the string is dynamic content or does not need to be translated, mark it as translated manually.

use maomi::locale_string::LocaleString;
#[component(Backend = DomBackend)]
struct MyWebsite {
template: template! {
<div> { LocaleString::translated("My Website") } </div>
}
}

Translation groups

By default, the "[translation]" group in translation files are used. It is able to use other groups.

#[component(Backend = DomBackend, Translation = my_group)]
struct MyWebsite {
template: template! {
<div> "My Website" </div>
}
}

Then in the translation file:

[my_group]
"My Website" = "我的网站"

For dynamic string translation, use "i18n_group!" to get the group.

i18n_group!(my_group as trans);
#[component(Backend = DomBackend)]
struct MyWebsite {
template: template! {
<div> { trans!("My Website") } </div>
}
}

Format tools for translation files

The "maomi-tools" crate provides a tool "maomi-i18n-format" to collect untranslated strings and format the translation files.

cargo install maomi-tools

Collect the metadata with "MAOMI_I18N_LOCALE" and "MAOMI_I18N_FORMAT_METADATA" environment variable.

MAOMI_I18N_LOCALE=zh_CN MAOMI_I18N_FORMAT_METADATA=on cargo check

Then do the format:

MAOMI_I18N_LOCALE=zh_CN maomi-i18n-format