Use filters for gettext in Tera

This commit is contained in:
Bat 2018-06-17 10:38:59 +01:00
parent 1092ceba76
commit 657a0837e2
2 changed files with 4 additions and 4 deletions

View file

@ -53,11 +53,11 @@ impl Fairing for I18n {
} }
} }
fn tera_gettext(ctx: HashMap<String, serde_json::Value>) -> Result<serde_json::Value, TeraError> { fn tera_gettext(msg: serde_json::Value, ctx: HashMap<String, serde_json::Value>) -> Result<serde_json::Value, TeraError> {
let trans = gettext(ctx.get("t").unwrap().as_str().unwrap()); let trans = gettext(msg.as_str().unwrap());
Ok(serde_json::Value::String(Tera::one_off(trans.as_ref(), &ctx, false).unwrap_or(String::from("")))) Ok(serde_json::Value::String(Tera::one_off(trans.as_ref(), &ctx, false).unwrap_or(String::from(""))))
} }
pub fn tera(t: &mut Tera) { pub fn tera(t: &mut Tera) {
t.register_global_function("_", Box::new(tera_gettext)) t.register_filter("_", tera_gettext)
} }

View file

@ -6,7 +6,7 @@
{% endblock title %} {% endblock title %}
{% block content %} {% block content %}
<h1>{{ _(t="Welcome on {{ instance_name }}", instance_name=instance.name) }}</h1> <h1>{{ "Welcome on {{ instance_name }}" | _(instance_name=instance.name) }}</h1>
<h2>Latest articles</h2> <h2>Latest articles</h2>
<div class="cards"> <div class="cards">