Add capitalised am/pm and improve translation search

This commit is contained in:
Alessandro Pellizzari 2018-12-09 07:26:02 +00:00
parent 91d300149c
commit ac3a7cc907
3 changed files with 25 additions and 23 deletions

View file

@ -110,7 +110,7 @@ fn main() {
} }
if let Some(ampm) = locale_data.ampm { if let Some(ampm) = locale_data.ampm {
if ampm.len() == 2 { if ampm.len() == 2 || ampm.len() == 4 {
let _ = f let _ = f
.write_all( .write_all(
format!( format!(

View file

@ -47,6 +47,8 @@
], ],
"ampm": [ "ampm": [
"am", "am",
"pm" "pm",
"AM",
"PM"
] ]
} }

View file

@ -192,7 +192,7 @@ where
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, "{}", ampm(t.hour12().0 as usize, locale))), 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())), UpperAmPm => time.map(|t| write!(w, "{}", ampm(t.hour12().0 as usize + 2, locale))),
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 {
@ -271,47 +271,47 @@ where
Ok(()) Ok(())
} }
fn short_month(month: usize, locale: &str) -> String { fn short_month(month: usize, locale: &str) -> &'static str {
locales::LOCALES locales::LOCALES
.short_months .short_months
.get(locale) .get(locale)
.or_else(|| locales::LOCALES.short_months.get("C")) .and_then(|res| res.get(month))
.and_then(|res| res.get(month).map(|v| v.to_string())) .or_else(|| locales::LOCALES.short_months.get("C").and_then(|res| res.get(month)))
.unwrap_or_else(|| format!("{}", month)) .expect("Internal error: missing short months in the C locale")
} }
fn long_month(month: usize, locale: &str) -> String { fn long_month(month: usize, locale: &str) -> &'static str {
locales::LOCALES locales::LOCALES
.long_months .long_months
.get(locale) .get(locale)
.or_else(|| locales::LOCALES.long_months.get("C")) .and_then(|res| res.get(month))
.and_then(|res| res.get(month).map(|v| v.to_string())) .or_else(|| locales::LOCALES.long_months.get("C").and_then(|res| res.get(month)))
.unwrap_or_else(|| format!("{}", month)) .expect("Internal error: missing long months in the C locale")
} }
fn short_weekday(day: usize, locale: &str) -> String { fn short_weekday(day: usize, locale: &str) -> &'static str {
locales::LOCALES locales::LOCALES
.short_weekdays .short_weekdays
.get(locale) .get(locale)
.or_else(|| locales::LOCALES.short_weekdays.get("C")) .and_then(|res| res.get(day))
.and_then(|res| res.get(day).map(|v| v.to_string())) .or_else(|| locales::LOCALES.short_weekdays.get("C").and_then(|res| res.get(day)))
.unwrap_or_else(|| format!("{}", day)) .expect("Internal error: missing short weekdays in the C locale")
} }
fn long_weekday(day: usize, locale: &str) -> String { fn long_weekday(day: usize, locale: &str) -> &'static str {
locales::LOCALES locales::LOCALES
.long_weekdays .long_weekdays
.get(locale) .get(locale)
.or_else(|| locales::LOCALES.long_weekdays.get("C")) .and_then(|res| res.get(day))
.and_then(|res| res.get(day).map(|v| v.to_string())) .or_else(|| locales::LOCALES.long_weekdays.get("C").and_then(|res| res.get(day)))
.unwrap_or_else(|| format!("{}", day)) .expect("Internal error: missing long weekdays in the C locale")
} }
fn ampm(spec: usize, locale: &str) -> String { fn ampm(spec: usize, locale: &str) -> &'static str {
locales::LOCALES locales::LOCALES
.ampm .ampm
.get(locale) .get(locale)
.or_else(|| locales::LOCALES.ampm.get("C")) .and_then(|res| res.get(spec))
.and_then(|res| res.get(spec).map(|v| v.to_string())) .or_else(|| locales::LOCALES.ampm.get("C").and_then(|res| res.get(spec)))
.unwrap_or_else(|| format!("{}", spec)) .expect("Internal error: missing AM/PM in the C locale")
} }