Parse and stringify BCP 47 language tags
npm install 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.
* What is this?
* When should I use this?
* Install
* Use
* API
* [parse(tag[, options])](#parsetag-options)
* stringify(schema)
* Schema
* function warning(reason, code, offset)
* Types
* Compatibility
* Security
* Related
* Contribute
* License
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.
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.
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`
`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'
This package exports the following identifiers: parse and stringify.
There is no default export.
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).i-klingon
For example, does not match the BCP 47 language algorithm but istlh
considered valid by BCP 47 nonetheless.
It is suggested to use instead (the ISO 639-3 code for Klingon).normalize
When 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).en-GB-abcdefghi
So, for example, where by default an empty object is returnedforgiving
(as the language variant is too long), in mode the language ofschema is populated with en and the region is populated with GB.
###### options.warning
When given, warning is called when an error is encounteredFunction
([][warning]).
###### Returns
Parsed BCP 47 language tag ([Schema][schema]).
###### Throws
When tag is null or undefined.
Compile a [schema][schema] to a BCP 47 language tag.
###### Returns
BCP 47 language tag (string).
A schema represents a language tag.
A schema is deemed empty when it has neither language, irregular, regular,privateuse
nor (where an empty privateuse array is handled as noprivateuse 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).en
For example, (English) or cmn (Mandarin Chinese).
###### schema.extendedLanguageSubtags
Selected three-character [ISO 639][iso-639] codes(Array), such asyue 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 inhy-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).CN
For example, in cmn-Hans-CN (Mandarin Chinese, Simplified script, as used419
in China) or in es-419 (Spanish as used in Latin America and the
Caribbean).
###### schema.variants
5 to 8 (inclusive) character language variants (Array), such asrozaj and biske in sl-rozaj-biske (San Giorgio dialect of Resian dialect
of Slovenian).
###### schema.extensions
List of extensions (Array
###### schema.privateuse
List of private-use subtags (Array), 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-lojbancel-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-oedi-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
*
Called when an error occurs.
###### Parameters
* reason (string)code
— English reason for failure
* (number)offset
— code for failure
* (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 |
This package is fully typed with [TypeScript][].
It exports additional Schema, Extension, Warning, and Options types that
model their respective interfaces.
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.
This package is safe.
* wooorm/bcp-47-match
— match BCP 47 language tags with language ranges per RFC 4647
* wooorm/bcp-47-normalize
— normalize, canonicalize, and format BCP 47 tags
* wooorm/iso-3166
— ISO 3166 codes
* wooorm/iso-639-2
— ISO 639-2 codes
* wooorm/iso-639-3
— ISO 639-3 codes
* wooorm/iso-15924
— ISO 15924 codes
* wooorm/un-m49`
— UN M49 codes
Yes please!
See [How to Contribute to Open Source][contribute].
[MIT][license] © [Titus Wormer][author]
[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