Crate accept_language

source ·
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

  • Compare an Accept-Language header value with your application’s supported languages to find the common languages that could be presented to a user.
  • 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.
  • 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.
  • Similar to intersection but with the quality as f32 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 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.
  • Similar to parse but with quality f32 appended to notice if it is a default value. is used by intersection_with_quality and intersection_ordered_with_quality.