pub struct Locale(/* private fields */);Expand description
The locale the client asked for, as negotiated from Accept-Language.
Recorded on the response’s top-level meta.lang so a response says which
locale it was produced for, and carried on the Context
so a handler that does have a catalog can consult it.
This is the client’s request, not a promise: nothing in this crate
translates, so lang describes what was asked for and the
Translation keys are what make honouring it possible.
Implementations§
Source§impl Locale
impl Locale
Sourcepub fn negotiate(header: &str) -> Option<Self>
pub fn negotiate(header: &str) -> Option<Self>
The best match from an Accept-Language header value.
Ranked by the q values the header carries, highest first, with a
missing q meaning q=1 as RFC 9110 §12.4.2 specifies. * is
skipped — it names no locale to record — and so is q=0, which is a
client saying it will not accept that language.
Returns None when the header is absent, empty, or names nothing
usable; the response then carries no lang rather than a guess.
use ash_jsonapi::i18n::Locale;
let best = Locale::negotiate("de-DE,de;q=0.9,en;q=0.8").unwrap();
assert_eq!(best.as_str(), "de-DE");
// `q` ordering wins over position:
let best = Locale::negotiate("en;q=0.5, fr;q=0.9").unwrap();
assert_eq!(best.as_str(), "fr");
assert!(Locale::negotiate("*").is_none());