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
Trait Implementations
sourceimpl<'a> FromRequest<'a> for Locale
impl<'a> FromRequest<'a> for Locale
sourcefn from_request<'life0, 'async_trait>(
req: &'a Request,
_body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where
'a: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'async_trait>(
req: &'a Request,
_body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where
'a: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Extract from request head and body.
Auto Trait Implementations
impl !RefUnwindSafe for Locale
impl Send for Locale
impl Sync for Locale
impl Unpin for Locale
impl !UnwindSafe for Locale
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more