Expand description
accept-language
is a tiny library for parsing the Accept-Language header from browsers (as defined here).
It’s intended to be used in a web server that supports some level of internationalization (i18n), but can be used anytime an Accept-Language header string is available.
In order to help facilitate better i18n, a function is provided to return the intersection of
the languages the user prefers and the languages your application supports.
You can try the cargo test
and cargo bench
to verify the behaviour.
§Example
use accept_language::{intersection, parse};
let user_languages = parse("en-US, en-GB;q=0.5");
let common_languages = intersection("en-US, en-GB;q=0.5", &["en-US", "de", "en-GB"]);
Functions§
- intersection
- Compare an Accept-Language header value with your application’s supported languages to find the common languages that could be presented to a user.
- intersection_
ordered - Similar to
intersection
but using binary sort. The supported languages MUST be in alphabetical order, to find the common languages that could be presented to a user. Executes roughly 25% faster. - intersection_
ordered_ with_ quality - Similar to
intersection_with_quality
. The supported languages MUST be in alphabetical order, to find the common languages that could be presented to a user. Executes roughly 25% faster. - intersection_
with_ quality - Similar to
intersection
but with the quality asf32
appended for each language. This enables distinction between the default language of a user (value 1.0) and the best match. If you don’t want to assign your users immediatly to a non-default choice and you plan to add more languages later on in your webserver. - parse
- Parse a raw Accept-Language header value into an ordered list of language tags.
This should return the exact same list as
window.navigator.languages
in supported browsers. - parse_
with_ quality - Similar to
parse
but with qualityf32
appended to notice if it is a default value. is used byintersection_with_quality
andintersection_ordered_with_quality
.