Skip to main content

init_translations

Macro init_translations 

Source
macro_rules! init_translations {
    ($dirname:expr) => { ... };
}
Expand description

Initialize translations when using the gettext feature.

Call this at the start of your main function, before starting other threads, with the path where translations are located. This macro internally calls the bindtextdomain function from gettext. It also sets the C library’s LC_MESSAGES locale from the environment, which isn’t thread-safe.

The first argument of the macro must be an expression that implements Into<std::path::PathBuf>. It specifies the directory in which gettext should search for translations.

Translations are expected to be found at <dirname>/<locale>/LC_MESSAGES/<crate>.mo, where dirname is the directory passed as an argument to this macro, locale is a locale name (e.g., en, en_GB, fr), and crate is the package name obtained from the CARGO_PKG_NAME environment variable.

See also the Translation documentation.

§Example

fn main() {
   slint::init_translations!(concat!(env!("CARGO_MANIFEST_DIR"), "/translations/"));
   // ...
}

For example, assuming this is in a crate called example and the default locale is configured to be French, it will load translations at runtime from /path/to/example/translations/fr/LC_MESSAGES/example.mo.

Another example of loading translations relative to the executable:

slint::init_translations!(std::env::current_exe().unwrap().parent().unwrap().join("translations"));