#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PaginationDisplayFormat {
#[default]
Locale,
Plain,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DataTableLocale {
pub footer_rows: String,
pub pagination_range: String,
pub rows_per_page: String,
pub no_rows: String,
pub no_results: String,
pub loading: String,
pub infinite_end: String,
pub quick_search_placeholder: String,
}
impl Default for DataTableLocale {
fn default() -> Self {
Self {
footer_rows: "{count} rows".to_string(),
pagination_range: "{from}–{to} of {total}".to_string(),
rows_per_page: "Rows per page".to_string(),
no_rows: "No rows".to_string(),
no_results: "No rows match your filters.".to_string(),
loading: "Loading".to_string(),
infinite_end: "End of list".to_string(),
quick_search_placeholder: "Search".to_string(),
}
}
}
impl DataTableLocale {
pub fn format_footer_rows(&self, count: usize) -> String {
self.footer_rows.replace("{count}", &count.to_string())
}
pub fn format_pagination_range(
&self,
from: usize,
to: usize,
total: usize,
estimated: bool,
) -> String {
let total_str = if estimated {
format!("more than {total}")
} else {
total.to_string()
};
self.pagination_range
.replace("{from}", &from.to_string())
.replace("{to}", &to.to_string())
.replace("{total}", &total_str)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn format_pagination_range_default() {
let locale = DataTableLocale::default();
assert_eq!(
locale.format_pagination_range(1, 10, 100, false),
"1–10 of 100"
);
}
#[test]
fn format_pagination_range_estimated() {
let locale = DataTableLocale::default();
assert_eq!(
locale.format_pagination_range(1, 5, 5, true),
"1–5 of more than 5"
);
}
#[test]
fn format_pagination_range_custom_template() {
let locale = DataTableLocale {
pagination_range: "{from} à {to} sur {total}".into(),
..Default::default()
};
assert_eq!(
locale.format_pagination_range(1, 10, 100, false),
"1 à 10 sur 100"
);
}
}