| Crates.io | lingua-i18n-rs |
| lib.rs | lingua-i18n-rs |
| version | 0.3.0 |
| created_at | 2025-04-01 11:10:36.208023+00 |
| updated_at | 2025-12-16 13:00:19.854407+00 |
| description | A simple and lightweight internationalization (i18n) library for Rust |
| homepage | |
| repository | https://github.com/Karnes-Development/lingua-i18n-rs |
| max_upload_size | |
| id | 1614710 |
| size | 44,880 |
A simple and lightweight internationalization (i18n) library for Rust applications.
Add this to your Cargo.toml:
[dependencies]
lingua-i18n-rs = "0.3.0"
use lingua_i18n_rs::prelude::*;
fn main() -> Result<(), LinguaError> {
// Initialize with language files in the "languages" directory
Lingua::new("languages").init()?;
// Get a simple translation
println!("{}", Lingua::t("welcome", &[])?);
// With parameter substitution
println!("{}", Lingua::t("greeting", &[("name", "World")])?);
// Using nested keys
println!("{}", Lingua::t("menu.file.save", &[])?);
// List available languages
let languages = Lingua::get_languages()?;
println!("Available languages: {:?}", languages);
// Change language
if Lingua::set_language("fr")? {
println!("Language changed to French");
}
Ok(())
}
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"
}
}
}
Lingua::new(language_dir: &str) -> LinguaBuilderCreate a new builder with a custom language directory.
LinguaBuilder::init() -> Result<Lingua, LinguaError>Initialize the library and load all available languages from the specified 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 Ok(true) if successful, or an error if the language is not available.
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.
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
This project is licensed under the MIT License - see the LICENSE file for details.