# accept-language-parser

> Parse the accept-language header from a HTTP request

Latest version **1.5.0** (published 2018-03-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install accept-language-parser
pnpm add accept-language-parser
yarn add accept-language-parser
bun add accept-language-parser
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.0 |
| Published | 2018-03-20 |
| First published | 2014-03-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/accept-language-parser) |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 165 |
| Author | Andy Royle |
| Maintainers | andyroyle, matteofigus, pbazydlo, ryantomlinson |
| Keywords | accept-language, i18n, parser |

## Links

- npm: https://www.npmjs.com/package/accept-language-parser
- Repository: https://github.com/opentable/accept-language-parser
- Issues: https://github.com/opentable/accept-language-parser/issues
- npm.io page: https://npm.io/package/accept-language-parser

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.5.0 (latest) — 2018-03-20
- 1.4.1 — 2017-07-31
- 1.4.0 — 2017-03-27
- 1.3.0 — 2016-11-11
- 1.2.0 — 2016-08-09
- 1.1.2 — 2016-06-08
- 1.1.1 — 2016-05-23
- 1.1.0 — 2016-05-16
- 1.0.2 — 2014-05-13
- 1.0.1 — 2014-05-03
- 1.0.0 — 2014-03-31

## README

accept-language-parser
======================

[![Build Status](https://travis-ci.org/opentable/accept-language-parser.png?branch=master)](https://travis-ci.org/opentable/accept-language-parser) [![NPM version](https://badge.fury.io/js/accept-language-parser.png)](http://badge.fury.io/js/accept-language-parser) ![Dependencies](https://david-dm.org/opentable/accept-language-parser.png)

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

---
_Source: https://npm.io/package/accept-language-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
