Upgrade actix, gettext

This commit is contained in:
asonix 2019-05-11 12:58:04 -05:00 committed by Ana Gelez
parent ada273e4a9
commit ee253eca27
4 changed files with 39 additions and 48 deletions

View file

@ -8,17 +8,18 @@ repository = "https://github.com/Plume-org/rocket_i18n"
keywords = ["i18n", "rocket", "gettext", "internationalization", "localization"] keywords = ["i18n", "rocket", "gettext", "internationalization", "localization"]
categories = ["internationalization", "localization", "web-programming"] categories = ["internationalization", "localization", "web-programming"]
version = "0.4.0" version = "0.4.0"
edition = "2018"
[dependencies] [dependencies]
gettext = "0.3" gettext = "0.4"
[dependencies.rocket] [dependencies.rocket]
version = "0.4.0" version = "0.4.0"
optional = true optional = true
[dependencies.actix-web] [dependencies.actix-web]
version = "0.7" version = "1.0"
optional = true optional = true
[features] [features]
default = ["rocket"] default = ["actix-web"]

View file

@ -1 +1 @@
nightly-2018-12-06 nightly-2019-08-09

View file

@ -1,6 +1,6 @@
//! # Rocket I18N //! # Rocket I18N
//! //!
//! A crate to help you internationalize your Rocket or Actix Web applications. //! A crate to help you internationalize your Actix or Actix Web applications.
//! //!
//! It just selects the correct locale for each request, and return the corresponding `gettext::Catalog`. //! It just selects the correct locale for each request, and return the corresponding `gettext::Catalog`.
//! //!
@ -14,33 +14,20 @@
//! gettext-macros = "0.1" # Provides proc-macros to manage translations //! gettext-macros = "0.1" # Provides proc-macros to manage translations
//! ``` //! ```
//! //!
//! Then, in your `main.rs`: //! Then, in your `main.rs`, add the translations to you application's data:
//! //!
//! ```rust,ignore //! ```rust,ignore
//! # use rocket; //! App::new()
//! use gettext_macros::{compile_i18n, include_i18n, init_i18n}; //! .data(rocket_i18n::i18n("your-domain", vec!["en", "pl"]))
//! //! .service(...)
//! init_i18n!("my_web_app", en, eo, it, pl);
//!
//! fn main() {
//! rocket::ignite()
//! // Make Rocket manage your translations.
//! .manage(include_i18n!());
//! // Register routes, etc
//! }
//!
//! compile_i18n!();
//! ``` //! ```
//! //!
//!
//! Then in all your requests you'll be able to use the `i18n` macro to translate anything. //! Then in all your requests you'll be able to use the `i18n` macro to translate anything.
//! It takes a `gettext::Catalog` and a string to translate as argument. //! It takes a `gettext::Catalog` and a string to translate as argument.
//! //!
//! ```rust,ignore //! ```rust,ignore
//! use gettext_macros::i18n; //! fn route(i18n: I18n) -> String {
//! use rocket_i18n::I18n;
//!
//! #[get("/")]
//! fn route(i18n: I18n) -> &str {
//! i18n!(i18n.catalog, "Hello, world!") //! i18n!(i18n.catalog, "Hello, world!")
//! } //! }
//! ``` //! ```
@ -68,23 +55,14 @@
//! store your catalog. //! store your catalog.
#[cfg(feature = "actix-web")]
extern crate actix_web;
extern crate gettext;
#[cfg(feature = "rocket")]
extern crate rocket;
pub use gettext::*; pub use gettext::*;
use std::fs; use std::fs;
#[cfg(feature = "rocket")]
mod with_rocket;
#[cfg(feature = "actix-web")] #[cfg(feature = "actix-web")]
mod with_actix; mod with_actix;
#[cfg(feature = "actix-web")] #[cfg(feature = "rocket")]
pub use with_actix::Internationalized; mod with_rocket;
const ACCEPT_LANG: &'static str = "Accept-Language"; const ACCEPT_LANG: &'static str = "Accept-Language";
@ -98,7 +76,7 @@ pub struct I18n {
pub type Translations = Vec<(&'static str, Catalog)>; pub type Translations = Vec<(&'static str, Catalog)>;
/// Loads translations at runtime. Usually used with `rocket::Rocket::manage`. /// Loads translations at runtime. Usually used with `actix_web::web::App::data`.
/// ///
/// Note that the `.mo` files should be present with your binary. If you want to embed them, /// Note that the `.mo` files should be present with your binary. If you want to embed them,
/// use `gettext_macros::include_i18n`. /// use `gettext_macros::include_i18n`.

View file

@ -2,7 +2,7 @@ use std::{error::Error, fmt};
use crate::{I18n, Translations, ACCEPT_LANG}; use crate::{I18n, Translations, ACCEPT_LANG};
use actix_web::{FromRequest, HttpRequest, ResponseError}; use actix_web::{dev::Payload, FromRequest, HttpRequest, ResponseError};
#[derive(Debug)] #[derive(Debug)]
pub struct MissingTranslationsError(String); pub struct MissingTranslationsError(String);
@ -23,20 +23,32 @@ impl ResponseError for MissingTranslationsError {
// this defaults to an empty InternalServerError response // this defaults to an empty InternalServerError response
} }
pub trait Internationalized { #[derive(Debug)]
fn get(&self) -> Translations; pub struct MissingStateError;
impl fmt::Display for MissingStateError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Could not retrieve state")
}
} }
impl<S> FromRequest<S> for I18n impl Error for MissingStateError {
where fn description(&self) -> &str {
S: Internationalized, "Could not retrieve state"
{ }
type Config = (); }
type Result = Result<Self, actix_web::Error>;
fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result { impl ResponseError for MissingStateError {
let state = req.state(); // this defaults to an empty InternalServerError response
let langs = state.get(); }
impl FromRequest for I18n {
type Config = ();
type Error = actix_web::Error;
type Future = Result<Self, Self::Error>;
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
let langs = req.app_data::<Translations>().ok_or(MissingStateError)?;
let lang = req let lang = req
.headers() .headers()