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" "Project-Id-Version: plume\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-06-15 16:33-0700\n" "POT-Creation-Date: 2018-06-15 16:33-0700\n"
"PO-Revision-Date: 2018-06-15 16:35+0100\n" "PO-Revision-Date: 2018-06-15 16:33-0700\n"
"Last-Translator: baptiste <baptiste@gelez.xyz>\n" "Last-Translator: Automatically generated\n"
"Language-Team: English\n" "Language-Team: none\n"
"Language: en_US\n" "Language: en\n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n"
msgid "Welcome on %s" msgid "Welcome on {{ instance_name }}"
msgstr "Welcome on %s" msgstr "Welcome on {{ instance_name }}"

View File

@ -12,5 +12,5 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n"
msgid "Welcome on %s" msgid "Welcome on {{ instance_name }}"
msgstr "" msgstr "Bienvenue sur {{ instance_name }}"

View File

@ -12,5 +12,5 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
msgid "Welcome on %s" msgid "Welcome on {{ instance_name }}"
msgstr "" msgstr ""

View File

@ -2,6 +2,7 @@ use gettextrs::*;
use rocket::{Data, Request, Rocket, fairing::{Fairing, Info, Kind}}; use rocket::{Data, Request, Rocket, fairing::{Fairing, Info, Kind}};
use serde_json; use serde_json;
use std::collections::HashMap; use std::collections::HashMap;
use std::env;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use tera::{Tera, Error as TeraError}; use tera::{Tera, Error as TeraError};
@ -29,7 +30,7 @@ impl Fairing for I18n {
} }
fn on_attach(&self, rocket: Rocket) -> Result<Rocket, Rocket> { 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); textdomain(self.domain);
Ok(rocket) Ok(rocket)
} }
@ -42,14 +43,21 @@ impl Fairing for I18n {
.split(",") .split(",")
.nth(0) .nth(0)
.unwrap_or("en"); .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> { 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) { 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 %} {% endblock title %}
{% block content %} {% block content %}
<h1>Welcome on {{ instance.name }}</h1> <h1>{{ _(t="Welcome on {{ instance_name }}", instance_name=instance.name) }}</h1>
<h2>Latest articles</h2> <h2>Latest articles</h2>
<div class="cards"> <div class="cards">

Binary file not shown.

Binary file not shown.