Macro slint::init_translations

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

Initialize translations when using the gettext feature.

Call this in your main function with the path where translations are located. This macro internally calls the bindtextdomain function from gettext.

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.

§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"));