Localisations in an HashMap

This commit is contained in:
Alessandro Pellizzari 2018-12-02 16:30:21 +00:00
parent 7445bea0f7
commit 201f9fec06
5 changed files with 173 additions and 0 deletions

View file

@ -6,3 +6,9 @@ authors = ["Alessandro Pellizzari <alex@amiran.it>"]
[dependencies] [dependencies]
chrono = "0.4" chrono = "0.4"
num-integer = { version = "0.1", default-features = false } num-integer = { version = "0.1", default-features = false }
lazy_static = "1.2"
[build-dependencies]
serde = "1"
serde_json = "1"
walkdir = "2"

62
build.rs Normal file
View file

@ -0,0 +1,62 @@
extern crate walkdir;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use walkdir::WalkDir;
pub struct Locale {
short_months: Vec<String>,
long_months: Vec<String>,
short_weekdays: Vec<String>,
long_weekdays: Vec<String>,
}
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("locales.rs");
let mut f = File::create(&dest_path).unwrap();
f.write_all(r###"
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>,
}
lazy_static! {
static ref LOCALES: Locales = {
let mut res = HashMap::new();
"###.as_bytes()).unwrap();
println!("Building...");
for entry in WalkDir::new("locales") {
let entry = entry.unwrap();
println!("{}", entry.path().display());
if entry.path().extension().map(|e| e != "json").unwrap_or(false) {
println!("Not a json file");
continue
}
}
f.write_all(r###"
res
};
}
"###.as_bytes()).unwrap();
}

48
locales/en.json Normal file
View file

@ -0,0 +1,48 @@
{
"short_months": [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
],
"long_months": [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
"short_weekdays": [
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun"
],
"long_weekdays": [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
]
}

View file

@ -1,3 +1,5 @@
#[macro_use]
extern crate lazy_static;
extern crate chrono; extern crate chrono;
extern crate num_integer; extern crate num_integer;
@ -7,6 +9,8 @@ use chrono::format::{Fixed, Item, Numeric, Pad, StrftimeItems};
use chrono::{Datelike, FixedOffset, NaiveDate, NaiveTime, Offset, TimeZone, Timelike}; use chrono::{Datelike, FixedOffset, NaiveDate, NaiveTime, Offset, TimeZone, Timelike};
use num_integer::{div_floor, mod_floor}; use num_integer::{div_floor, mod_floor};
mod locales;
pub trait LocaleDate { pub trait LocaleDate {
fn formatl<'a>(&self, fmt: &'a str, locale: &str) -> DelayedFormatL10n<StrftimeItems<'a>>; fn formatl<'a>(&self, fmt: &'a str, locale: &str) -> DelayedFormatL10n<StrftimeItems<'a>>;
} }

53
src/locales.rs Normal file
View file

@ -0,0 +1,53 @@
// This file will be overwritten by build.rs during compilation
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>,
}
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![
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"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
};
}