From 1092ceba7617bea9b5629635264c90986f77dd4e Mon Sep 17 00:00:00 2001 From: Bat Date: Sat, 16 Jun 2018 23:49:44 +0100 Subject: [PATCH] Fix locale switching + Make the gettext Tera function work --- po/en.po | 12 ++++++------ po/fr.po | 4 ++-- po/plume.pot | 2 +- src/i18n.rs | 16 ++++++++++++---- templates/instance/index.html.tera | 2 +- translations/en/LC_MESSAGES/plume.mo | Bin 459 -> 481 bytes translations/fr/LC_MESSAGES/plume.mo | Bin 394 -> 483 bytes 7 files changed, 22 insertions(+), 14 deletions(-) diff --git a/po/en.po b/po/en.po index 4ffaf0d8..01a565ec 100644 --- a/po/en.po +++ b/po/en.po @@ -3,14 +3,14 @@ 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:35+0100\n" -"Last-Translator: baptiste \n" -"Language-Team: English\n" -"Language: en_US\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 %s" -msgstr "Welcome on %s" +msgid "Welcome on {{ instance_name }}" +msgstr "Welcome on {{ instance_name }}" diff --git a/po/fr.po b/po/fr.po index dadf3043..2e3a7675 100644 --- a/po/fr.po +++ b/po/fr.po @@ -12,5 +12,5 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n" -msgid "Welcome on %s" -msgstr "" +msgid "Welcome on {{ instance_name }}" +msgstr "Bienvenue sur {{ instance_name }}" diff --git a/po/plume.pot b/po/plume.pot index e536d0df..8d2f16aa 100644 --- a/po/plume.pot +++ b/po/plume.pot @@ -12,5 +12,5 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" -msgid "Welcome on %s" +msgid "Welcome on {{ instance_name }}" msgstr "" diff --git a/src/i18n.rs b/src/i18n.rs index ca2f685c..2cf5f427 100644 --- a/src/i18n.rs +++ b/src/i18n.rs @@ -2,6 +2,7 @@ use gettextrs::*; use rocket::{Data, Request, Rocket, fairing::{Fairing, Info, Kind}}; use serde_json; use std::collections::HashMap; +use std::env; use std::fs; use std::path::PathBuf; use tera::{Tera, Error as TeraError}; @@ -29,7 +30,7 @@ impl Fairing for I18n { } fn on_attach(&self, rocket: Rocket) -> Result { - bindtextdomain(self.domain, fs::canonicalize(&PathBuf::from("./translations")).unwrap().to_str().unwrap()); + bindtextdomain(self.domain, fs::canonicalize(&PathBuf::from("./translations/")).unwrap().to_str().unwrap()); textdomain(self.domain); Ok(rocket) } @@ -42,14 +43,21 @@ impl Fairing for I18n { .split(",") .nth(0) .unwrap_or("en"); - setlocale(LocaleCategory::LcAll, format!("{}.UTF-8", lang.replace("-", "_"))); + + // We can't use setlocale(LocaleCategory::LcAll, lang), because it only accepts system-wide installed + // locales (and most of the time there are only a few of them). + // But, when we set the LANGUAGE environment variable, and an empty string as a second parameter to + // setlocale, gettext will be smart enough to find a matching locale in the locally installed ones. + env::set_var("LANGUAGE", lang); + setlocale(LocaleCategory::LcAll, ""); } } fn tera_gettext(ctx: HashMap) -> Result { - Ok(serde_json::Value::String(String::from(""))) + let trans = gettext(ctx.get("t").unwrap().as_str().unwrap()); + 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_global_function("gettext", Box::new(tera_gettext)) + t.register_global_function("_", Box::new(tera_gettext)) } diff --git a/templates/instance/index.html.tera b/templates/instance/index.html.tera index 480c758c..28cbe06a 100644 --- a/templates/instance/index.html.tera +++ b/templates/instance/index.html.tera @@ -6,7 +6,7 @@ {% endblock title %} {% block content %} -

Welcome on {{ instance.name }}

+

{{ _(t="Welcome on {{ instance_name }}", instance_name=instance.name) }}

Latest articles

diff --git a/translations/en/LC_MESSAGES/plume.mo b/translations/en/LC_MESSAGES/plume.mo index 27705383d0b74ed386957ce20f0a6a15851e3d46..a6d290ee1571194d025268269cbe00e96ac7dc0f 100644 GIT binary patch delta 191 zcmX@j{E&HqinkmC14AGPGcqs~0BI2a5ReT-aKHp(g{S5u=jWy>U>y`6L#X=!O&}<`w58mgE;%DL9svV~8y=2|J_<>#fs#jF%k^Cp`yP7=Xl7Xtt#T{g-9 delta 169 zcmaFJe42TJiWM&d14AGX$1*Z7!~?L<~$oi~_<_bCUCOQx)>_6jX~R=GVlS zY8x0D7;yO{7MJLT6eZ>r=OmWo7g;GJB^H!q7MG+d*uYp0>8UxXReBYbRdzt-dFiEz l>8ZLQsfoE(3a)wSIhn;7aA7Nj)V%o6;K|mElbBVD830H_EcpNc diff --git a/translations/fr/LC_MESSAGES/plume.mo b/translations/fr/LC_MESSAGES/plume.mo index ca4fa88801de6f2481185754ad892be0f40f26e3..bdee9a784b83ccabae5aa15850cd43efc4d0c692 100644 GIT binary patch delta 171 zcmeBTe#~5dPl#nI0}wC)u?!IF05K~N+W;{L1OTxd5C?+Tj0_9~Kw1%q4+7ah1P37f zU=W^~lboNMs*si{tbSO9SVP|^}egVeyl#{3{g0PZXW+W-In