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:
-
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}" } -
Rust Code: Use the
I18Nderive 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); } -
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; } -
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
I18nifyLayermiddleware:use axum::{Router, routing::get, Extension}; use i18nify::I18nifyLayer; let app = Router::new() .route("/", get(root)) .layer(I18nifyLayer::new(DocLocale, "en")); -
Use the
Localeto 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.