From d4c00f18921e81469e2934207742ceeec22ca9bf Mon Sep 17 00:00:00 2001 From: Alessandro Pellizzari Date: Thu, 6 Dec 2018 09:17:18 +0000 Subject: [PATCH] Allow to localise AM and PM and rename en to C --- build.rs | 47 +++++++++++++------------------------ locales/{en.json => C.json} | 4 ++++ locales/it.json | 4 ++++ src/lib.rs | 13 ++++++++-- src/locales.rs | 1 + 5 files changed, 36 insertions(+), 33 deletions(-) rename locales/{en.json => C.json} (94%) diff --git a/build.rs b/build.rs index 7f342ac..fc85708 100644 --- a/build.rs +++ b/build.rs @@ -18,6 +18,7 @@ pub struct Locale { long_months: Option>, short_weekdays: Option>, long_weekdays: Option>, + ampm: Option>, } fn main() { @@ -32,38 +33,8 @@ fn main() { long_months: HashMap::new(), short_weekdays: HashMap::new(), long_weekdays: HashMap::new(), + ampm: 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"], - ); "#####.as_bytes()); println!("Building..."); @@ -139,6 +110,20 @@ fn main() { ).as_bytes()).unwrap(); } } + + if let Some(ampm) = locale_data.ampm { + if ampm.len() == 2 { + let _ = f.write_all(format!( + "res.ampm.insert(\"{}\".into(), vec![{}]);\n", + locale_name, + ampm + .iter() + .map(|s| format!("\"{}\"", s)) + .collect::>() + .join(",") + ).as_bytes()).unwrap(); + } + } } } diff --git a/locales/en.json b/locales/C.json similarity index 94% rename from locales/en.json rename to locales/C.json index 1f6e1c4..ee37b53 100644 --- a/locales/en.json +++ b/locales/C.json @@ -44,5 +44,9 @@ "Friday", "Saturday", "Sunday" + ], + "ampm": [ + "am", + "pm" ] } diff --git a/locales/it.json b/locales/it.json index d94535c..5344723 100644 --- a/locales/it.json +++ b/locales/it.json @@ -44,5 +44,9 @@ "Venerdí", "Sabato", "Domenica" + ], + "ampm": [ + "am", + "pm" ] } diff --git a/src/lib.rs b/src/lib.rs index e3a3239..194e361 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -191,8 +191,8 @@ where LongMonthName => date.map(|d| write!(w, "{}", long_month(d.month0() as usize, locale))), ShortWeekdayName => date.map(|d| write!(w, "{}", short_weekday(d.weekday().num_days_from_monday() as usize, locale))), LongWeekdayName => date.map(|d| write!(w, "{}", long_weekday(d.weekday().num_days_from_monday() as usize, locale))), - LowerAmPm => time.map(|t| write!(w, "{}", if t.hour12().0 { "pm" } else { "am" })), - UpperAmPm => time.map(|t| write!(w, "{}", if t.hour12().0 { "PM" } else { "AM" })), + LowerAmPm => time.map(|t| write!(w, "{}", ampm(t.hour12().0 as usize, locale))), + UpperAmPm => time.map(|t| write!(w, "{}", ampm(t.hour12().0 as usize, locale).to_uppercase())), Nanosecond => time.map(|t| { let nano = t.nanosecond() % 1_000_000_000; if nano == 0 { @@ -306,3 +306,12 @@ fn long_weekday(day: usize, locale: &str) -> String { .and_then(|res| res.get(day).map(|v| v.to_string())) .unwrap_or_else(|| format!("{}", day)) } + +fn ampm(spec: usize, locale: &str) -> String { + locales::LOCALES + .ampm + .get(locale) + .or_else(|| locales::LOCALES.ampm.get("C")) + .and_then(|res| res.get(spec).map(|v| v.to_string())) + .unwrap_or_else(|| format!("{}", spec)) +} diff --git a/src/locales.rs b/src/locales.rs index fee07fa..0504a9c 100644 --- a/src/locales.rs +++ b/src/locales.rs @@ -6,6 +6,7 @@ pub struct Locales { pub long_months: HashMap>, pub short_weekdays: HashMap>, pub long_weekdays: HashMap>, + pub ampm: HashMap>, } include!(concat!(env!("OUT_DIR"), "/locales.rs"));