UPDATE: Dump of initial files
This commit is contained in:
78
generate_locale/src/constants.rs
Normal file
78
generate_locale/src/constants.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const LOCAL_TYPE: &str = "modern"; // or "full"
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Locale {
|
||||
pub dates: Dates,
|
||||
pub numbers: NumbersProperties,
|
||||
pub currency: Currency
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Currency {
|
||||
pub iso: String,
|
||||
pub symbol: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct NumbersProperties {
|
||||
#[serde(rename = "symbols-numberSystem-latn")]
|
||||
pub symbols: NumbersSymbols,
|
||||
#[serde(rename = "decimalFormats-numberSystem-latn")]
|
||||
pub decimal_formats: DecimalFormats,
|
||||
#[serde(rename = "currencyFormats-numberSystem-latn")]
|
||||
pub currency_formats: CurrencyFormats,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Dates {
|
||||
pub day_names: Vec<String>,
|
||||
pub day_names_short: Vec<String>,
|
||||
pub months: Vec<String>,
|
||||
pub months_short: Vec<String>,
|
||||
pub months_letter: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct NumbersSymbols {
|
||||
pub decimal: String,
|
||||
pub group: String,
|
||||
pub list: String,
|
||||
pub percent_sign: String,
|
||||
pub plus_sign: String,
|
||||
pub minus_sign: String,
|
||||
pub approximately_sign: String,
|
||||
pub exponential: String,
|
||||
pub superscripting_exponent: String,
|
||||
pub per_mille: String,
|
||||
pub infinity: String,
|
||||
pub nan: String,
|
||||
pub time_separator: String,
|
||||
}
|
||||
|
||||
|
||||
|
||||
// See: https://cldr.unicode.org/translation/number-currency-formats/number-and-currency-patterns
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct CurrencyFormats {
|
||||
pub standard: String,
|
||||
#[serde(rename = "standard-alphaNextToNumber")]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub standard_alpha_next_to_number: Option<String>,
|
||||
#[serde(rename = "standard-noCurrency")]
|
||||
pub standard_no_currency: String,
|
||||
pub accounting: String,
|
||||
#[serde(rename = "accounting-alphaNextToNumber")]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub accounting_alpha_next_to_number: Option<String>,
|
||||
#[serde(rename = "accounting-noCurrency")]
|
||||
pub accounting_no_currency: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DecimalFormats {
|
||||
pub standard: String,
|
||||
}
|
||||
83
generate_locale/src/dates.rs
Normal file
83
generate_locale/src/dates.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::constants::{Dates, LOCAL_TYPE};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CaGCalendarsFormat {
|
||||
format: HashMap<String, HashMap<String, String>>,
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CaGCalendarsII {
|
||||
months: CaGCalendarsFormat,
|
||||
days: CaGCalendarsFormat,
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CaGCalendarsI {
|
||||
gregorian: CaGCalendarsII,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CaGCalendars {
|
||||
calendars: CaGCalendarsI,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CaGId {
|
||||
identity: Value,
|
||||
dates: CaGCalendars,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CaGregorian {
|
||||
main: HashMap<String, CaGId>,
|
||||
}
|
||||
|
||||
pub fn get_dates_formatting(cldr_dir: &str, locale_id: &str) -> Result<Dates, &'static str> {
|
||||
let calendar_file = format!(
|
||||
"{}cldr-json/cldr-dates-{}/main/{}/ca-gregorian.json",
|
||||
cldr_dir, LOCAL_TYPE, locale_id
|
||||
);
|
||||
|
||||
let contents =
|
||||
fs::read_to_string(calendar_file).or(Err("Failed reading 'ca-gregorian' file"))?;
|
||||
let ca_gregorian: CaGregorian =
|
||||
serde_json::from_str(&contents).or(Err("Failed parsing 'ca-gregorian' file"))?;
|
||||
let gregorian = &ca_gregorian.main[locale_id].dates.calendars.gregorian;
|
||||
// See: http://cldr.unicode.org/translation/date-time-1/date-time-patterns
|
||||
// for the difference between stand-alone and format. We will use only the format mode
|
||||
let months_format = &gregorian.months.format;
|
||||
let days_format = &gregorian.days.format;
|
||||
let mut day_names = vec![];
|
||||
let mut day_names_short = vec![];
|
||||
|
||||
let mut months = vec![];
|
||||
let mut months_short = vec![];
|
||||
let mut months_letter = vec![];
|
||||
|
||||
let month_index = vec![
|
||||
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
|
||||
];
|
||||
for index in month_index {
|
||||
months_letter.push(months_format["narrow"][index].to_owned());
|
||||
months_short.push(months_format["abbreviated"][index].to_owned());
|
||||
months.push(months_format["wide"][index].to_owned());
|
||||
}
|
||||
|
||||
let day_index = vec!["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
|
||||
for day in day_index {
|
||||
day_names_short.push(days_format["abbreviated"][day].to_owned());
|
||||
day_names.push(days_format["wide"][day].to_owned());
|
||||
}
|
||||
|
||||
Ok(Dates {
|
||||
day_names,
|
||||
day_names_short,
|
||||
months,
|
||||
months_short,
|
||||
months_letter,
|
||||
})
|
||||
}
|
||||
61
generate_locale/src/main.rs
Normal file
61
generate_locale/src/main.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use std::fs;
|
||||
use std::{collections::HashMap, io::Write, path::PathBuf};
|
||||
|
||||
use constants::{Locale, Currency};
|
||||
|
||||
use clap::Parser;
|
||||
use numbers::get_numbers_formatting;
|
||||
|
||||
mod constants;
|
||||
mod dates;
|
||||
mod numbers;
|
||||
mod util;
|
||||
|
||||
use dates::get_dates_formatting;
|
||||
use util::get_all_locales_id;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[clap(author, version, about, long_about = None)]
|
||||
pub struct Opt {
|
||||
/// File with the list of required locales
|
||||
#[clap(long, value_parser)]
|
||||
locales: Option<PathBuf>,
|
||||
|
||||
/// Folder with the cldr data
|
||||
#[clap(long, value_parser)]
|
||||
cldr_dir: String,
|
||||
|
||||
/// output json file with all locale info
|
||||
#[clap(long, value_parser)]
|
||||
output: PathBuf,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), String> {
|
||||
let opt = Opt::from_args();
|
||||
let cldr_dir = opt.cldr_dir;
|
||||
let locales_list: Vec<String> = if let Some(locales_path) = opt.locales {
|
||||
let contents = fs::read_to_string(locales_path).or(Err("Failed reading file"))?;
|
||||
serde_json::from_str(&contents).or(Err("Failed parsing file"))?
|
||||
} else {
|
||||
get_all_locales_id(&cldr_dir)
|
||||
};
|
||||
|
||||
let mut locales = HashMap::new();
|
||||
|
||||
for locale_id in locales_list {
|
||||
let dates = get_dates_formatting(&cldr_dir, &locale_id)?;
|
||||
let numbers = get_numbers_formatting(&cldr_dir, &locale_id)?;
|
||||
// HACK: the currency is not a part of the cldr locale
|
||||
// We just stick here one and make this adaptable in the calc module for now
|
||||
let currency = Currency {
|
||||
iso: "USD".to_string(),
|
||||
symbol: "$".to_string()
|
||||
};
|
||||
locales.insert(locale_id, Locale { dates, numbers, currency });
|
||||
}
|
||||
|
||||
let s = serde_json::to_string(&locales).or(Err("Failed to stringify data"))?;
|
||||
let mut f = fs::File::create(opt.output).or(Err("Failed to create file"))?;
|
||||
f.write_all(s.as_bytes()).or(Err("Failed writing"))?;
|
||||
Ok(())
|
||||
}
|
||||
65
generate_locale/src/numbers.rs
Normal file
65
generate_locale/src/numbers.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::constants::{NumbersProperties, LOCAL_TYPE};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CaGCalendarsFormat {
|
||||
format: HashMap<String, HashMap<String, String>>,
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CaGCalendarsII {
|
||||
months: CaGCalendarsFormat,
|
||||
days: CaGCalendarsFormat,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct NumbersJSONId {
|
||||
identity: Value,
|
||||
numbers: NumbersProperties,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct NumbersJSON {
|
||||
main: HashMap<String, NumbersJSONId>,
|
||||
}
|
||||
|
||||
pub fn get_numbers_formatting(
|
||||
cldr_dir: &str,
|
||||
locale_id: &str,
|
||||
) -> Result<NumbersProperties, String> {
|
||||
let numbers_file = format!(
|
||||
"{}cldr-json/cldr-numbers-{}/main/{}/numbers.json",
|
||||
cldr_dir, LOCAL_TYPE, locale_id
|
||||
);
|
||||
|
||||
let contents = fs::read_to_string(numbers_file).or(Err("Failed reading 'numbers.json'"))?;
|
||||
let numbers_json: &NumbersJSON =
|
||||
&serde_json::from_str(&contents).or(Err("Failed parsing 'numbers.json' file"))?;
|
||||
// Grouping is either
|
||||
// * #,##,##0.### (indian way)
|
||||
// * #,##0.### (standard)
|
||||
// * 0.###### (posix)
|
||||
// anything else is an error
|
||||
let grouping_str = &numbers_json.main[locale_id]
|
||||
.numbers
|
||||
.decimal_formats
|
||||
.standard;
|
||||
let _grouping = if grouping_str == "#,##0.###" {
|
||||
"standard"
|
||||
} else if grouping_str == "#,##,##0.###" {
|
||||
"indian"
|
||||
} else if grouping_str == "0.######" {
|
||||
"posix"
|
||||
} else {
|
||||
let message = format!(
|
||||
"Unexpected grouping {} in locale {}",
|
||||
grouping_str, locale_id
|
||||
);
|
||||
return Err(message);
|
||||
};
|
||||
Ok(numbers_json.main[locale_id].numbers.clone())
|
||||
}
|
||||
26
generate_locale/src/util.rs
Normal file
26
generate_locale/src/util.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use std::fs;
|
||||
|
||||
use crate::constants::LOCAL_TYPE;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct AlI {
|
||||
modern: Vec<String>,
|
||||
full: Vec<String>,
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct AvailableLocales {
|
||||
available_locales: AlI,
|
||||
}
|
||||
|
||||
pub fn get_all_locales_id(cldr_dir: &str) -> Vec<String> {
|
||||
let al_file = format!("{}cldr-json/cldr-core/availableLocales.json", cldr_dir);
|
||||
let contents = fs::read_to_string(al_file).unwrap();
|
||||
let locales: AvailableLocales = serde_json::from_str(&contents).unwrap();
|
||||
if LOCAL_TYPE == "modern" {
|
||||
locales.available_locales.modern
|
||||
} else {
|
||||
locales.available_locales.full
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user