Pumpkin/pumpkin-data/build/translations.rs
Alexander Medvedev e45ab613f4 chore: parse translations at compile time
not finished but its much safer and nicer now
2026-02-08 13:22:36 +01:00

35 lines
915 B
Rust

use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use std::{collections::BTreeMap, fs};
pub(crate) fn build() -> TokenStream {
println!("cargo:rerun-if-changed=../assets/en_us.json");
let en_us: BTreeMap<String, String> = serde_json::from_str(
&fs::read_to_string("../assets/en_us.json").expect("en_us is missing"),
)
.unwrap();
let mut constants = TokenStream::new();
for (name, value) in &en_us {
let clean_name = name.to_uppercase().replace(['.', '-'], "_");
let ident = format_ident!("{}", clean_name);
if !value.is_empty() {
constants.extend(quote! {
#[doc = #value]
pub const #ident: &str = #name;
});
} else {
constants.extend(quote! {
pub const #ident: &str = #name;
});
}
}
quote! {
#constants
}
}