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§
source§impl<'a> FromRequest<'a> for Locale
impl<'a> FromRequest<'a> for Locale
source§async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self>
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self>
Extract from request head and body.
Auto Trait Implementations§
impl Freeze for Locale
impl !RefUnwindSafe for Locale
impl Send for Locale
impl Sync for Locale
impl Unpin for Locale
impl !UnwindSafe for Locale
Blanket Implementations§
source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> TowerCompatExt for T
impl<T> TowerCompatExt for T
source§fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: Body + Send + Sync + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<BoxBody<Bytes, Error>>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: Body + Send + Sync + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<BoxBody<Bytes, Error>>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
Available on crate feature
tower-compat
only.Converts a tower service to a poem endpoint.