Instead of implicitly localizing a Vec
by joining with a newline, the new List
type forwards to icu_list
to create single-line localized lists.
Q7LUHXXBN3ACNMGT4O2SKY5EGDEJQXMVJGYETTKNWIUAZCQV6HGAC
7M4UI3TWQIAA333GQ577HDWDWZPSZKWCYG556L6SBRLB6SZDQYPAC
RUCC2HKZZTUHN3G6IWS4NK3VYGXAI6PORJH2YZKPRAYSDWH63ESQC
USKESL6XR6C7676X3PO3SFFL5EMKMA7EQMPZAA72A7F7UZSONOIQC
X6AMFX3NNMVV7NXWT3KM6IEXE3UX67W7C7NPPVZA7YH2B3F6NOPAC
2HHBS7VWRQRDNDCCSV3BJJJHA7LGN3L4CMKIP7URWQFXPI6QFNDQC
UKFEFT6LSI4K7X6UHQFZYD52DILKXMZMYSO2UYS2FCHNPXIF4BEQC
VZYZRAO4EXCHW2LBVFG5ELSWG5SCNDREMJ6RKQ4EKQGI2T7SD3ZQC
HHJDRLLNN36UNIA7STAXEEVBCEMPJNB7SJQOS3TJLLYN4AEZ4MHQC
macro_rules! impl_list {
($list_type:ty) => {
impl<T: Localize> Localize for $list_type {
fn localize_for(&self, locale: &Locale) -> String {
let localized_items: Vec<String> =
self.iter().map(|item| item.localize_for(locale)).collect();
localized_items.join("\n")
}
}
};
pub trait Length {
fn len(&self) -> usize;
}
pub struct List<L: Localize> {
messages: Vec<L>,
length: ListLength,
impl_list!(Vec<T>);
impl_list!(&[T]);
impl<L: Localize> List<L> {
pub fn new(messages: Vec<L>, length: ListLength) -> Self {
Self { messages, length }
}
}
impl<L: Localize> Length for List<L> {
fn len(&self) -> usize {
self.messages.len()
}
}
impl<L: Localize> Localize for List<L> {
fn localize_for(&self, locale: &Locale) -> String {
let list_formatter = ListFormatter::try_new_and(
locale.into(),
ListFormatterOptions::default().with_length(self.length),
)
.unwrap();
let localized_messages = self
.messages
.iter()
.map(|message| message.localize_for(locale));
list_formatter.format(localized_messages).to_string()
}
}
//! Example showing how to localize lists
use icu_locale::{Locale, locale};
use l10n_embed::Localize;
use l10n_embed::list::{List, ListLength};
const DEFAULT_LOCALE: Locale = locale!("en-US");
fn main() {
let items = vec!["item 1", "item 2", "item 3", "item 4"];
// Create some lists of different lengths
let narrow_list = List::new(items.clone(), ListLength::Narrow);
let short_list = List::new(items.clone(), ListLength::Short);
let wide_list = List::new(items.clone(), ListLength::Wide);
// Localize these lists
println!("Narrow list: {}", narrow_list.localize_for(&DEFAULT_LOCALE));
println!("Short list: {}", short_list.localize_for(&DEFAULT_LOCALE));
println!("Wide list: {}", wide_list.localize_for(&DEFAULT_LOCALE));
}