#rocket-web #language #rocket #multi-language #web #web-apps

rocket_lang

rocket_lang provides a configurable enum type for multi-language rocket applications

6 releases

0.2.0 Jul 12, 2022
0.1.2 May 25, 2022
0.0.2 Mar 16, 2022

#21 in #multi-language

Apache-2.0

30KB
579 lines

Rocket Lang

Rocket-lang provides a configurable enum type for multi-language rocket applications.

LangCode

A request guard corresponding to the ISO 639-1 code standard. Usage example:

#[get("/some-path/")]
fn some_path(lang: LangCode) -> Template {
    // we can now choose which template to display
    // based of the user's language preference
    let path = format!("home/{}", LangCode); 
    Template::render(path, json!({}))
}

Config

The behavior of the enum can be configured with the Config structure, which can be attached to a rocket instance. When this is not used, the guard defaults to English.

accept_language

If the preferred method for language resolution is the http accept-language header, the qualities for each language can be set like this:

let config = Config::new(); 
let config[Es] = 1.0; 
let config[En] = 0.5;

url

The guard can also be configured to extract the language code from a fixed position in the path:

/// takes the language code from the last path segment:
let config = Config::new().url(-1); 

This way the language code can be retrieved from a positional url segment.

#[get("see-lang/<_>")]
fn see_lang(lang: LangCode) -> &'static str {
    lang.as_str()
}

custom

If none of the previous approaches suit your needs, you may also use a closure to create a language code from a request:

let config = Config::custom(|req: &Request|{
    let lang = from_url(req)?;
    Ok(lang) 
}); 

Composable

Other request guards can consume the structure in their API. Most notably, it can be used by foreign structures to return error messages in multiple languages.

use rocket_lang::Error; 

// here the error message displayed by
// `Unauthorized` will automatically suit the callers configuration.
#[get("/unauthorized")]
fn unauthorized() -> Unauthorized {
    Unauthorized
}

// A possible implementation of `Unauthorized`
impl<'r, 'o: 'r> Responder<'r, 'o> for Unauthorized {
    fn respond_to(self, request: &'r Request<'_>) -> rocket::response::Result<'o> {
        let lang: LangCode = request
            .try_into()
            .map_err(|x: Error| x.status())?;
        let msg = match lang {
            LangCode::Es => "No autorizado",
            LangCode::Fr => "Non autorisé",
            LangCode::Ge => "Nicht autorisiert",
            _            => "Unauthorized",  
        };
        msg.respond_to(request)
    }
}

Dependencies

~18–52MB
~840K SLoC