recoyx_message_localization

Crates.iorecoyx_message_localization
lib.rsrecoyx_message_localization
version1.1.2
sourcesrc
created_at2022-05-14 17:25:14.907002
updated_at2022-05-25 11:00:09.258071
descriptionFlexible message localization.
homepage
repositoryhttps://github.com/recoyx/message_localization_rs
max_upload_size
id586721
size40,835
Klaider Animekist Matheus Dias de Souza (Klaider)

documentation

README

Message Localization

crates.io docs.rs

All-in-one package for flexible localization on Rust.

Features:

  • MessageLocator
    • Load assets from either HTTP or file system.
  • General language code and country code manipulation.
    • Locale object and parse_locale(str)
    • Country object and parse_country(str)

Getting started

This example uses the Tokio asynchronous runtime framework, solely for demonstrative purposes.

Add the following dependencies to Cargo.toml:

[dependencies]
recoyx_localization = "1"
maplit = "1.0"
tokio = { version = "1", features = ["full"] }

Example asset located at res/lang/en/_.json:

{
    "message_id": "Some message",
    "parameterized": "Here: $x",
    "contextual_male": "Male message",
    "contextual_female": "Female message",
    "contextual_other": "Other message",
    "qty_empty": "Empty ($number)",
    "qty_one": "One ($number)",
    "qty_multiple": "Multiple ($number)"
}

Example program using these assets:

use recoyx_localization::{
    MessageLocator, MessageLocatorOptions, MessageLocatorAssetOptions,
    MessageLocatorLoadVia,
    localization_vars,
};
use maplit::hashmap;

#[tokio::main]
async fn main() {
    let mut msg_locator = MessageLocator::new(
        MessageLocatorOptions::new()
            // Specify supported locale codes.
            // The form in which the locale code appears here
            // is a post-component for the assets "src" path. 
            // For example: "path/to/res/lang/en-US"
            .supported_locales(vec!["en", "en-US", "pt-BR"])
            .default_locale("en-US")
            .fallbacks(hashmap! {
                "en-US" => vec!["en"],
                "pt-BR" => vec!["en-US"],
            })
            .assets(MessageLocatorAssetOptions::new()
                .src("res/lang")
                .base_file_names(vec!["_"])
                // "clean_unused" indicates whether to clean previous unused locale data. 
                .clean_unused(true)
                // Specify MessageLocatorLoadVia::FileSystem or MessageLocatorLoadVia::Http
                .load_via(MessageLocatorLoadVia::FileSystem))
    ); // msg_locator

    if (!msg_locator.load(None).await) {
        // failed to load
    }

    println!("{}", msg_locator.get("_.message_id"));
    println!("{}", msg_locator.get_formatted("_.parameterized", vec![ &localization_vars!{
        "x" => "foo"
    } ]));
    println!("{}", msg_locator.get_formatted("_.contextual", vec![ "female" ]));
}
Commit count: 0

cargo fmt