I18n fairing
This commit is contained in:
parent
c9b4c40fa1
commit
327768d3fe
42
src/i18n.rs
Normal file
42
src/i18n.rs
Normal file
@ -0,0 +1,42 @@
|
||||
use gettextrs::*;
|
||||
use rocket::{Data, Request, Rocket, fairing::{Fairing, Info, Kind}};
|
||||
|
||||
const ACCEPT_LANG: &'static str = "Accept-Language";
|
||||
|
||||
pub struct I18n {
|
||||
domain: &'static str
|
||||
}
|
||||
|
||||
impl I18n {
|
||||
pub fn new(domain: &'static str) -> I18n {
|
||||
I18n {
|
||||
domain: domain
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Fairing for I18n {
|
||||
fn info(&self) -> Info {
|
||||
Info {
|
||||
name: "Gettext I18n",
|
||||
kind: Kind::Attach | Kind::Request
|
||||
}
|
||||
}
|
||||
|
||||
fn on_attach(&self, rocket: Rocket) -> Result<Rocket, Rocket> {
|
||||
bindtextdomain(self.domain, "/usr/local/share/locale");
|
||||
textdomain(self.domain);
|
||||
Ok(rocket)
|
||||
}
|
||||
|
||||
fn on_request(&self, request: &mut Request, _: &Data) {
|
||||
let lang = request
|
||||
.headers()
|
||||
.get_one(ACCEPT_LANG)
|
||||
.unwrap_or("en")
|
||||
.split(",")
|
||||
.nth(0)
|
||||
.unwrap_or("en");
|
||||
setlocale(LocaleCategory::LcAll, format!("{}.UTF-8", lang.replace("-", "_")));
|
||||
}
|
||||
}
|
@ -39,6 +39,7 @@ use std::env;
|
||||
|
||||
mod activity_pub;
|
||||
mod db_conn;
|
||||
mod i18n;
|
||||
mod models;
|
||||
mod schema;
|
||||
mod routes;
|
||||
@ -128,5 +129,6 @@ fn main() {
|
||||
])
|
||||
.manage(init_pool())
|
||||
.attach(Template::fairing())
|
||||
.attach(i18n::I18n::new("plume"))
|
||||
.launch();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user