Skip to main content

Crate i18n_string

Crate i18n_string 

Source
Expand description

§i18n-string

A lightweight and flexible Rust library for handling internationalization strings with template support.

§Getting Started

§Installation

Add this to your Cargo.toml:

[dependencies]
i18n-string = "2.0"

§Basic Usage

use std::{
    borrow::Cow,
    str::FromStr,
};

use i18n_string::{I18nString, Resolver, I18nStringTranslateExt};

struct SimpleResolver;

impl Resolver for SimpleResolver {
    fn resolve<'s>(&'s self, template: &'s str) -> Cow<'s, str> {
        match template {
            "world" => Cow::Borrowed("<translated world>"),
            _ => template.into(),
        }
    }
}

fn main() {
    let s = I18nString::template(
        "hello {0}, you are {1}",
        [
            I18nString::template("world", []),
            I18nString::literal("123")
        ]
    );
    // or create directly from template
    let sd = I18nString::from_str("t!('hello {0}, you are {1}', t!('world'), '123')").unwrap();
    assert_eq!(s, sd);

    println!("Original: {}", s.to_no_translate_string());
    println!("Translated: {}", s.translate(&SimpleResolver));
}

Modules§

escape

Structs§

InvalidFormat
Error type for invalid I18nString format.
NoResolver
A resolver that does not resolve any templates.

Enums§

I18nString
A string that can be translated into multiple languages.

Traits§

I18nStringBuilderExt
Extension trait for I18nString to build it from other types.
I18nStringTranslateExt
Extension trait for I18nString to translate it into a no-translate string.
Resolver
Trait for resolving translated I18nString templates.