From 006714bcccc7919b2da8acba7a36d7ce6ddb9208 Mon Sep 17 00:00:00 2001 From: Llaurence Date: Sat, 23 Mar 2019 16:21:34 +0100 Subject: [PATCH] Add I18n.lang Used to get the language for the current request. It can be used for database requests or to adapt the UI. Signed-off-by: Llaurence --- src/lib.rs | 4 +++- src/with_actix.rs | 5 +++-- src/with_rocket.rs | 5 +++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d4f43fe..4638d81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,8 +90,10 @@ const ACCEPT_LANG: &'static str = "Accept-Language"; /// A request guard to get the right translation catalog for the current request. pub struct I18n { - /// The catalog containing the translated messages, in the correct locale for this request. + /// The catalog containing the translated messages, in the correct locale for this request. pub catalog: Catalog, + /// The language of the current request. + pub lang: &'static str, } pub type Translations = Vec<(&'static str, Catalog)>; diff --git a/src/with_actix.rs b/src/with_actix.rs index 77aac5d..962e68a 100644 --- a/src/with_actix.rs +++ b/src/with_actix.rs @@ -55,8 +55,9 @@ where .unwrap_or("en"); match langs.iter().find(|l| l.0 == lang) { - Some(catalog) => Ok(I18n { - catalog: catalog.1.clone(), + Some(translation) => Ok(I18n { + catalog: translation.1.clone(), + lang: translation.0, }), None => Err(MissingTranslationsError(lang.to_owned()).into()), } diff --git a/src/with_rocket.rs b/src/with_rocket.rs index 5630e40..ed14c38 100644 --- a/src/with_rocket.rs +++ b/src/with_rocket.rs @@ -28,8 +28,9 @@ impl<'a, 'r> FromRequest<'a, 'r> for I18n { .unwrap_or("en"); match langs.iter().find(|l| l.0 == lang) { - Some(catalog) => Outcome::Success(I18n { - catalog: catalog.1.clone(), + Some(translation) => Outcome::Success(I18n { + catalog: translation.1.clone(), + lang: translation.0, }), None => Outcome::Failure((Status::InternalServerError, ())), } -- 2.30.2