Crates.io | i18n_error |
lib.rs | i18n_error |
version | 0.1.0 |
source | src |
created_at | 2024-06-13 00:52:43.818417 |
updated_at | 2024-06-13 00:52:43.818417 |
description | i18n_error provides a convenient way to define and manage error messages with internationalization (i18n) support. |
homepage | |
repository | https://github.com/poi2/i18n_error |
max_upload_size | |
id | 1270105 |
size | 26,477 |
This library provides a convenient way to define and manage error messages with internationalization (i18n) support in Rust.
Add the following to your Cargo.toml
:
[dependencies]
i18n_error = "0.1"
rust-i18n = "3"
Then, run cargo build to install the dependencies.
use i18n_error::{I18nError, LanguageCode, ToI18nString};
#[macro_use]
extern crate rust_i18n;
rust_i18n::i18n!("locales");
#[derive(I18nError)]
#[i18n_language_codes(En, Fr)]
enum UseCaseError {
#[i18n_key("error.UseCaseError.AuthorizationError")]
Authorization,
#[i18n_delegate]
Domain(DomainError),
}
#[derive(I18nError)]
#[i18n_language_codes(En, Fr)]
enum DomainError {
#[i18n_key("error.DomainError.ResourceNotFound")]
ResourceNotFound,
}
fn main() {
let error = UseCaseError::Authorization;
assert_eq!(error.to_i18n_string(LanguageCode::En), "You do not have permission.".to_string());
assert_eq!(error.to_i18n_string(LanguageCode::Fr), "Vous n'avez pas la permission.".to_string());
let error = UseCaseError::Domain(DomainError::ResourceNotFound);
assert_eq!(error.to_i18n_string(LanguageCode::En), "Resource not found.".to_string());
assert_eq!(error.to_i18n_string(LanguageCode::Fr), "Ressource non trouvée.".to_string());
}
/locales
directory. Refer to the rust-i18n for more details.rust-i18n
in lib.rs
or main.rs
file.#[derive(I18nError)]
to derive the necessary traits.#[i18n_language_codes(En, Fr)]
. to specify the supported languages.LanguageCode
based on ISO 639-1. Refer to the rust-i18n/LanguageCode for more details.#[i18n_key("{key of message in locale file}")]
to map error variants to messages in the locale files.#[i18n_delegate]
.#[derive(I18nError)]
.I18nError
relies on rust-i18n
. You need to prepare your locale files in a format that conforms to rust-i18n
standards. For more details, refer to the rust-i18n documentation.
In I18nError
, locale files must be named using ISO 639-1 codes in lowercase. To support English and French, you need to create /locales/en.toml
and /locales/fr.toml
.
The string specified with i18n_key
corresponds to a YAML path in the locale file. For instance, if the key is error.UseCaseError.AuthorizationError
, the structure of the locale file should be as follows:
"error":
"UseCaseError":
"AuthorizationError": "You do not have permission."
If the corresponding key does not exist in the locale file, the string {LanguageCode in lowercase}.{key}
will be returned.
If an enum variant contains an error object, you can delegate the message generation to the inner error object by using the #[i18n_delegate]
attribute. For example, in the UseCaseError::Domain
variant, the message generation is delegated to DomainError
because i18n_delegate is specified.
#[derive(I18nError)]
#[i18n_language_codes(En, Fr)]
enum UseCaseError {
#[i18n_delegate]
Domain(DomainError),
}
In this case, DomainError
must derive I18nError
.
#[derive(I18nError)]
#[i18n_language_codes(En, Fr)]
enum DomainError {
#[i18n_key("error.DomainError.ResourceNotFound")]
ResourceNotFound,
}
If DomainError
does not derive I18nError
, a compile error will occur.
enum DomainError {
ResourceNotFound,
}
error[E0599]: no method named `to_i18n_string` found for reference `&DomainError` in the current scope
--> src/lib.rs:28:14
|
28 | #[derive(I18nError)]
| ^^^^^^^^^ method not found in `&DomainError`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `to_i18n_string`, perhaps you need to implement it:
candidate #1: `ToI18nString`
= note: this error originates in the derive macro `I18nError` (in Nightly builds, run with -Z macro-backtrace for more info)
rustcE0599
This project is licensed under the MIT License.
For more information on the license, please see the license.