-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from Baptistemontan/build
crate for build.rs utilities
- Loading branch information
Showing
15 changed files
with
459 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,15 @@ | ||
use icu_datagen::baked_exporter::*; | ||
use icu_datagen::prelude::*; | ||
use leptos_i18n_build::TranslationsInfos; | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
println!("cargo:rerun-if-changed=Cargo.toml"); | ||
|
||
let mod_directory = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()).join("baked_data"); | ||
|
||
// This is'nt really needed, but ICU4X wants the directory to be empty | ||
// and Rust Analyzer can trigger the build.rs without cleaning the out directory. | ||
if mod_directory.exists() { | ||
std::fs::remove_dir_all(&mod_directory).unwrap(); | ||
} | ||
let translations_infos = TranslationsInfos::parse().unwrap(); | ||
|
||
let exporter = BakedExporter::new(mod_directory, Default::default()).unwrap(); | ||
translations_infos.rerun_if_locales_changed(); | ||
|
||
DatagenDriver::new() | ||
// Keys needed for plurals | ||
.with_keys(icu_datagen::keys(&[ | ||
"plurals/cardinal@1", | ||
"plurals/ordinal@1", | ||
])) | ||
// Used locales, no fallback needed | ||
.with_locales_no_fallback([langid!("en"), langid!("fr")], Default::default()) | ||
.export(&DatagenProvider::new_latest_tested(), exporter) | ||
.unwrap(); | ||
translations_infos.generate_data(mod_directory).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "leptos_i18n_build" | ||
version = { workspace = true } | ||
edition = "2021" | ||
authors = ["Baptiste de Montangon"] | ||
license = "MIT" | ||
repository = "https://github.com/Baptistemontan/leptos_i18n" | ||
description = "build.rs utilities for the leptos_i18n crate" | ||
readme = "../README.md" | ||
|
||
[dependencies] | ||
leptos_i18n_parser = { workspace = true } | ||
icu_datagen = { version = "1.5" } | ||
icu = { version = "1.5" } | ||
icu_provider = { version = "1.5" } | ||
|
||
[features] | ||
default = ["json_files"] | ||
json_files = ["leptos_i18n_parser/json_files"] | ||
yaml_files = ["leptos_i18n_parser/yaml_files"] | ||
json5_files = ["leptos_i18n_parser/json5_files"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use std::collections::HashSet; | ||
|
||
use icu_datagen::prelude::DataKey; | ||
use leptos_i18n_parser::{ | ||
parse_locales::locale::{BuildersKeysInner, InterpolOrLit, LocaleValue, RangeOrPlural}, | ||
utils::formatter::Formatter, | ||
}; | ||
|
||
/// This enum represent the different `Fromatters` and options your translations could be using. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub enum Options { | ||
/// Use of plurals. | ||
Plurals, | ||
/// Use of the `date`, `time` or `datetime` formatter. | ||
FormatDateTime, | ||
/// Use of the `list` formatter. | ||
FormatList, | ||
/// Use of the `number` formatter. | ||
FormatNums, | ||
} | ||
|
||
pub fn find_used_datakey(keys: &BuildersKeysInner, used_icu_keys: &mut HashSet<Options>) { | ||
for locale_value in keys.0.values() { | ||
match locale_value { | ||
LocaleValue::Subkeys { keys, .. } => find_used_datakey(keys, used_icu_keys), | ||
LocaleValue::Value(InterpolOrLit::Lit(_)) => {} // skip literals | ||
LocaleValue::Value(InterpolOrLit::Interpol(interpolation_keys)) => { | ||
for (_, var_infos) in interpolation_keys.iter_vars() { | ||
if matches!(var_infos.range_count, Some(RangeOrPlural::Plural)) { | ||
used_icu_keys.insert(Options::Plurals); | ||
} | ||
|
||
for formatter in &var_infos.formatters { | ||
let dk = match formatter { | ||
Formatter::None => continue, | ||
Formatter::Number => Options::FormatNums, | ||
Formatter::Date(_) | Formatter::Time(_) | Formatter::DateTime(_, _) => { | ||
Options::FormatDateTime | ||
} | ||
Formatter::List(_, _) => Options::FormatList, | ||
}; | ||
used_icu_keys.insert(dk); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn get_keys(used_icu_keys: impl IntoIterator<Item = Options>) -> impl Iterator<Item = DataKey> { | ||
used_icu_keys.into_iter().flat_map(Options::into_data_keys) | ||
} | ||
|
||
impl Options { | ||
/// Return a `Vec<DataKey>` needed to use the given option. | ||
pub fn into_data_keys(self) -> Vec<DataKey> { | ||
match self { | ||
Options::Plurals => icu_datagen::keys(&["plurals/cardinal@1", "plurals/ordinal@1"]), | ||
Options::FormatDateTime => icu_datagen::keys(&[ | ||
"datetime/timesymbols@1", | ||
"datetime/timelengths@1", | ||
"datetime/skeletons@1", | ||
"plurals/ordinal@1", | ||
"datetime/week_data@1", | ||
"decimal/symbols@1", | ||
"datetime/gregory/datelengths@1", | ||
"datetime/gregory/datesymbols@1", | ||
"datetime/buddhist/datelengths@1", | ||
"datetime/buddhist/datesymbols@1", | ||
"calendar/chinesecache@1", | ||
"datetime/chinese/datelengths@1", | ||
"datetime/chinese/datesymbols@1", | ||
"datetime/coptic/datelengths@1", | ||
"datetime/coptic/datesymbols@1", | ||
"calendar/dangicache@1", | ||
"datetime/dangi/datelengths@1", | ||
"datetime/dangi/datesymbols@1", | ||
"datetime/ethiopic/datelengths@1", | ||
"datetime/ethiopic/datesymbols@1", | ||
"datetime/hebrew/datelengths@1", | ||
"datetime/hebrew/datesymbols@1", | ||
"datetime/indian/datelengths@1", | ||
"datetime/indian/datesymbols@1", | ||
"datetime/islamic/datelengths@1", | ||
"datetime/islamic/datesymbols@1", | ||
"calendar/islamicobservationalcache@1", | ||
"calendar/islamicummalquracache@1", | ||
"datetime/japanese/datelengths@1", | ||
"datetime/japanese/datesymbols@1", | ||
"calendar/japanese@1", | ||
"datetime/japanext/datelengths@1", | ||
"datetime/japanext/datesymbols@1", | ||
"calendar/japanext@1", | ||
"datetime/persian/datelengths@1", | ||
"datetime/persian/datesymbols@1", | ||
"datetime/roc/datelengths@1", | ||
"datetime/roc/datesymbols@1", | ||
]), | ||
Options::FormatList => icu_datagen::keys(&["list/and@1", "list/or@1", "list/unit@1"]), | ||
Options::FormatNums => icu_datagen::keys(&["decimal/symbols@1"]), | ||
} | ||
} | ||
} |
Oops, something went wrong.