valence/valence_protocol/build/translation_key.rs
Sandro Marques 6437381339
Translation key extractor and code generator (#160)
Generates a new `translation_key.rs` with all bundled translations.
Closes #158.
2022-11-27 05:12:08 -08:00

44 lines
1.1 KiB
Rust

use anyhow::Ok;
use heck::ToShoutySnakeCase;
use proc_macro2::TokenStream;
use quote::quote;
use serde::Deserialize;
use crate::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.to_shouty_snake_case());
let key = &translation.key;
let english_translation = &translation.english_translation;
let doc = escape(english_translation);
quote! {
#[doc = #doc]
pub const #const_id: &str = #key;
}
})
.collect::<Vec<TokenStream>>();
Ok(quote! {
#(#translation_key_consts)*
})
}