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 27705383..a6d290ee 100644 Binary files a/translations/en/LC_MESSAGES/plume.mo and b/translations/en/LC_MESSAGES/plume.mo differ diff --git a/translations/fr/LC_MESSAGES/plume.mo b/translations/fr/LC_MESSAGES/plume.mo index ca4fa888..bdee9a78 100644 Binary files a/translations/fr/LC_MESSAGES/plume.mo and b/translations/fr/LC_MESSAGES/plume.mo differ