pub struct Config {
    pub wildcard: Option<LangCode>,
    /* private fields */
}
Expand description

This struct allows for customization of LangCode’s behavior. The precedence for every configuration is:

  1. custom closure
  2. from url
  3. accept language header
  4. wildcard

If none of these are able to produce an Ok value, an error is returned. Note that returning errors is discouraged, as it may lead to a poor user experience.

Custom closure

This configuration has the biggest precedence. There is full access to the incoming request.

let config = Config::new().custom(|request: &Request| {
    Ok(lang_from_request(request))
});

Url

The url method can be used to specify which segment should be interpreted as a language code. Negative indexes can be used to refer to positions relative to the last segment. Thus -1 corresponds to the last segment, -2 the second to last, and so on.

examples

// we use -1 to specify that the last segment is our language code
let config = Config::new().url(-1);
// we have to specify which language we want on the handler
#[get("/index/en")
fn english_language() {
   /* ... */
}
// we can handle all languages at once with a wildcard segment.
// if we visit with an invalid language code ("/index/not-a-lang-code/"), an error is returned.
#[get("/index/<_>", rank = 2)
fn any_language() {
   /* ... */
}

Accept Language

The accept language header qualities can be set by indexing into the config struct. By default, all values are set to 0.0. These values should correspond to a number between 0.0 and 1.0 specifying the quality of that language support of your site.

let config = Config::new();
config[En] = 0.3;
config[Ar] = 1.0;

Wildcard

The wildcard will be used to create a value if none of the previous attempts succeeded. Note that wildcards are useful for single language applications, but they may not scale as well as url resolution. By default the wildcard is set to None.

let config = Config::new().url(1).wildcard(Es);

Fields

wildcard: Option<LangCode>

Implementations

The wildcard is used as a last resort if all other options failed.

Takes the Config structure by value and returns a new one with the url configuration set. The position parameter determines which path segment will be interpreted as a language code. Negative positions will be interpreted as being relative to the last path segment.

Constructs a new configuration object.

Used to specify a custom language resolution method.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Returns the “default value” for a type. Read more

Returns an Info structure containing the name and Kind of this fairing. The name can be any arbitrary string. Kind must be an ord set of Kind variants. Read more

The ignite callback. Returns Ok if ignition should proceed and Err if ignition and launch should be aborted. Read more

The request callback. Read more

The liftoff callback. Read more

The response callback. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts self into a collection.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more