Parse and stringify BCP 47 language tags.
- What is this?
- When should I use this?
- Install
- Use
- API
- Compatibility
- Security
- Related
- Contribute
- License
This is a package that can parse BCP 47 language tags to objects 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. In Node.js (version 14.14+), install with npm:
npm install bcp-47In browsers with esm.sh:
<script type="module">
import {parse, stringify} from 'https://esm.sh/bcp-47@2?bundle'
</script>import {parse, stringify} from 'bcp-47'
const schema = parse('hy-Latn-IT-arevela')
console.log(schema)
console.log(stringify(schema))Yields:
{
extendedLanguageSubtags: [],
extensions: [],
irregular: null,
language: 'hy',
privateuse: [],
region: 'IT',
regular: null,
script: 'Latn',
variants: [ 'arevela' ]
}hy-Latn-IT-arevela
This package exports the identifiers
parse and
stringify.
There is no default export.
It exports the additional TypeScript types
Extension,
Options,
Schema, and
Warning.
Extension subtag with its singleton (TypeScript type).
For example, u-co-phonebk in de-DE-u-co-phonebk.
extensions(Array<string>) — subtags, such ascoandphonebksingleton(string) — single character that is notx, such asu
Configuration for parse (TypeScript type).
forgiving(boolean, default:false) — keep what was found before an error, instead of returning an empty schemanormalize(boolean, default:true) — replace deprecated tags with their preferred value, such asi-klingonwithtlhwarning(Warning, optional) — called when an error is found
Parsed language tag (TypeScript type).
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).
extendedLanguageSubtags(Array<string>) — extended language subtags, such asyueinzh-yue-HKextensions(Array<Extension>) — extensions, such asu-co-phonebkinde-DE-u-co-phonebkirregular(string, optional) — irregular legacy tag, such asi-defaultlanguage(string, optional) — ISO 639 language code or registered language subtag, such asenorcmnprivateuse(Array<string>) — private-use subtags, such asfooinen-x-fooregion(string, optional) — ISO 3166-1 or UN M49 code, such asCNincmn-Hans-CNor419ines-419regular(string, optional) — regular legacy tag, such aszh-minscript(string, optional) — ISO 15924 script code, such asLatninhy-Latn-IT-arevelavariants(Array<string>) — variant subtags, such asrozajandbiskeinsl-rozaj-biske
Called when an error occurs (TypeScript type).
reason(string) — reason for failure in Englishcode(number) — code for failureoffset(number) — index of place where the error occurred in the tag
Nothing (undefined).
| 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 |
| 7 | Empty private-use area, expected at least 1 subtag |
Parse a BCP 47 language tag.
👉 Note: the algorithm is case insensitive.
tag(string, required) — BCP 47 tag to parseoptions(Options, optional) — configuration
Parsed BCP 47 language tag (Schema).
When tag is null or undefined.
Compile a language schema to a BCP 47 language tag.
schema(Partial<Schema>, optional) — parsed language tag
BCP 47 language tag (string).
This package is compatible with maintained versions of Node.js.
When we cut a new major release,
we drop support for unmaintained versions of Node.
This means we try to keep the current release line,
bcp-47@2,
compatible with Node.js 14.14.
This package is safe.
wooorm/bcp-47-match— match BCP 47 language tags with language ranges per RFC 4647wooorm/bcp-47-normalize— normalize, canonicalize, and format BCP 47 tagswooorm/iso-3166— ISO 3166 codeswooorm/iso-639-2— ISO 639-2 codeswooorm/iso-639-3— ISO 639-3 codeswooorm/iso-15924— ISO 15924 codeswooorm/un-m49— UN M49 codes
Yes please! See How to Contribute to Open Source.