-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
51 lines (43 loc) · 1.53 KB
/
Copy pathbuild.rs
File metadata and controls
51 lines (43 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use leptos_i18n_build::{Config, TranslationsInfos};
use std::{error::Error, fs, path::PathBuf};
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=Cargo.toml");
println!("cargo::rerun-if-changed=locales");
let out = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()).join("i18n");
// 根据 feature 选择 locale 源目录,复制到 locales/ 供 leptos_i18n 读取
let src_dir = if std::env::var("CARGO_FEATURE_OTH").is_ok() {
"locales/oth"
} else {
"locales/cn"
};
for entry in fs::read_dir(src_dir)? {
let entry = entry?;
let dest = PathBuf::from("locales").join(entry.file_name());
fs::copy(entry.path(), &dest)?;
}
let locales: Vec<String> = fs::read_dir("locales")?
.filter_map(|e| e.ok())
.map(|e| e.path())
.filter(|p| p.extension().map_or(false, |ext| ext == "json"))
.filter_map(|p| p.file_stem()?.to_str().map(String::from))
.collect();
let default = "zh".to_string();
let default = if locales.contains(&default) {
default
} else {
locales[0].clone()
};
let mut cfg = Config::new(&default)?;
cfg.options.interpolate_display = true;
for loc in &locales {
if loc != &default {
cfg = cfg.add_locale(loc)?;
}
}
let infos = TranslationsInfos::parse(cfg)?;
infos.emit_diagnostics();
infos.rerun_if_locales_changed();
infos.generate_i18n_module(out)?;
Ok(())
}