Rocket lang provides a configurable enum type for multi-language rocket applications.
A request guard corresponding to the ISO 639-1 code standard. Usage example: ```rust
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!({})) } ```
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, its default behavior is to retrieve the language code from the Accept-Language
header.
If the preferred method for language resolution is the http accept-language header, the qualities for each language can be set like this:
rust
let config = Config::new();
let config[Es] = 1.0;
let config[En] = 0.5;
The guard can also be configured to extract the language code from a fixed position in the path:
rust
/// 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. This also allows other request guards to consume the structure in their API. Most notably, it can be used by foreign structures to return error messages in multiple languages. ```rust
fn seelang(lang: LangCode) -> &'static str { lang.asstr() }
// here the error message displayed by
// Error
will automatically suit the callers configuration.
fn customerrormessage(user: User) -> Error { Error::Unauthorized }
// A possible implementation of Error
impl<'r, 'o: 'r> Responder<'r, 'o> for Error {
fn respondto(self, request: &'r Request<'>) -> rocket::response::Result<'o> {
let lang: LangCode = request
.tryinto()
.maperr(|x: Error| x.status())?;
let msg = match lang {
LangCode::En => "Unauthorized",
LangCode::Es => "No autorizado",
...
};
msg.respond_to(request)
}
}
```
If none of the previous approaches suit your needs, you may also use a closure to create a language code from a request:
rust
let config = Config::custom(|req: &Request|{
let lang = from_url(req)?;
Ok(lang)
});