string.rs
//! Example showing how to localize strings
use std::borrow::Cow;
use icu_locale::locale;
use l10n_embed::{Context, Localize};
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
let strings: [(&str, Box<dyn Localize>); 3] = [
("Static string", Box::new(static_str)),
("Heap allocated string", Box::new(heap_allocated_string)),
("Copy on write string", Box::new(copy_on_write_str)),
];
let context = Context::new(locale!("en-US"), true);
let mut buffer = String::new();
for (name, string) in strings {
buffer.clear();
string.localize(&context, &mut buffer);
println!("{name}: {buffer}");
}
}