pub struct Locale { /* private fields */ }
Available on crate feature i18n only.
Expand description

An extractor that parses the Accept-Language header and negotiates language bundles.

Example

use poem::{
    handler,
    http::header,
    i18n::{I18NResources, Locale},
    test::TestClient,
    Endpoint, EndpointExt, Request, Route,
};

let resources = I18NResources::builder()
    .add_ftl("en-US", "hello-world = hello world!")
    .add_ftl("zh-CN", "hello-world = 你好世界!")
    .build()
    .unwrap();

#[handler]
async fn index(locale: Locale) -> String {
    locale
        .text("hello-world")
        .unwrap_or_else(|_| "error".to_string())
}

let app = Route::new().at("/", index).data(resources);
let cli = TestClient::new(app);

let resp = cli
    .get("/")
    .header(header::ACCEPT_LANGUAGE, "en-US")
    .send()
    .await;
resp.assert_status_is_ok();
resp.assert_text("hello world!").await;

let resp = cli
    .get("/")
    .header(header::ACCEPT_LANGUAGE, "zh-CN")
    .send()
    .await;
resp.assert_status_is_ok();
resp.assert_text("你好世界!").await;

Implementations

Gets the text with arguments.

See also: I18NBundle::text_with_args

Gets the text.

See also: I18NBundle::text

Trait Implementations

Extract from request head and body.
Extract from request head. 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.

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Attaches the current Context to this type, returning a WithContext wrapper. Read more
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.

Should always be Self
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