From 201f9fec06710b5a24445b66ebc786a1b00b7c5b Mon Sep 17 00:00:00 2001 From: Alessandro Pellizzari Date: Sun, 2 Dec 2018 16:30:21 +0000 Subject: [PATCH] Localisations in an HashMap --- Cargo.toml | 6 +++++ build.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ locales/en.json | 48 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 4 ++++ src/locales.rs | 53 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 build.rs create mode 100644 locales/en.json create mode 100644 src/locales.rs diff --git a/Cargo.toml b/Cargo.toml index 6bb90cf..ca9a478 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,9 @@ authors = ["Alessandro Pellizzari "] [dependencies] chrono = "0.4" num-integer = { version = "0.1", default-features = false } +lazy_static = "1.2" + +[build-dependencies] +serde = "1" +serde_json = "1" +walkdir = "2" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..a9c7dc5 --- /dev/null +++ b/build.rs @@ -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, + long_months: Vec, + short_weekdays: Vec, + long_weekdays: Vec, +} + + +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; + + pub struct Locale { + short_months: Vec, + long_months: Vec, + short_weekdays: Vec, + long_weekdays: Vec, + } + + 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(); +} diff --git a/locales/en.json b/locales/en.json new file mode 100644 index 0000000..1f6e1c4 --- /dev/null +++ b/locales/en.json @@ -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" + ] +} diff --git a/src/lib.rs b/src/lib.rs index e0ff488..65fcc3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#[macro_use] +extern crate lazy_static; extern crate chrono; 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 num_integer::{div_floor, mod_floor}; +mod locales; + pub trait LocaleDate { fn formatl<'a>(&self, fmt: &'a str, locale: &str) -> DelayedFormatL10n>; } diff --git a/src/locales.rs b/src/locales.rs new file mode 100644 index 0000000..6a29998 --- /dev/null +++ b/src/locales.rs @@ -0,0 +1,53 @@ +// This file will be overwritten by build.rs during compilation + +use std::collections::HashMap; + +pub type Locales = HashMap; + +pub struct Locale { + short_months: Vec, + long_months: Vec, + short_weekdays: Vec, + long_weekdays: Vec, +} + +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::>(), + long_months: vec![ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December", + ].into_iter() + .map(|s| s.to_owned()) + .collect::>(), + short_weekdays: vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] + .into_iter() + .map(|s| s.to_owned()) + .collect::>(), + long_weekdays: vec!["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] + .into_iter() + .map(|s| s.to_owned()) + .collect::>(), + }, + ); + + res + }; +}