mirror of
https://github.com/0x5eal/chrono-lc.git
synced 2024-12-12 12:50:36 +00:00
Chenge Locales structure and implement dynamic translation
This commit is contained in:
parent
33def591ec
commit
c70481d776
2 changed files with 50 additions and 68 deletions
43
src/lib.rs
43
src/lib.rs
|
@ -145,7 +145,7 @@ where
|
||||||
),
|
),
|
||||||
|
|
||||||
// for the future expansion
|
// for the future expansion
|
||||||
Internal(ref int) => (1, None),
|
Internal(_) => (1, None),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(v) = v {
|
if let Some(v) = v {
|
||||||
|
@ -272,46 +272,37 @@ where
|
||||||
Ok(())
|
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 {
|
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 {
|
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 {
|
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 {
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::LocaleDate;
|
use super::LocaleDate;
|
||||||
use super::*;
|
|
||||||
|
|
||||||
// This test is copied from chrono's, disabling unsupported features
|
// This test is copied from chrono's, disabling unsupported features
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -2,26 +2,25 @@
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub type Locales = HashMap<String, Locale>;
|
pub struct Locales {
|
||||||
|
pub short_months: HashMap<String, Vec<&'static str>>,
|
||||||
pub struct Locale {
|
pub long_months: HashMap<String, Vec<&'static str>>,
|
||||||
short_months: Vec<String>,
|
pub short_weekdays: HashMap<String, Vec<&'static str>>,
|
||||||
long_months: Vec<String>,
|
pub long_weekdays: HashMap<String, Vec<&'static str>>,
|
||||||
short_weekdays: Vec<String>,
|
|
||||||
long_weekdays: Vec<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref LOCALES: Locales = {
|
pub static ref LOCALES: Locales = {
|
||||||
let mut res = HashMap::new();
|
let mut res = Locales{
|
||||||
res.insert(
|
short_months: HashMap::new(),
|
||||||
"en".into(),
|
long_months: HashMap::new(),
|
||||||
Locale {
|
short_weekdays: HashMap::new(),
|
||||||
short_months: vec!["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
|
long_weekdays: HashMap::new(),
|
||||||
.into_iter()
|
};
|
||||||
.map(|s| s.to_owned())
|
|
||||||
.collect::<Vec<String>>(),
|
res.short_months.insert("C".into(), vec!["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]);
|
||||||
long_months: vec![
|
|
||||||
|
res.long_months.insert("C".into(), vec![
|
||||||
"January",
|
"January",
|
||||||
"February",
|
"February",
|
||||||
"March",
|
"March",
|
||||||
|
@ -34,19 +33,11 @@ lazy_static! {
|
||||||
"October",
|
"October",
|
||||||
"November",
|
"November",
|
||||||
"December",
|
"December",
|
||||||
].into_iter()
|
]);
|
||||||
.map(|s| s.to_owned())
|
|
||||||
.collect::<Vec<String>>(),
|
res.short_weekdays.insert("C".into(), vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]);
|
||||||
short_weekdays: vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
|
|
||||||
.into_iter()
|
res.long_weekdays.insert("C".into(), vec!["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]);
|
||||||
.map(|s| s.to_owned())
|
|
||||||
.collect::<Vec<String>>(),
|
|
||||||
long_weekdays: vec!["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
|
|
||||||
.into_iter()
|
|
||||||
.map(|s| s.to_owned())
|
|
||||||
.collect::<Vec<String>>(),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
res
|
res
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue