1 | accept-language [![Build Status](https://travis-ci.org/tinganho/node-accept-language.png)](https://travis-ci.org/tinganho/node-accept-language)
|
2 | ========================
|
3 |
|
4 | [![NPM](https://nodei.co/npm/accept-language.png?downloads=true&stars=true)](https://nodei.co/npm/accept-language/)
|
5 |
|
6 | `accept-language` parses HTTP Accept-Language header and returns a matched defined language.
|
7 |
|
8 | ### Installation:
|
9 |
|
10 | ```
|
11 | npm install accept-language --save
|
12 | ```
|
13 |
|
14 | ### Usage:
|
15 |
|
16 | ```ts
|
17 | // var acceptLanguage = require('accept-language');
|
18 | import acceptLanguage from 'accept-language';
|
19 | acceptLanguage.languages(['en-US', 'zh-CN']);
|
20 | console.log(acceptLanguage.get('en-GB,en;q=0.8,sv'));
|
21 | /*
|
22 |
|
23 | 'en-US'
|
24 |
|
25 | */
|
26 | ```
|
27 |
|
28 | ### Usage with Express:
|
29 | If you are using Express server please use the middleware [express-request-language](https://www.npmjs.com/package/express-request-language).
|
30 |
|
31 | ### API
|
32 | #### acceptLanguage.languages(Array languageTags);
|
33 | Define your language tags ordered in highest priority comes first fashion. The language tags must comply with [BCP47][] standard.
|
34 |
|
35 | ```javascript
|
36 | acceptLanguage.languages(['en-US', 'zh-CN']);
|
37 | ```
|
38 |
|
39 | #### acceptLanguage.get(String acceptLanguageString);
|
40 | Get the most likely language given an `Accept-Language` string. In order for it to work you must set all your languages first.
|
41 | ```javascript
|
42 | acceptLanguage.get('en-GB,en;q=0.8,sv');
|
43 | ```
|
44 |
|
45 | ### Maintainer
|
46 |
|
47 | Tingan Ho [@tingan87][]
|
48 |
|
49 | ### License
|
50 | MIT
|
51 |
|
52 | [L10ns]: http://l10ns.org
|
53 | [BCP47]: https://tools.ietf.org/html/bcp47
|
54 | [@tingan87]: https://twitter.com/tingan87
|