recoyx_localization

Crates.iorecoyx_localization
lib.rsrecoyx_localization
version1.4.2
sourcesrc
created_at2021-02-18 17:58:27.634778
updated_at2021-03-03 14:24:33.276048
descriptionAll-in-one package for flexible localization.
homepage
repositoryhttps://github.com/recoyx/localization-rs
max_upload_size
id357156
size58,784
Klaider Animekist Matheus Dias de Souza (Klaider)

documentation

README

Localization

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

Features:

  • LocaleMap
    • Load assets from HTTP and File System.
    • Handle plural rules.
    • Relative-time formatting. This relies on the crate timeago.
  • General language code and country code manipulation.
    • Locale and parse_locale(str)
    • Country and parse_country(str)

Support for these features are upcoming:

  • Date and time formatting
  • Number formatting

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 path/to/res/lang/en/common.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::{
    LocaleMap, LocaleMapOptions, LocaleMapAssetOptions,
    Gender, LocaleMapLoaderType,
    localization_vars,
};
use maplit::hashmap;

#[tokio::main]
async fn main() {
    let mut locale_map = LocaleMap::new(
        LocaleMapOptions::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(LocaleMapAssetOptions::new()
                .src("path/to/res/lang")
                .base_file_names(vec!["common", "validation"])
                // "auto_clean" indicates whether to clean previous unused locale data. 
                .auto_clean(true)
                // Specify LocaleMapLoaderType::FileSystem or LocaleMapLoaderType::Http
                .loader_type(LocaleMapLoaderType::FileSystem))
    ); // locale_map
    locale_map.load(None).await;

    println!("{}", locale_map.get("common.message_id"));
    println!("{}", locale_map.get_formatted("common.parameterized", vec![ &localization_vars!{
        "x" => "foo"
    } ]));
    println!("{}", locale_map.get_formatted("common.contextual", vec![ &Gender::Female ]));
    for i in 0..3 {
        println!("{}", locale_map.get_formatted("common.qty", vec![ &i ]));
    }
}
Commit count: 0

cargo fmt