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

View File

@ -17,6 +17,7 @@ fn update_po() {
println!("Updating {}", lang.clone()); println!("Updating {}", lang.clone());
// Update it // Update it
Command::new("msgmerge") Command::new("msgmerge")
.arg("-U")
.arg(po_path.to_str().unwrap()) .arg(po_path.to_str().unwrap())
.arg(pot_path.to_str().unwrap()) .arg(pot_path.to_str().unwrap())
.spawn() .spawn()

View File

@ -14,3 +14,8 @@ msgstr ""
msgid "Welcome on {{ instance_name }}" msgid "Welcome on {{ instance_name }}"
msgstr "Welcome on {{ instance_name }}" msgstr "Welcome on {{ instance_name }}"
msgid "One follower"
msgid_plural "{{ count }} followers"
msgstr[0] "One follower"
msgstr[1] "{{ count }} followers"

16
po/en.po~ Normal file
View File

@ -0,0 +1,16 @@
msgid ""
msgstr ""
"Project-Id-Version: plume\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-06-15 16:33-0700\n"
"PO-Revision-Date: 2018-06-15 16:33-0700\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"Language: en\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
msgid "Welcome on {{ instance_name }}"
msgstr "Welcome on {{ instance_name }}"

View File

@ -14,3 +14,8 @@ msgstr ""
msgid "Welcome on {{ instance_name }}" msgid "Welcome on {{ instance_name }}"
msgstr "Bienvenue sur {{ instance_name }}" msgstr "Bienvenue sur {{ instance_name }}"
msgid "One follower"
msgid_plural "{{ count }} followers"
msgstr[0] "{{ count }} abonné⋅e"
msgstr[1] "{{ count }} abonné⋅e⋅s"

16
po/fr.po~ Normal file
View File

@ -0,0 +1,16 @@
msgid ""
msgstr ""
"Project-Id-Version: plume\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-06-15 16:33-0700\n"
"PO-Revision-Date: 2018-06-15 16:33-0700\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
msgid "Welcome on {{ instance_name }}"
msgstr "Bienvenue sur {{ instance_name }}"

View File

@ -14,3 +14,8 @@ msgstr ""
msgid "Welcome on {{ instance_name }}" msgid "Welcome on {{ instance_name }}"
msgstr "" msgstr ""
msgid "One follower"
msgid_plural "{{ count }} followers"
msgstr[0] ""
msgstr[1] ""

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("")))) Ok(serde_json::Value::String(Tera::one_off(trans.as_ref(), &ctx, false).unwrap_or(String::from(""))))
} }
pub fn tera(t: &mut Tera) { fn tera_ngettext(msg: serde_json::Value, ctx: HashMap<String, serde_json::Value>) -> Result<serde_json::Value, TeraError> {
t.register_filter("_", tera_gettext) 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);
} }

View File

@ -42,7 +42,7 @@
</div> </div>
<div> <div>
<a href="followers/">{{ n_followers }} follower{{ n_followers | pluralize }}</a> <a href="followers/">{{ "{{ count }} followers" | _n(singular="One follower", count=n_followers) }}</a>
</div> </div>
<div> <div>

Binary file not shown.

Binary file not shown.