Normalise locale before using it

This commit is contained in:
Alessandro Pellizzari 2018-12-09 08:02:04 +00:00
parent 7c8289ef82
commit 6b496f847c

View file

@ -104,6 +104,7 @@ pub fn format_l10n<'a, I>(
where where
I: Iterator<Item = Item<'a>>, I: Iterator<Item = Item<'a>>,
{ {
let locale = locale.to_lowercase().replace("_", "-");
for item in items { for item in items {
match item { match item {
Item::Literal(s) | Item::Space(s) => write!(w, "{}", s)?, Item::Literal(s) | Item::Space(s) => write!(w, "{}", s)?,
@ -188,12 +189,12 @@ where
} }
let ret = match spec { let ret = match spec {
ShortMonthName => date.map(|d| write!(w, "{}", short_month(d.month0() as usize, locale))), ShortMonthName => date.map(|d| write!(w, "{}", short_month(d.month0() as usize, &locale))),
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, "{}", 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 + 2, locale))), 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 {
@ -232,9 +233,9 @@ where
write!( write!(
w, w,
"{}, {:2} {} {:04} {:02}:{:02}:{:02} ", "{}, {:2} {} {:04} {:02}:{:02}:{:02} ",
short_weekday(d.weekday().num_days_from_monday() as usize, locale), short_weekday(d.weekday().num_days_from_monday() as usize, &locale),
d.day(), d.day(),
short_month(d.month0() as usize, locale), short_month(d.month0() as usize, &locale),
d.year(), d.year(),
t.hour(), t.hour(),
t.minute(), t.minute(),
@ -296,12 +297,16 @@ fn find_key(key: usize, data: &'static HashMap<String, Vec<&'static str>>, local
data.get(locale) data.get(locale)
.and_then(|res| res.get(key)) .and_then(|res| res.get(key))
.or_else(|| { .or_else(|| {
if locale.contains("-") {
locale locale
.split(|c| c == '-' || c == '_') .split('-')
.collect::<Vec<&str>>() .collect::<Vec<&str>>()
.get(0) .get(0)
.cloned() .cloned()
.and_then(|locale| data.get(locale).and_then(|res| res.get(key))) .and_then(|locale| data.get(locale).and_then(|res| res.get(key)))
} else {
None
}
}) })
.or_else(|| data.get("C").and_then(|res| res.get(key))) .or_else(|| data.get("C").and_then(|res| res.get(key)))
} }