UPDATE: Dump of initial files

This commit is contained in:
Nicolás Hatcher
2023-11-18 21:26:18 +01:00
commit c5b8efd83d
279 changed files with 42654 additions and 0 deletions

1
generate_locale/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
target/*

283
generate_locale/Cargo.lock generated Normal file
View File

@@ -0,0 +1,283 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "clap"
version = "3.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"once_cell",
"strsim",
"termcolor",
"textwrap",
]
[[package]]
name = "clap_derive"
version = "3.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "generate_locale"
version = "0.1.0"
dependencies = [
"clap",
"serde",
"serde_json",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "indexmap"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "itoa"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"
[[package]]
name = "libc"
version = "0.2.132"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5"
[[package]]
name = "once_cell"
version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0"
[[package]]
name = "os_str_bytes"
version = "6.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "serde"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52205623b1b0f064a4e71182c3b18ae902267282930c6d5462c91b859668426e"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16"
[[package]]
name = "unicode-ident"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@@ -0,0 +1,12 @@
[package]
name = "generate_locale"
version = "0.1.0"
authors = ["Nicolás Hatcher <nicolas@theuniverse.today>"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
clap = { version = "3.2.22", features = ["derive"] }

26
generate_locale/README.md Normal file
View File

@@ -0,0 +1,26 @@
# Generate Locale
This is a small util to generate locales for IronCalc.
To build
```bash
$ cargo build --release
```
To run it you will need a checkout of the [CLDR json repo](https://github.com/unicode-org/cldr-json)
```bash
$ generate_locale --locales=<locales-file> --cldr-dir=<cldr-dir> --output=<output-file>
```
Further information:
http://cldr.unicode.org/
## TODO:
* Add tests
* Checkout whole folder?

View File

@@ -0,0 +1,3 @@
[
"en", "en-GB", "de", "es"
]

View 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,
}

View 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,
})
}

View 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(())
}

View 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())
}

View 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
}
}