valence/crates/valence_core/build/translation_key.rs
AviiNL d3ed3123ca
Temp fix for heck with unicode enabled (#326)
## Description

This should _temporarily_ fix the heck issue described in #324 until
https://github.com/withoutboats/heck/issues/42 is fixed downstream.

When it is fixed downstream, this commit should get reverted.

Fixes #324
2023-04-28 11:53:10 +00:00

43 lines
1.2 KiB
Rust

use anyhow::Ok;
use heck::ToShoutySnakeCase;
use proc_macro2::TokenStream;
use quote::quote;
use serde::Deserialize;
use valence_build_utils::ident;
#[derive(Deserialize, Clone, Debug)]
struct Translation {
key: String,
english_translation: String,
}
/// Escapes characters that have special meaning inside docs.
fn escape(text: &str) -> String {
text.replace('[', "\\[").replace(']', "\\]")
}
pub fn build() -> anyhow::Result<TokenStream> {
let translations = serde_json::from_str::<Vec<Translation>>(include_str!(
"../../../extracted/translation_keys.json"
))?;
let translation_key_consts = translations
.iter()
.map(|translation| {
let const_id = ident(translation.key.replace('.', "_").to_shouty_snake_case());
let key = &translation.key;
let english_translation = &translation.english_translation;
let doc = format!("\"{}\"", escape(english_translation));
quote! {
#[doc = #doc]
pub const #const_id: &str = #key;
}
})
.collect::<Vec<TokenStream>>();
Ok(quote! {
#(#translation_key_consts)*
})
}