From 25ec318f3f39aec064f656126172dbd7f7ab824f Mon Sep 17 00:00:00 2001 From: Bat Date: Sun, 17 Jun 2018 11:16:20 +0100 Subject: [PATCH] Inject ngettext into Tera templates Fix .po updating (without the -U option, the result was written to stdout, not the .po file) --- build.rs | 1 + po/en.po | 5 +++++ po/en.po~ | 16 ++++++++++++++++ po/fr.po | 5 +++++ po/fr.po~ | 16 ++++++++++++++++ po/plume.pot | 5 +++++ src/i18n.rs | 14 ++++++++++++-- templates/users/details.html.tera | 2 +- translations/en/LC_MESSAGES/plume.mo | Bin 481 -> 567 bytes translations/fr/LC_MESSAGES/plume.mo | Bin 483 -> 586 bytes 10 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 po/en.po~ create mode 100644 po/fr.po~ diff --git a/build.rs b/build.rs index f85f7cec..2e0afde9 100644 --- a/build.rs +++ b/build.rs @@ -17,6 +17,7 @@ fn update_po() { println!("Updating {}", lang.clone()); // Update it Command::new("msgmerge") + .arg("-U") .arg(po_path.to_str().unwrap()) .arg(pot_path.to_str().unwrap()) .spawn() diff --git a/po/en.po b/po/en.po index 01a565ec..bbe17a7c 100644 --- a/po/en.po +++ b/po/en.po @@ -14,3 +14,8 @@ msgstr "" msgid "Welcome on {{ instance_name }}" msgstr "Welcome on {{ instance_name }}" + +msgid "One follower" +msgid_plural "{{ count }} followers" +msgstr[0] "One follower" +msgstr[1] "{{ count }} followers" diff --git a/po/en.po~ b/po/en.po~ new file mode 100644 index 00000000..01a565ec --- /dev/null +++ b/po/en.po~ @@ -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 }}" diff --git a/po/fr.po b/po/fr.po index 2e3a7675..ad0a9294 100644 --- a/po/fr.po +++ b/po/fr.po @@ -14,3 +14,8 @@ msgstr "" msgid "Welcome on {{ 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" diff --git a/po/fr.po~ b/po/fr.po~ new file mode 100644 index 00000000..2e3a7675 --- /dev/null +++ b/po/fr.po~ @@ -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 }}" diff --git a/po/plume.pot b/po/plume.pot index 8d2f16aa..e654621f 100644 --- a/po/plume.pot +++ b/po/plume.pot @@ -14,3 +14,8 @@ msgstr "" msgid "Welcome on {{ instance_name }}" msgstr "" + +msgid "One follower" +msgid_plural "{{ count }} followers" +msgstr[0] "" +msgstr[1] "" diff --git a/src/i18n.rs b/src/i18n.rs index 96c699f4..caad5698 100644 --- a/src/i18n.rs +++ b/src/i18n.rs @@ -58,6 +58,16 @@ fn tera_gettext(msg: serde_json::Value, ctx: HashMap) 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) -> Result { + 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); } diff --git a/templates/users/details.html.tera b/templates/users/details.html.tera index a9184745..524754dd 100644 --- a/templates/users/details.html.tera +++ b/templates/users/details.html.tera @@ -42,7 +42,7 @@
- {{ n_followers }} follower{{ n_followers | pluralize }} + {{ "{{ count }} followers" | _n(singular="One follower", count=n_followers) }}
diff --git a/translations/en/LC_MESSAGES/plume.mo b/translations/en/LC_MESSAGES/plume.mo index a6d290ee1571194d025268269cbe00e96ac7dc0f..cfc0f640a794367d1d99f94834ac6f63069f810a 100644 GIT binary patch delta 184 zcmaFJyq%@~o)F7a1|VPtVi_Pd0b*7l_5orLNC09bAWj5gIUsHU;y^|QhQ&Y{B>ol1 z28l~BF)#p`U;r`;$b|s^yi|p>{G6Qp^3)=R>S~4L{L;J`!ju94#M36E delta 97 zcmdna@{l>;o)F7a1|VPpVi_RT0b*7lwgF-g2moR^APxj!Mh1ogDE|K_B z*@{3~gNcCw$OHqBSwJoX_~)f6q~+)2%wF;rJ8Bj`2eFb08XthZvX%Q delta 93 zcmX@b@|Zc`o)F7a1|VPpVi_RT0b*7lwgF-g2moR^APxkv85tM~fV3hI9|W?22o6B{ OL11HZA>(8XCM5vXv