1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
# bcp-47
[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
Parse and stringify [BCP 47][spec] language tags.
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`parse(tag[, options])`](#parsetag-options)
* [`stringify(schema)`](#stringifyschema)
* [`Schema`](#schema)
* [`function warning(reason, code, offset)`](#function-warningreason-code-offset)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)
## What is this?
This is a package that can parse BCP 47 language tags to an object representing
them, and serialize those objects back into language tags.
It supports a forgiving mode to handle incorrect BCP 47 tags and can emit
warnings about problems in incorrect tags.
## When should I use this?
You can use this package if you need to access the data stored in BCP 47
language tags.
You can also use this package if you want to check (lint) or manipulate tags.
## Install
This package is [ESM only][esm].
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
```sh
npm install bcp-47
```
In Deno with [Skypack][]:
```js
import * as bcp47 from 'https://cdn.skypack.dev/bcp-47@2?dts'
```
In browsers with [Skypack][]:
```html
<script type="module">
import * as bcp47 from 'https://cdn.skypack.dev/bcp-47@2?min'
</script>
```
## Use
```js
import {parse, stringify} from 'bcp-47'
const schema = parse('hy-Latn-IT-arevela')
console.log(schema)
console.log(stringify(schema))
```
Yields:
```js
{ language: 'hy',
extendedLanguageSubtags: [],
script: 'Latn',
region: 'IT',
variants: ['arevela'],
extensions: [],
privateuse: [],
irregular: null,
regular: null }
'hy-Latn-IT-arevela'
```
## API
This package exports the following identifiers: `parse` and `stringify`.
There is no default export.
### `parse(tag[, options])`
Parse a BCP 47 tag into a language schema.
Note that the algorithm is case insensitive.
###### `options.normalize`
Whether to normalize legacy tags when possible (`boolean`, default:
`true`).
For example, `i-klingon` does not match the BCP 47 language algorithm but is
considered valid by BCP 47 nonetheless.
It is suggested to use `tlh` instead (the ISO 639-3 code for Klingon).
When `normalize` is `true`, passing `i-klingon` or other deprecated tags, is
handled as if their suggested valid tag was given instead.
###### `options.forgiving`
By default, when an error is encountered, an empty object is returned.
When in forgiving mode, all found values up to the point of the error
are included (`boolean`, default: `false`).
So, for example, where by default `en-GB-abcdefghi` an empty object is returned
(as the language variant is too long), in `forgiving` mode the `language` of
`schema` is populated with `en` and the `region` is populated with `GB`.
###### `options.warning`
When given, `warning` is called when an error is encountered
([`Function`][warning]).
###### Returns
Parsed BCP 47 language tag ([`Schema`][schema]).
###### Throws
When `tag` is `null` or `undefined`.
### `stringify(schema)`
Compile a [`schema`][schema] to a BCP 47 language tag.
###### Returns
BCP 47 language tag (`string`).
### `Schema`
A schema represents a language tag.
A schema is deemed empty when it has neither `language`, `irregular`, `regular`,
nor `privateuse` (where an empty `privateuse` array is handled as no
`privateuse` as well).
###### `schema.language`
Two or three character [ISO 639][iso-639] language code, four character reserved
language code, or 5 to 8 (inclusive) characters registered language subtag
(`string`).
For example, `en` (English) or `cmn` (Mandarin Chinese).
###### `schema.extendedLanguageSubtags`
Selected three-character [ISO 639][iso-639] codes(`Array<string>`), such as
`yue` in `zh-yue-HK` (Chinese, Cantonese, as used in Hong Kong SAR).
###### `schema.script`
Four character [ISO 15924][iso-15924] script code (`string`), such as `Latn` in
`hy-Latn-IT-arevela` (Eastern Armenian written in Latin script, as used in
Italy).
###### `schema.region`
Two alphabetical character [ISO 3166-1][iso-3166-1] code or three digit
[UN M49][un-m49] code (`string`).
For example, `CN` in `cmn-Hans-CN` (Mandarin Chinese, Simplified script, as used
in China) or `419` in `es-419` (Spanish as used in Latin America and the
Caribbean).
###### `schema.variants`
5 to 8 (inclusive) character language variants (`Array<string>`), such as
`rozaj` and `biske` in `sl-rozaj-biske` (San Giorgio dialect of Resian dialect
of Slovenian).
###### `schema.extensions`
List of extensions (`Array<Object>`), each an object containing a one character
`singleton`, and a list of `extensions` (`string`).
`singleton` cannot be `x` (case insensitive) and `extensions` must be between
two and eight (inclusive) characters.
For example, an extension would be `u-co-phonebk` in `de-DE-u-co-phonebk`
(German, as used in Germany, using German phonebook sort order), where `u` is
the `singleton` and `co` and `phonebk` are its extensions.
###### `schema.privateuse`
List of private-use subtags (`Array<string>`), where each subtag must be between
one and eight (inclusive) characters.
###### `schema.regular`
One of the `regular` tags (`string`): tags that are seen as something different
by the algorithm.
Valid values are:
* `art-lojban`
* `cel-gaulish`
* `no-bok`
* `no-nyn`
* `zh-guoyu`
* `zh-hakka`
* `zh-min`
* `zh-min-nan`
* `zh-xiang`
###### `schema.irregular`
One of the `irregular` tags (`string`): tags that are seen as invalid by the
algorithm).
Valid values are:
* `en-GB-oed`
* `i-ami`
* `i-bnn`
* `i-default`
* `i-enochian`
* `i-hak`
* `i-klingon`
* `i-lux`
* `i-mingo`
* `i-navajo`
* `i-pwn`
* `i-tao`
* `i-tay`
* `i-tsu`
* `sgn-BE-FR`
* `sgn-BE-NL`
* `sgn-CH-DE`
### `function warning(reason, code, offset)`
Called when an error occurs.
###### Parameters
* `reason` (`string`)
— English reason for failure
* `code` (`number`)
— code for failure
* `offset` (`number`)
— index-based place where the error occurred in the tag
###### Warnings
| code | reason |
| :--- | :--------------------------------------------------------------------- |
| 1 | Too long variant, expected at most 8 characters |
| 2 | Too long extension, expected at most 8 characters |
| 3 | Too many extended language subtags, expected at most 3 subtags |
| 4 | Empty extension, extensions must have at least 2 characters of content |
| 5 | Too long private-use area, expected at most 8 characters |
| 6 | Found superfluous content after tag |
## Types
This package is fully typed with [TypeScript][].
It exports additional `Schema`, `Extension`, `Warning`, and `Options` types that
model their respective interfaces.
## Compatibility
This package is at least compatible with all maintained versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
It also works in Deno and modern browsers.
## Security
This package is safe.
## Related
* [`wooorm/bcp-47-match`](https://github.com/wooorm/bcp-47-match)
— match BCP 47 language tags with language ranges per RFC 4647
* [`wooorm/bcp-47-normalize`](https://github.com/wooorm/bcp-47-normalize)
— normalize, canonicalize, and format BCP 47 tags
* [`wooorm/iso-3166`](https://github.com/wooorm/iso-3166)
— ISO 3166 codes
* [`wooorm/iso-639-2`](https://github.com/wooorm/iso-639-2)
— ISO 639-2 codes
* [`wooorm/iso-639-3`](https://github.com/wooorm/iso-639-3)
— ISO 639-3 codes
* [`wooorm/iso-15924`](https://github.com/wooorm/iso-15924)
— ISO 15924 codes
* [`wooorm/un-m49`](https://github.com/wooorm/un-m49)
— UN M49 codes
## Contribute
Yes please!
See [How to Contribute to Open Source][contribute].
## License
[MIT][license] © [Titus Wormer][author]
<!-- Definitions -->
[build-badge]: https://github.com/wooorm/bcp-47/workflows/main/badge.svg
[build]: https://github.com/wooorm/bcp-47/actions
[coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/bcp-47.svg
[coverage]: https://codecov.io/github/wooorm/bcp-47
[downloads-badge]: https://img.shields.io/npm/dm/bcp-47.svg
[downloads]: https://www.npmjs.com/package/bcp-47
[size-badge]: https://img.shields.io/bundlephobia/minzip/bcp-47.svg
[size]: https://bundlephobia.com/result?p=bcp-47
[npm]: https://docs.npmjs.com/cli/install
[skypack]: https://www.skypack.dev
[license]: license
[author]: https://wooorm.com
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[typescript]: https://www.typescriptlang.org
[contribute]: https://opensource.guide/how-to-contribute/
[spec]: https://tools.ietf.org/rfc/bcp/bcp47.html
[warning]: #function-warningreason-code-offset
[schema]: #schema
[iso-639]: https://en.wikipedia.org/wiki/ISO_639
[iso-15924]: https://en.wikipedia.org/wiki/ISO_15924
[iso-3166-1]: https://en.wikipedia.org/wiki/ISO_3166-1
[un-m49]: https://en.wikipedia.org/wiki/UN_M.49
|