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
|
||||
Internal(ref int) => (1, None),
|
||||
Internal(_) => (1, None),
|
||||
};
|
||||
|
||||
if let Some(v) = v {
|
||||
|
@ -272,46 +272,37 @@ where
|
|||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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)]
|
||||
mod tests {
|
||||
use super::LocaleDate;
|
||||
use super::*;
|
||||
|
||||
// This test is copied from chrono's, disabling unsupported features
|
||||
#[test]
|
||||
|
|
|
@ -2,26 +2,25 @@
|
|||
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub type Locales = HashMap<String, Locale>;
|
||||
|
||||
pub struct Locale {
|
||||
short_months: Vec<String>,
|
||||
long_months: Vec<String>,
|
||||
short_weekdays: Vec<String>,
|
||||
long_weekdays: Vec<String>,
|
||||
pub struct Locales {
|
||||
pub short_months: HashMap<String, Vec<&'static str>>,
|
||||
pub long_months: HashMap<String, Vec<&'static str>>,
|
||||
pub short_weekdays: HashMap<String, Vec<&'static str>>,
|
||||
pub long_weekdays: HashMap<String, Vec<&'static str>>,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref LOCALES: Locales = {
|
||||
let mut res = HashMap::new();
|
||||
res.insert(
|
||||
"en".into(),
|
||||
Locale {
|
||||
short_months: vec!["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
|
||||
.into_iter()
|
||||
.map(|s| s.to_owned())
|
||||
.collect::<Vec<String>>(),
|
||||
long_months: vec![
|
||||
pub static ref LOCALES: Locales = {
|
||||
let mut res = Locales{
|
||||
short_months: HashMap::new(),
|
||||
long_months: HashMap::new(),
|
||||
short_weekdays: HashMap::new(),
|
||||
long_weekdays: 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",
|
||||
|
@ -34,19 +33,11 @@ lazy_static! {
|
|||
"October",
|
||||
"November",
|
||||
"December",
|
||||
].into_iter()
|
||||
.map(|s| s.to_owned())
|
||||
.collect::<Vec<String>>(),
|
||||
short_weekdays: vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
|
||||
.into_iter()
|
||||
.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.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"]);
|
||||
|
||||
res
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue