//! Implementations of `Localize` for various layout helpers
use icu_locale::Locale;
use crate::Localize;
use crate::list::Length;
pub struct Lines<L: Localize, const SEPARATOR_COUNT: usize = 1> {
messages: Vec<L>,
}
impl<L: Localize, const SEPARATOR_COUNT: usize> Lines<L, SEPARATOR_COUNT> {
pub fn new(messages: Vec<L>) -> Self {
Self { messages }
}
}
impl<L: Localize, const SEPARATOR_COUNT: usize> Length for Lines<L, SEPARATOR_COUNT> {
fn len(&self) -> usize {
self.messages.len()
}
}
impl<L: Localize, const SEPARATOR_COUNT: usize> Localize for Lines<L, SEPARATOR_COUNT> {
fn localize_for(&self, locale: &Locale) -> String {
let localized_items: Vec<String> = self
.messages
.iter()
.map(|item| item.localize_for(locale))
.collect();
localized_items.join(&"\n".repeat(SEPARATOR_COUNT))
}
}
pub struct Padded<L: Localize, const VERTICAL_PADDING: usize, const HORIZONTAL_PADDING: usize> {
message: L,
}
impl<L: Localize, const VERTICAL_PADDING: usize, const HORIZONTAL_PADDING: usize>
Padded<L, VERTICAL_PADDING, HORIZONTAL_PADDING>
{
pub fn new(message: L) -> Self {
Self { message }
}
}
impl<L: Localize, const VERTICAL_PADDING: usize, const HORIZONTAL_PADDING: usize> Localize
for Padded<L, VERTICAL_PADDING, HORIZONTAL_PADDING>
{
fn canonical_locale(&self) -> Locale {
self.message.canonical_locale()
}
fn available_locales(&self) -> Vec<Locale> {
self.message.available_locales()
}
fn localize_for(&self, locale: &Locale) -> String {
let mut output = String::new();
// Only apply the vertical padding above the message, not below
output.push_str(&"\n".repeat(VERTICAL_PADDING));
let localized_message = self.message.localize_for(locale);
// Apply the horizontal padding to each line
let horizontal_padding = " ".repeat(HORIZONTAL_PADDING);
for (index, line) in localized_message.lines().enumerate() {
// Add a newline on every additional line
if index > 0 {
output.push('\n');
}
output.push_str(&horizontal_padding);
output.push_str(line);
}
output
}
}