Add I18n.lang #16

Merged
ghost merged 1 commit from plume/rocket_i18n:i18n-lang into master 2019-03-23 16:24:23 +00:00
3 changed files with 9 additions and 5 deletions

View file

@ -92,6 +92,8 @@ const ACCEPT_LANG: &'static str = "Accept-Language";
pub struct I18n {
/// 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)>;

View file

@ -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()),
}

View file

@ -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, ())),
}