Create a codegen macro that generates ftl files from a struct
This commit is contained in:
parent
35c034b04b
commit
f8c19b2d18
|
@ -2901,6 +2901,10 @@ dependencies = [
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "messages-codegen"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mime"
|
name = "mime"
|
||||||
version = "0.2.6"
|
version = "0.2.6"
|
||||||
|
|
|
@ -22,6 +22,7 @@ members = [
|
||||||
"kifu/gtk",
|
"kifu/gtk",
|
||||||
"l10n",
|
"l10n",
|
||||||
"memorycache",
|
"memorycache",
|
||||||
|
"messages-codegen",
|
||||||
"nom-training",
|
"nom-training",
|
||||||
"result-extended",
|
"result-extended",
|
||||||
"screenplay",
|
"screenplay",
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "messages-codegen"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,53 @@
|
||||||
|
/* Given that fluent doesn't provide an ability to see all of the placeholders within a message,
|
||||||
|
* and that the placeholders may change depending on the values of others, I don't have a way to
|
||||||
|
* ensure that the translation code remains entirely connected to the source messages themselves.
|
||||||
|
*
|
||||||
|
* Given that I can't really enforce it, I can, perhaps, codegen it in a way that at least the two
|
||||||
|
* are right next to one another. A macro that generates the data structure and can write the
|
||||||
|
* source strings file.
|
||||||
|
*
|
||||||
|
* For overall file structure, I want the messages, L10N, and FluentErgo tied together.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! messages {
|
||||||
|
($($name:ident($($arg:ident: $argtype:ty)*) => $message:literal,)+) => {
|
||||||
|
pub enum Messages {
|
||||||
|
$($name($name)),+
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Messages {
|
||||||
|
fn gen_strings() -> Vec<String> {
|
||||||
|
vec![
|
||||||
|
$(concat!(stringify!($name), " => ", $message).to_string(),)+
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$(pub struct $name {
|
||||||
|
$($arg: $argtype)*
|
||||||
|
})+
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
messages! {
|
||||||
|
Welcome(name: String) => "Hello, ${name}",
|
||||||
|
GamesInDatabase(count: usize) => "{$count ->
|
||||||
|
[one] There is one game in the database
|
||||||
|
*[other] There are ${count} games in the database
|
||||||
|
}",
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_can_generate_an_ftl() {
|
||||||
|
for s in Messages::gen_strings() {
|
||||||
|
println!("{}", s);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert!(false);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue