accept-language-parser
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/accept-language-parser package

1.5.0 • Public • Published

accept-language-parser

Build Status NPM version Dependencies

Parses the accept-language header from an HTTP request and produces an array of language objects sorted by quality.

Installation:

npm install accept-language-parser

API

parser.parse(acceptLanguageHeader)

var parser = require('accept-language-parser');

var languages = parser.parse('en-GB,en;q=0.8');

console.log(languages);

Output will be:

[
  {
    code: "en",
    region: "GB",
    quality: 1.0
  },
  {
    code: "en",
    region: undefined,
    quality: 0.8
  }
];

Output is always sorted in quality order from highest -> lowest. as per the http spec, omitting the quality value implies 1.0.

parser.pick(supportedLangugagesArray, acceptLanguageHeader, options = {})

Alias: parser.pick(supportedLanguagesArray, parsedAcceptLanguageHeader)

var parser = require('accept-language-parser');

var language = parser.pick(['fr-CA', 'fr-FR', 'fr'], 'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8');

console.log(language);

Output will be:

"fr-CA"

The options currently supports only loose option that allows partial matching on supported languages. For example:

parser.pick(['fr', 'en'], 'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8');

Would return:

"fr"

In loose mode the order of supportedLanguagesArray matters, as it is the first partially matching language that is returned. It means that if you want to pick more specific langauge first, you should list it first as well, for example: ['fr-CA', 'fr'].

Running test

npm install
npm test

License

MIT

Package Sidebar

Install

npm i accept-language-parser

Weekly Downloads

389,386

Version

1.5.0

License

MIT

Unpacked Size

13.9 kB

Total Files

8

Last publish

Collaborators

  • acolchado
  • arnoldzokas
  • matteofigus
  • ryantomlinson
  • andyroyle
  • pbazydlo