3 unstable releases
Uses new Rust 2024
new 0.2.0 | Apr 2, 2025 |
---|---|
0.1.1 | Apr 1, 2025 |
0.1.0 | Apr 1, 2025 |
#102 in Internationalization (i18n)
254 downloads per month
21KB
362 lines
Lingua I18n for Rust
A simple and lightweight internationalization (i18n) library for Rust applications.
Features
- Easy to set up and use
- JSON-based translations
- Nested keys support with dot notation
- Variable substitution with {{variable}} syntax
- Automatic language detection from system settings
- Minimal dependencies
Installation
Add this to your Cargo.toml
:
[dependencies]
lingua-i18n-rs = "0.2.0"
Quick Start
use lingua_i18n_rs::prelude::*;
fn main() {
// Initialize with language files in the "language" directory
Lingua::init().unwrap();
// Get a simple translation
println!("{}", Lingua::t("welcome", &[]).unwrap());
// With parameter substitution
println!("{}", Lingua::t("greeting", &[("name", "World")]).unwrap());
// Using nested keys
println!("{}", Lingua::t("menu.file.save", &[]).unwrap());
// List available languages
let languages = Lingua::get_languages().unwrap();
println!("Available languages: {:?}", languages);
// Change language
if Lingua::set_language("fr") {
println!("Language changed to French");
}
}
Language Files
Place your translation files in a directory (default: "languages"). Each file should be named with its language code and have a .json
extension:
languages/en.json:
{
"welcome": "Welcome to the application!",
"greeting": "Hello, {{name}}!",
"menu": {
"file": {
"save": "Save"
}
}
}
languages/de.json:
{
"welcome": "Willkommen in der Anwendung!",
"greeting": "Hallo, {{name}}!",
"menu": {
"file": {
"save": "Speichern"
}
}
}
API Reference
Lingua::init() -> Result<(), LinguaError>
Initialize with the default language directory ("language").
Lingua::init_with_dir(dir: &str) -> Result<(), LinguaError>
Initialize with a custom language directory.
Lingua::t(key: &str, params: &[(&str, &str)]) -> Result<String, LinguaError>
Translate a key with optional parameters. Short form of translate
.
Lingua::translate(key: &str, params: &[(&str, &str)]) -> Result<String, LinguaError>
Translate a key with optional parameters.
Lingua::set_language(lang_code: &str) -> Result<bool, LinguaError>
Change the current language. Returns true if successful.
Lingua::get_languages() -> Result<Vec<String>, LinguaError>
Get a list of all available languages.
Lingua::get_language() -> Result<String, LinguaError>
Get the current language code.
Lingua::load_lang_from_config(path: &Path, key: &str) -> Result<String, LinguaError>
Load a language code from a configuration file. If you are using a configuration file to store the language code, you can use this function to load it.
Examples
See the examples directory for more complete examples. To run an example, use the following command:
cargo run --example <example_name>
For an example using Lingua, see the rusty-weather
License
This project is licensed under the MIT License - see the LICENSE file for details.
Dependencies
~0.7–1.6MB
~33K SLoC