pub struct Locale { /* private fields */ }
This is supported 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},
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 req = Request::builder()
.header(header::ACCEPT_LANGUAGE, "en-US")
.finish();
let resp = app.get_response(req).await;
assert_eq!(
resp.into_body().into_string().await.unwrap(),
"hello world!"
);
let req = Request::builder()
.header(header::ACCEPT_LANGUAGE, "zh-CN")
.finish();
let resp = app.get_response(req).await;
assert_eq!(resp.into_body().into_string().await.unwrap(), "你好世界!");
Implementations
sourceimpl Locale
impl Locale
sourcepub fn text_with_args<'a>(
&self,
id: impl AsRef<str>,
args: impl Into<I18NArgs<'a>>
) -> Result<String, I18NError>
pub fn text_with_args<'a>(
&self,
id: impl AsRef<str>,
args: impl Into<I18NArgs<'a>>
) -> Result<String, I18NError>
Gets the text with arguments.
See also: I18NBundle::text_with_args
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 T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
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
sourceimpl<T> FutureExt for T
impl<T> FutureExt for T
sourcefn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
sourcefn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more