USKESL6XR6C7676X3PO3SFFL5EMKMA7EQMPZAA72A7F7UZSONOIQC
//! Example showing how to localize Jiff timestamps
use icu_locale::{Locale, locale};
use jiff::{Timestamp, ToSpan};
use l10n_embed::Localize;
const DEFAULT_LOCALE: Locale = locale!("en-US");
fn main() -> Result<(), jiff::Error> {
// Create some timestamps
let current_timestamp = Timestamp::now();
let current_datetime = current_timestamp.in_tz("UTC")?;
let unix_epoch = Timestamp::UNIX_EPOCH;
let in_two_hours = current_timestamp.checked_add(2.hours())?;
let start_of_year = current_datetime.first_of_year()?.timestamp();
// Localize those timestamps into a relative time
println!(
"Current time: {}",
current_timestamp.message_for_locale(&DEFAULT_LOCALE)
);
println!(
"Unix epoch: {}",
unix_epoch.message_for_locale(&DEFAULT_LOCALE)
);
println!(
"Two hours from now: {}",
in_two_hours.message_for_locale(&DEFAULT_LOCALE)
);
println!(
"Since start of year (UTC): {}",
start_of_year.message_for_locale(&DEFAULT_LOCALE)
);
Ok(())
}
//! Example showing how to style localized types
use anstyle::AnsiColor;
use icu_locale::{Locale, locale};
use jiff::Timestamp;
use l10n_embed::Localize;
use l10n_embed::style::Styled;
const DEFAULT_LOCALE: Locale = locale!("en-US");
// This is used to disable rendering of colors, e.g. based on user configuration
const STYLE_ENABLED: bool = true;
fn main() {
// Create some localizable data to be styled
let five_million: Styled<u64> = Styled::new(5_000_000, STYLE_ENABLED)
.bold()
.color(AnsiColor::Green);
let static_str: Styled<&'static str> = Styled::new("&str", STYLE_ENABLED)
.italic()
.color(AnsiColor::Blue);
let unix_epoch: Styled<Timestamp> = Styled::new(Timestamp::UNIX_EPOCH, STYLE_ENABLED)
.strikethrough()
.color(AnsiColor::Red);
// Localize these strings, which adds colors (if enabled) and forwards to the stored type's implementation.
println!(
"Five million: {}",
five_million.message_for_locale(&DEFAULT_LOCALE)
);
println!(
"Static string: {}",
static_str.message_for_locale(&DEFAULT_LOCALE)
);
println!(
"Unix epoch: {}",
unix_epoch.message_for_locale(&DEFAULT_LOCALE)
);
}
//! Example showing how to localize strings
use std::borrow::Cow;
use icu_locale::{Locale, locale};
use l10n_embed::Localize;
const DEFAULT_LOCALE: Locale = locale!("en-US");
fn main() {
// Create some strings
let static_str = "&str";
let heap_allocated_string = String::from("String");
let copy_on_write_str = Cow::from("Cow<str>");
// Localize these strings, which just prints them.
// This would mostly be useful for non-localizable content such as usernames or URLs
println!(
"Static string: {}",
static_str.message_for_locale(&DEFAULT_LOCALE)
);
println!(
"Heap allocated string: {}",
heap_allocated_string.message_for_locale(&DEFAULT_LOCALE)
);
println!(
"Copy on write string: {}",
copy_on_write_str.message_for_locale(&DEFAULT_LOCALE)
);
}
//! Example showing how to localize fixed_decimal decimals
use fixed_decimal::Decimal;
use icu_locale::{Locale, locale};
use l10n_embed::Localize;
const DEFAULT_LOCALE: Locale = locale!("en-US");
fn main() -> Result<(), fixed_decimal::LimitError> {
// Create some decimals
let zero: i32 = 0;
let five_million: u64 = 5_000_000;
let zero_point_two: f64 = 0.2;
let mut negative_two_point_five = Decimal::from(-25);
negative_two_point_five.multiply_pow10(-1);
// Localize those decimals into a human-readable number
println!("Zero: {}", zero.message_for_locale(&DEFAULT_LOCALE));
println!(
"Five million: {}",
five_million.message_for_locale(&DEFAULT_LOCALE)
);
println!(
"Zero point two: {}",
zero_point_two.message_for_locale(&DEFAULT_LOCALE)
);
println!(
"Negative two point five: {}",
negative_two_point_five.message_for_locale(&DEFAULT_LOCALE)
);
Ok(())
}