5 个稳定版本

1.3.1 2022年11月15日
1.2.1 2022年11月15日
1.0.0 2022年11月5日

#24 in #rialight

ISC 许可证

30KB
538

rialight::本地化

LocaleBundle

要使用 LocaleBundle,请将这些依赖项添加到 Cargo.toml

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

示例资产位于 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)"
}

示例用法

use rialight_localization::{
    LocaleBundle, LocaleBundleOptions, LocaleBundleOptionsForAssets,
    LocaleBundleLoadMethod,
    bundle_vars,
};
use maplit::hashmap;

#[tokio::main]
async fn main() {
    let mut bundle = LocaleBundle::new(
        LocaleBundleOptions::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", "pt-BR"])
            .default_locale("en-US")
            .fallbacks(hashmap! {
                "pt-BR" => vec!["en-US"],
            })
            .assets(LocaleBundleOptionsForAssets::new()
                .src("res/lang")
                .base_file_names(vec!["_"])
                // "clean_unused" indicates whether to clean previous unused locale data. 
                .clean_unused(true)
                // Specify LocaleBundleLoadMethod::FileSystem or LocaleBundleLoadMethod::Http
                .load_method(LocaleBundleLoadMethod::FileSystem))
    ); // bundle

    if !bundle.load(None).await {
        // failed to load
        return;
    }

    println!("{}", bundle.get("_.message_id"));
    println!("{}", bundle.get_formatted("_.parameterized", vec![ &bundle_vars!{
        "x" => "foo"
    } ]));
    println!("{}", bundle.get_formatted("_.contextual", vec![ &"female" ]));
}

依赖项

~10–24MB
~376K SLoC