Browse Source

Upgrade actix, gettext

master
asonix 4 years ago
committed by Ana Gelez
parent
commit
ee253eca27
  1. 7
      Cargo.toml
  2. 2
      rust-toolchain
  3. 42
      src/lib.rs
  4. 34
      src/with_actix.rs

7
Cargo.toml

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

2
rust-toolchain

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

42
src/lib.rs

@ -1,6 +1,6 @@
//! # 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`.
//!
@ -14,33 +14,20 @@
//! 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
//! # use rocket;
//! use gettext_macros::{compile_i18n, include_i18n, init_i18n};
//!
//! 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!();
//! App::new()
//! .data(rocket_i18n::i18n("your-domain", vec!["en", "pl"]))
//! .service(...)
//! ```
//!
//!
//! 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.
//!
//! ```rust,ignore
//! use gettext_macros::i18n;
//! use rocket_i18n::I18n;
//!
//! #[get("/")]
//! fn route(i18n: I18n) -> &str {
//! fn route(i18n: I18n) -> String {
//! i18n!(i18n.catalog, "Hello, world!")
//! }
//! ```
@ -68,23 +55,14 @@
//! store your catalog.
#[cfg(feature = "actix-web")]
extern crate actix_web;
extern crate gettext;
#[cfg(feature = "rocket")]
extern crate rocket;
pub use gettext::*;
use std::fs;
#[cfg(feature = "rocket")]
mod with_rocket;
#[cfg(feature = "actix-web")]
mod with_actix;
#[cfg(feature = "actix-web")]
pub use with_actix::Internationalized;
#[cfg(feature = "rocket")]
mod with_rocket;
const ACCEPT_LANG: &'static str = "Accept-Language";
@ -98,7 +76,7 @@ pub struct I18n {
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,
/// use `gettext_macros::include_i18n`.

34
src/with_actix.rs

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

Loading…
Cancel
Save