Fix locale switching + Make the gettext Tera function work

This commit is contained in:
Bat 2018-06-16 23:49:44 +01:00
parent b18aa33c70
commit 1092ceba76
7 changed files with 22 additions and 14 deletions

View file

@ -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 <baptiste@gelez.xyz>\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 }}"

View file

@ -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 }}"

View file

@ -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 ""

View file

@ -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<Rocket, Rocket> {
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<String, serde_json::Value>) -> Result<serde_json::Value, TeraError> {
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))
}

View file

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

Binary file not shown.

Binary file not shown.