#icu4x #localization #cache #rust-pattern

i18n_message-rizzen-yazston

The i18n-message crate of the Internationalisation project

1 unstable release

0.6.1 Jul 6, 2023

#404 in Internationalization (i18n)

BSD-3-Clause

325KB
4.5K SLoC

i18n_message

Rizzen Yazston

Message system

The i18n_message crate contains the messaging system.

A message system that connects to a string data store, to obtain strings for the specified language using a string identifier, and formatting the string to replace any placeholders within the string with provided values.

The message is capable of caching retrieved strings that are prepared for placeholder replacement, thus can be reused without the need to parse the string for placeholders.

The message system makes use of all the other component crates that make up the i18n project. Ideally one only needs to use the meta crate i18n, as it includes all the crates including this i18n_message crate.

Acknowledgement

Stefano Angeleri for advice on various design aspects of implementing the components of the internationalisation project, and also providing the Italian translation of error message strings.

Cargo.toml

[dependencies]
i18n_message-rizzen-yazston = "0.6.1"
i18n_icu-rizzen-yazston = "0.6.1"
i18n_lexer-rizzen-yazston = "0.6.1" # Needed for Token, TokenType
i18n_pattern-rizzen-yazston = "0.6.1"
i18n_provider-rizzen-yazston = "0.6.1"
i18n_provider_sqlite3-rizzen-yazston = "0.6.1"
i18n_utility-rizzen-yazston = "0.6.1"
tree-rizzen-yazston = "0.4.0"
icu_provider = "1.2.0"

# These are required for the DataProvider.
icu_properties = "1.2.0"
icu_segmenter = "1.2.0"
icu_plurals = "1.2.0"
icu_decimal = "1.2.0"
icu_calendar = "1.2.0"
icu_datetime = "1.2.0"

# This is required for the DataProvider.
[dependencies.fixed_decimal]
version = "0.5.3"
# Needed for floating point support.
features = [ "ryu" ]

Examples

use i18n_icu::IcuDataProvider;
use i18n_registry::LanguageTagRegistry;
use i18n_provider_sqlite3::ProviderSqlite3;
use i18n_pattern::{PlaceholderValue, CommandRegistry};
use i18n_message::Message;
use icu_testdata::buffer;
use icu_provider::serde::AsDeserializingBufferProvider;
use std::collections::HashMap;
use std::rc::Rc;
use std::error::Error;

fn message() -> Result<(), Box<dyn Error>> {
    let buffer_provider = buffer();
    let data_provider = buffer_provider.as_deserializing();
    let icu_data_provider = Rc::new(
        IcuDataProvider::try_new( &data_provider )?
    );
    let language_tag_registry = Rc::new( LanguageTagRegistry::new() );
    let lstring_provider = ProviderSqlite3::try_new(
        "./i18n/", &language_tag_registry
    )?;
    let command_registry = Rc::new( CommandRegistry::new() );
    let message_system = Message::try_new(
        &icu_data_provider, &language_tag_registry, &lstring_provider, &command_registry, true, true
    )?;
    let mut values = HashMap::<String, PlaceholderValue>::new();
    values.insert(
        "identifier".to_string(),
        PlaceholderValue::String( "i18n_message/string_not_found".to_string() )
    );
    values.insert(
        "language_tag".to_string(),
        PlaceholderValue::String( "en-ZA".to_string() )
    );
    values.insert(
        "fallback".to_string(),
        PlaceholderValue::String( "true".to_string() )
    );
    let lstring = message_system.format(
        "i18n_message/string_not_found",
        &values,
        &language_tag_registry.get_language_tag( "en-ZA" ).unwrap(),
        None,
        None
    )?;
    assert_eq!(
        lstring.as_str(),
        "No string was found for identifier ‘i18n_message/string_not_found’ and language tag ‘en-ZA’. Fallback used: True.",
        "Check placeholder values."
    );
    Ok( () )
}

Dependencies

~29MB
~537K SLoC