{"id":17837824,"url":"https://github.com/poi2/i18n_error","last_synced_at":"2026-01-15T22:28:28.122Z","repository":{"id":241366904,"uuid":"803093984","full_name":"poi2/i18n_error","owner":"poi2","description":"This library provides a convenient way to define and manage error messages with internationalization (i18n) support in Rust.","archived":false,"fork":false,"pushed_at":"2024-06-13T00:49:31.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-10T22:08:02.473Z","etag":null,"topics":["rust","rust-macro"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/poi2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-20T03:56:58.000Z","updated_at":"2024-06-13T00:48:13.000Z","dependencies_parsed_at":"2024-10-27T21:35:28.523Z","dependency_job_id":"cf791a44-5a1f-4506-ade4-87fad23828fa","html_url":"https://github.com/poi2/i18n_error","commit_stats":null,"previous_names":["poi2/i18n_error"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poi2%2Fi18n_error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poi2%2Fi18n_error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poi2%2Fi18n_error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/poi2%2Fi18n_error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/poi2","download_url":"https://codeload.github.com/poi2/i18n_error/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246933384,"owners_count":20857055,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rust","rust-macro"],"created_at":"2024-10-27T20:48:56.225Z","updated_at":"2026-01-15T22:28:28.092Z","avatar_url":"https://github.com/poi2.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# I18nError for Rust\n\nThis library provides a convenient way to define and manage error messages with internationalization (i18n) support in Rust.\n\n## Installation\n\nAdd the following to your `Cargo.toml`:\n\n```toml\n[dependencies]\ni18n_error = \"0.1\"\nrust-i18n = \"3\"\n```\n\nThen, run cargo build to install the dependencies.\n\n## Example\n\n```rust\nuse i18n_error::{I18nError, LanguageCode, ToI18nString};\n\n#[macro_use]\nextern crate rust_i18n;\n\nrust_i18n::i18n!(\"locales\");\n\n#[derive(I18nError)]\n#[i18n_language_codes(En, Fr)]\nenum UseCaseError {\n    #[i18n_key(\"error.UseCaseError.AuthorizationError\")]\n    Authorization,\n    #[i18n_delegate]\n    Domain(DomainError),\n}\n\n\n#[derive(I18nError)]\n#[i18n_language_codes(En, Fr)]\nenum DomainError {\n    #[i18n_key(\"error.DomainError.ResourceNotFound\")]\n    ResourceNotFound,\n}\n\nfn main() {\n    let error = UseCaseError::Authorization;\n    assert_eq!(error.to_i18n_string(LanguageCode::En), \"You do not have permission.\".to_string());\n    assert_eq!(error.to_i18n_string(LanguageCode::Fr), \"Vous n'avez pas la permission.\".to_string());\n\n    let error = UseCaseError::Domain(DomainError::ResourceNotFound);\n    assert_eq!(error.to_i18n_string(LanguageCode::En), \"Resource not found.\".to_string());\n    assert_eq!(error.to_i18n_string(LanguageCode::Fr), \"Ressource non trouvée.\".to_string());\n}\n```\n\n1. i18n Preparation\n   1. Create locales files in `/locales` directory. Refer to the [rust-i18n](https://github.com/longbridgeapp/rust-i18n) for more details.\n   2. Load `rust-i18n` in `lib.rs` or `main.rs` file.\n2. Define the Error Struct or Enum\n   1. Use `#[derive(I18nError)]` to derive the necessary traits.\n3. Set the Language Codes\n   1. Use `#[i18n_language_codes(En, Fr)]`. to specify the supported languages.\n   2. `LanguageCode` based on ISO 639-1. Refer to the [rust-i18n/LanguageCode](https://github.com/poi2/i18n_error/blob/main/src/language_code.rs#L9) for more details.\n4. Define the Error Variants\n   1. Use `#[i18n_key(\"{key of message in locale file}\")]` to map error variants to messages in the locale files.\n   2. This allows error messages to be generated from the locale file.\n5. Delegating Error Message Generation\n   1. If you want to delegate the generation of error messages to an inner error, use `#[i18n_delegate]`.\n   2. The inner error must also be defined with `#[derive(I18nError)]`.\n   3. This allows the error message to be generated from the inner error.\n\n## Details\n\n### Preparing Locale Files\n\n`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](https://github.com/longbridgeapp/rust-i18n) documentation.\n\nIn `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`.\n\n### Generating Error Messages\n\nThe 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:\n\n```yaml\n\"error\":\n  \"UseCaseError\":\n    \"AuthorizationError\": \"You do not have permission.\"\n```\n\nIf the corresponding key does not exist in the locale file, the string `{LanguageCode in lowercase}.{key}` will be returned.\n\n### Delegating Message Generation\n\nIf 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.\n\n```rust\n#[derive(I18nError)]\n#[i18n_language_codes(En, Fr)]\nenum UseCaseError {\n    #[i18n_delegate]\n    Domain(DomainError),\n}\n```\n\nIn this case, `DomainError` must derive `I18nError`.\n\n```rust\n#[derive(I18nError)]\n#[i18n_language_codes(En, Fr)]\nenum DomainError {\n    #[i18n_key(\"error.DomainError.ResourceNotFound\")]\n    ResourceNotFound,\n}\n```\n\nIf `DomainError` does not derive `I18nError`, a compile error will occur.\n\n```rust\nenum DomainError {\n    ResourceNotFound,\n}\n\nerror[E0599]: no method named `to_i18n_string` found for reference `\u0026DomainError` in the current scope\n  --\u003e src/lib.rs:28:14\n   |\n28 |     #[derive(I18nError)]\n   |              ^^^^^^^^^ method not found in `\u0026DomainError`\n   |\n   = help: items from traits can only be used if the trait is implemented and in scope\n   = note: the following trait defines an item `to_i18n_string`, perhaps you need to implement it:\n           candidate #1: `ToI18nString`\n   = note: this error originates in the derive macro `I18nError` (in Nightly builds, run with -Z macro-backtrace for more info)\n\nrustcE0599\n```\n\n## License\n\nThis project is licensed under the MIT License.  \nFor more information on the license, please see the \u003ca href=\"LICENSE\"\u003elicense\u003c/a\u003e.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpoi2%2Fi18n_error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpoi2%2Fi18n_error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpoi2%2Fi18n_error/lists"}