Allow to localise AM and PM and rename en to C

This commit is contained in:
Alessandro Pellizzari 2018-12-06 09:17:18 +00:00
parent 770653e2a1
commit d4c00f1892
5 changed files with 36 additions and 33 deletions

View file

@ -18,6 +18,7 @@ pub struct Locale {
long_months: Option<Vec<String>>, long_months: Option<Vec<String>>,
short_weekdays: Option<Vec<String>>, short_weekdays: Option<Vec<String>>,
long_weekdays: Option<Vec<String>>, long_weekdays: Option<Vec<String>>,
ampm: Option<Vec<String>>,
} }
fn main() { fn main() {
@ -32,38 +33,8 @@ fn main() {
long_months: HashMap::new(), long_months: HashMap::new(),
short_weekdays: HashMap::new(), short_weekdays: HashMap::new(),
long_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()); "#####.as_bytes());
println!("Building..."); println!("Building...");
@ -139,6 +110,20 @@ fn main() {
).as_bytes()).unwrap(); ).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::<Vec<String>>()
.join(",")
).as_bytes()).unwrap();
}
}
} }
} }

View file

@ -44,5 +44,9 @@
"Friday", "Friday",
"Saturday", "Saturday",
"Sunday" "Sunday"
],
"ampm": [
"am",
"pm"
] ]
} }

View file

@ -44,5 +44,9 @@
"Venerdí", "Venerdí",
"Sabato", "Sabato",
"Domenica" "Domenica"
],
"ampm": [
"am",
"pm"
] ]
} }

View file

@ -191,8 +191,8 @@ where
LongMonthName => date.map(|d| write!(w, "{}", long_month(d.month0() as usize, locale))), 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))), 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))), 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" })), LowerAmPm => time.map(|t| write!(w, "{}", ampm(t.hour12().0 as usize, locale))),
UpperAmPm => time.map(|t| write!(w, "{}", if t.hour12().0 { "PM" } else { "AM" })), UpperAmPm => time.map(|t| write!(w, "{}", ampm(t.hour12().0 as usize, locale).to_uppercase())),
Nanosecond => time.map(|t| { Nanosecond => time.map(|t| {
let nano = t.nanosecond() % 1_000_000_000; let nano = t.nanosecond() % 1_000_000_000;
if nano == 0 { 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())) .and_then(|res| res.get(day).map(|v| v.to_string()))
.unwrap_or_else(|| format!("{}", day)) .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))
}

View file

@ -6,6 +6,7 @@ pub struct Locales {
pub long_months: HashMap<String, Vec<&'static str>>, pub long_months: HashMap<String, Vec<&'static str>>,
pub short_weekdays: HashMap<String, Vec<&'static str>>, pub short_weekdays: HashMap<String, Vec<&'static str>>,
pub long_weekdays: HashMap<String, Vec<&'static str>>, pub long_weekdays: HashMap<String, Vec<&'static str>>,
pub ampm: HashMap<String, Vec<&'static str>>,
} }
include!(concat!(env!("OUT_DIR"), "/locales.rs")); include!(concat!(env!("OUT_DIR"), "/locales.rs"));