Inject ngettext into Tera templates

Fix .po updating (without the -U option, the result was written to stdout, not the .po file)
This commit is contained in:
Bat
2018-06-17 11:16:20 +01:00
parent 657a0837e2
commit 25ec318f3f
10 changed files with 61 additions and 3 deletions
+12 -2
View File
@@ -58,6 +58,16 @@ fn tera_gettext(msg: serde_json::Value, ctx: HashMap<String, serde_json::Value>)
Ok(serde_json::Value::String(Tera::one_off(trans.as_ref(), &ctx, false).unwrap_or(String::from(""))))
}
pub fn tera(t: &mut Tera) {
t.register_filter("_", tera_gettext)
fn tera_ngettext(msg: serde_json::Value, ctx: HashMap<String, serde_json::Value>) -> Result<serde_json::Value, TeraError> {
let trans = ngettext(
ctx.get("singular").unwrap().as_str().unwrap(),
msg.as_str().unwrap(),
ctx.get("count").unwrap().as_u64().unwrap() as u32
);
Ok(serde_json::Value::String(Tera::one_off(trans.as_ref(), &ctx, false).unwrap_or(String::from(""))))
}
pub fn tera(t: &mut Tera) {
t.register_filter("_", tera_gettext);
t.register_filter("_n", tera_ngettext);
}