From c70481d776bbc386691e7d646a496306ce8fc184 Mon Sep 17 00:00:00 2001 From: Alessandro Pellizzari Date: Mon, 3 Dec 2018 09:39:38 +0000 Subject: [PATCH] Chenge Locales structure and implement dynamic translation --- src/lib.rs | 43 ++++++++++++----------------- src/locales.rs | 75 ++++++++++++++++++++++---------------------------- 2 files changed, 50 insertions(+), 68 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 65fcc3e..6fc863a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,7 +145,7 @@ where ), // for the future expansion - Internal(ref int) => (1, None), + Internal(_) => (1, None), }; if let Some(v) = v { @@ -272,46 +272,37 @@ where Ok(()) } -// TODO: get values from localised arrays - -static SHORT_MONTHS: [&'static str; 12] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; -static LONG_MONTHS: [&'static str; 12] = [ - "January", - "February", - "March", - "April", - "May", - "June", - "July", - "August", - "September", - "October", - "November", - "December", -]; -static SHORT_WEEKDAYS: [&'static str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; -static LONG_WEEKDAYS: [&'static str; 7] = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; - fn short_month(month: usize, locale: &str) -> String { - SHORT_MONTHS[month].to_owned() + let res = locales::LOCALES.short_months.get(locale) + .or_else(|| locales::LOCALES.short_months.get("C")) + .unwrap(); + res.get(month).map(|v| v.to_string()).unwrap_or_else(|| format!("{}", month)) } fn long_month(month: usize, locale: &str) -> String { - LONG_MONTHS[month].to_owned() + let res = locales::LOCALES.long_months.get(locale) + .or_else(|| locales::LOCALES.long_months.get("C")) + .unwrap(); + res.get(month).map(|v| v.to_string()).unwrap_or_else(|| format!("{}", month)) } fn short_weekday(day: usize, locale: &str) -> String { - SHORT_WEEKDAYS[day].to_owned() + let res = locales::LOCALES.short_weekdays.get(locale) + .or_else(|| locales::LOCALES.short_weekdays.get("C")) + .unwrap(); + res.get(day).map(|v| v.to_string()).unwrap_or_else(|| format!("{}", day)) } fn long_weekday(day: usize, locale: &str) -> String { - LONG_WEEKDAYS[day].to_owned() + let res = locales::LOCALES.long_weekdays.get(locale) + .or_else(|| locales::LOCALES.long_weekdays.get("C")) + .unwrap(); + res.get(day).map(|v| v.to_string()).unwrap_or_else(|| format!("{}", day)) } #[cfg(test)] mod tests { use super::LocaleDate; - use super::*; // This test is copied from chrono's, disabling unsupported features #[test] diff --git a/src/locales.rs b/src/locales.rs index 6a29998..1b0b67d 100644 --- a/src/locales.rs +++ b/src/locales.rs @@ -2,51 +2,42 @@ use std::collections::HashMap; -pub type Locales = HashMap; - -pub struct Locale { - short_months: Vec, - long_months: Vec, - short_weekdays: Vec, - long_weekdays: Vec, +pub struct Locales { + pub short_months: HashMap>, + pub long_months: HashMap>, + pub short_weekdays: HashMap>, + pub long_weekdays: HashMap>, } lazy_static! { - static ref LOCALES: Locales = { - let mut res = HashMap::new(); - res.insert( - "en".into(), - Locale { - short_months: vec!["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] - .into_iter() - .map(|s| s.to_owned()) - .collect::>(), - long_months: vec![ - "January", - "February", - "March", - "April", - "May", - "June", - "July", - "August", - "September", - "October", - "November", - "December", - ].into_iter() - .map(|s| s.to_owned()) - .collect::>(), - short_weekdays: vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] - .into_iter() - .map(|s| s.to_owned()) - .collect::>(), - long_weekdays: vec!["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] - .into_iter() - .map(|s| s.to_owned()) - .collect::>(), - }, - ); + pub static ref LOCALES: Locales = { + let mut res = Locales{ + short_months: HashMap::new(), + long_months: HashMap::new(), + short_weekdays: HashMap::new(), + long_weekdays: HashMap::new(), + }; + + res.short_months.insert("C".into(), vec!["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]); + + res.long_months.insert("C".into(), vec![ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December", + ]); + + res.short_weekdays.insert("C".into(), vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]); + + res.long_weekdays.insert("C".into(), vec!["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]); res };