May 01, 2023
3 min read
Rust,
i18n,

i18nify

Internationalization library for Rust based on code generation.

Project Overview: i18nify - Internationalization Library for Rust

Project Name: i18nify

Description: i18nify is an internationalization (i18n) library for Rust that leverages code generation to prevent common localization bugs. It is an updated and maintained fork of the original i18n_codegen library by David Pedersen, which had become outdated and unmaintained.

Key Features:

  • Code Generation: Automatically generates Rust code from JSON or TOML files containing translations, reducing the risk of typos in i18n keys and ensuring consistency across locales.
  • Locale Support: Supports multiple locales and allows for easy addition of new translations.
  • Environment Variables: Allows the use of environment variables in the folder path for flexibility in deployment environments.
  • Integration with Axum: Provides seamless integration with the Axum web framework for server-side internationalization.

Installation: To use i18nify in your Rust project, you can add it to your Cargo.toml file with the desired features:

# Using JSON format
i18nify = { version = "0.3", features = ["json"] }

# Using TOML format
i18nify = { version = "0.3", features = ["toml"] }

Alternatively, you can add it using cargo:

cargo add i18nify # default features=['json']

Usage:

  1. Directory Structure: Create a directory with one JSON file per locale. For example:

    // tests/doc_locales/en.json
    {
      "hello_world": "Hello, World!",
      "greeting": "Hello {name}"
    }
    
    // tests/doc_locales/da.json
    {
      "hello_world": "Hej, Verden!",
      "greeting": "Hej {name}"
    }
  2. Rust Code: Use the I18N derive macro to generate the necessary code for internationalization:

    use demo::Internationalize;
    
    mod demo {
        use i18nify::I18N;
        #[derive(I18N)]
        #[i18n(folder = "tests/doc_locales")]
        pub struct DocLocale;
    }
    
    fn main() {
        // Retrieve internationalized text based on the `Locale` enum
        let hello = demo::Locale::En.hello();
        assert_eq!("Hello, World!", hello);
        println!("{}", hello);
    
        // Retrieve internationalized text using the `Internationalize` trait
        let hello = demo::DocLocale.da().hello();
        println!("{}", hello);
    }
  3. Environment Variables: You can use environment variables in the folder path for more flexibility:

    use demo::Internationalize;
    
    mod demo {
        use i18nify::I18N;
        #[derive(I18N)]
        #[i18n(folder = "$CARGO_MANIFEST_DIR/tests/doc_locales")]
        pub struct DocLocale;
    }
  4. Axum Integration: To integrate i18nify with the Axum web framework, follow these steps:

    • Define an internationalization trait implementation:

      use i18nify::{Internationalization, I18N};
      
      #[derive(I18N, Clone)]
      #[i18n(folder = "$CARGO_MANIFEST_DIR/tests/zh_locales")]
      pub struct DocLocale;
      
      impl Internationalization for DocLocale {
          type Item = Locale;
      
          fn i(&self, lang: &str) -> Self::Item {
              match lang.to_lowercase().as_str() {
                  "en" => Locale::En,
                  "zh-cn" => Locale::ZhCn,
                  _ => Locale::En,
              }
          }
      }
  • Add the I18nifyLayer middleware:

    use axum::{Router, routing::get, Extension};
    use i18nify::I18nifyLayer;
    
    let app = Router::new()
        .route("/", get(root))
        .layer(I18nifyLayer::new(DocLocale, "en"));
  • Use the Locale to get internationalized text in your handler:

    async fn root(Extension(locale): Extension<Locale>) -> impl IntoResponse {
        locale.greeting() // Hello, world
    }

Documentation: For more detailed information and advanced usage, refer to the official documentation: https://docs.rs/i18nify

Repository:

  • GitHub: [Link to GitHub Repository]

i18nify aims to provide a robust and user-friendly solution for internationalizing Rust applications, making it easier to manage and maintain translations across multiple locales.