LocalFmt: Provide simple localizable format strings
Overview
local-fmt is a Rust library designed to simplify the process of creating applications that support multiple languages. It allows developers to define localizable format strings in a structured and maintainable way, using familiar file formats like TOML, JSON, and YAML.
Crate Configuration Features
To customize the functionality of local-fmt, you can enable the following features in your Cargo.toml:
- serde: Enables serialization and deserialization of messages, allowing you to save and load message configurations from various formats. This feature is useful for persistent storage or network transmission of localized messages.
- macros: Includes macros such as
gen_static_message,gen_alloc_message, anddef_local_fmt. If you usedef_local_fmt, choose one of the following:- macros-toml: Enables parsing of TOML files.
- macros-json: Enables parsing of JSON files.
- macros-yaml: Enables parsing of YAML files.
Install
[]
= "0.2" # Required if you use `def_local_fmt` macro or
# call the `new` function of LocalFmt
= { = "0.5", = ["macros", "macros-toml"] }
Key Features
- Localizable Messages: Easily define messages in multiple languages using TOML, JSON, or YAML files.
- Dynamic Language Switching: Change the language at runtime using a function pointer, allowing for flexible language management.
- Compile-time Checks: Ensure the correctness of message formats at compile time by:
- Verifying that the number of arguments matches the placeholders.
- Ensuring that all required arguments are present.
- Providing detailed error messages that specify which language key is affected, helping you quickly identify and resolve issues.
- Integration with Serde: Optionally serialize and deserialize messages for persistent storage or network transmission.
Usage Example
Here's a simple example demonstrating how to use local-fmt in a Rust project.
This example shows how to create a localizable format string and use it to format a message.
use RwLock;
use Enumable;
use ;
// Nested struct example
static LANG: = new;
def_local_fmt!;
// Example content of doctest/lang/EN.toml
// hello = "Hello, world! {0}"
//
// [words]
// ownership = "ownership"
For more detailed information on def_local_fmt, see the documentation.
License
Licensed under