2 releases

0.4.1 Oct 22, 2024
0.4.0 Oct 21, 2024

#367 in Internationalization (i18n)


Used in i18nify

MIT license

33KB
772 lines

i18nify-macro

The implementation of the i18nify macro lies here.


lib.rs:

Internationalization library based on code generation.

By leveraging code generation we are able to prevent common bugs like typos in i18n keys, missing interpolations, or various mistakes between locales.

It requires a directory with one JSON file per locale. Here is an example with English and Danish translations:

// tests/doc_locales/en.json
{
    "hello_world": "Hello, World!",
    "greeting": "Hello {name}"
}

// tests/doc_locales/da.json
{
    "hello_world": "Hej, Verden!",
    "greeting": "Hej {name}"
}

And in Rust:

use demo::Internationalize;
mod demo {
    use i18nify::I18N;
    #[derive(I18N)]
    #[i18n(folder = "tests/doc_locales")]
    pub struct DocLocale;
}

fn main() {
    // Based on the `Locale` enum type to retrieve internationalized text
    let hello = demo::Locale::En.hello_world();
    println!("{}",hello);// Hello, World!
    
    // Based on the `Internationalize` trait implemented with `DocLocale` to retrieve internationalized text
    let greeting = DocLocale::da().greeting(Name("John"));
    println!("{}",greeting);// Hej John
}

Dependencies

~2–3MB
~62K SLoC