bcp-47
Parse and stringify BCP 47 language tags.
Contents
- What is this?
- When should I use this?
- Install
- Use
- API
- Compatibility
- Security
- Related
- Contribute
- License
What is this?
This is a package that can parse BCP 47 language tags to objects representing them, and serialize those objects back into language tags. It reports problems in tags as structured messages: fatal ones when a tag does not match the BCP 47 grammar (it is not well-formed), and other ones when a tag matches the grammar but breaks other rules (it is not valid), such as a repeated singleton or variant. It supports a forgiving mode to handle incorrect BCP 47 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. In Node.js (version 22+), install with npm:
npm install bcp-47
In browsers with esm.sh:
<script type="module">
import {parse, stringify} from 'https://esm.sh/bcp-47@3?bundle'
</script>
Use
import {parse, stringify} from 'bcp-47'
const schema = parse('hy-Latn-IT-arevela')
console.log(schema)
console.log(stringify(schema))
Yields:
{
extendedLanguageSubtags: [],
extensions: [],
language: 'hy',
privateuse: [],
region: 'IT',
script: 'Latn',
type: 'language',
variants: [ 'arevela' ]
}
hy-Latn-IT-arevela
API
This package exports the identifiers
parse and
stringify.
There is no default export.
It exports the additional TypeScript types
Extension,
Irregular,
IrregularSchema,
LanguageSchema,
LanguageSchemaInput,
Message,
OnMessage,
Options,
PrivateUseSchema,
Regular,
RegularSchema,
RuleId,
Schema,
SchemaInput, and
Singleton.
Extension
Extension subtag with its singleton (TypeScript type).
For example, u-co-phonebk in de-DE-u-co-phonebk.
Fields
extensions(Array<string>) — subtags, such ascoandphonebksingleton(Singleton) — singleton, such asu
Irregular
Irregular legacy tag (TypeScript type).
These tags do not match the BCP 47 grammar.
Type
type Irregular =
| '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'
IrregularSchema
Parsed irregular legacy tag (TypeScript type).
Fields
type('irregular') — kind of tagvalue(Irregular) — irregular legacy tag, such asi-default
LanguageSchema
Parsed language tag (TypeScript type).
Fields
extendedLanguageSubtags(Array<string>) — extended language subtags, such asyueinzh-yue-HKextensions(Array<Extension>) — extensions, such asu-co-phonebkinde-DE-u-co-phonebklanguage(string) — ISO 639 language code or registered language subtag, such asenorcmnprivateuse(Array<string>) — private-use subtags, such asfooinen-x-fooregion(stringorundefined) — ISO 3166-1 or UN M49 code, such asCNincmn-Hans-CNor419ines-419script(stringorundefined) — ISO 15924 script code, such asLatninhy-Latn-IT-arevelatype('language') — kind of tagvariants(Array<string>) — variant subtags, such asrozajandbiskeinsl-rozaj-biske
LanguageSchemaInput
Language tag to serialize (TypeScript type).
Like LanguageSchema,
LanguageSchemaInput,
but fields without a value can be omitted.
Fields
extendedLanguageSubtags(Array<string>, optional) — extended language subtagsextensions(Array<Extension>, optional) — extensionslanguage(string) — languageprivateuse(Array<string>, optional) — private-use subtagsregion(string, optional) — regionscript(string, optional) — scripttype('language') — kind of tagvariants(Array<string>, optional) — variant subtags
Message
Message about a problem in a tag (TypeScript type).
Fields
actual(string) — part of the tag that is reported, such asabcdefghiexpected(Array<string>, optional) — suggested values to use instead ofactualfatal(boolean) — whether the tag could not be parsed;truewhen it does not match the BCP 47 grammar (it is not well-formed),falsewhen it matches the grammar but breaks another rule (it is not valid)range([start: number, end: number]) — offsets of the reported part in the tag (0-indexed start, and the index after the end)reason(string) — reason for the message, in English (uses markdown)ruleId(RuleId) — category of the message, such as'variant-too-long'source('bcp-47') — namespace of the message
OnMessage
Called when a problem is found (TypeScript type).
Parameters
message(Message) — message about a problem in a tag
Returns
Nothing (undefined).
Options
Configuration for parse (TypeScript type).
Fields
forgiving(boolean, default:false) — keep what was found before a fatal error, instead of returningundefinednormalize(boolean, default:true) — replace deprecated tags with their preferred value, such asi-klingonwithtlhonmessage(OnMessage, optional) — called when a problem is found
PrivateUseSchema
Parsed private-use tag (TypeScript type).
Fields
privateuse([first: string, ...rest: Array<string>]) — private-use subtags, such asfooinx-footype('privateuse') — kind of tag
Regular
Regular legacy tag (TypeScript type).
These tags match the BCP 47 grammar but mean something different.
Type
type Regular =
| 'art-lojban'
| 'cel-gaulish'
| 'no-bok'
| 'no-nyn'
| 'zh-guoyu'
| 'zh-hakka'
| 'zh-min'
| 'zh-min-nan'
| 'zh-xiang'
RegularSchema
Parsed regular legacy tag (TypeScript type).
Fields
type('regular') — kind of tagvalue(Regular) — regular legacy tag, such aszh-min
RuleId
Category of a message (TypeScript type).
Type
type RuleId =
// Example: `aa-bbb-ccc-ddd-eee`.
| 'extended-language-subtags-too-many'
// Example: `en-i-a`.
| 'extension-empty'
// Example: `en-i-abcdefghi`.
| 'extension-too-long'
// Example: `en-x`.
| 'private-use-empty'
// Example: `en-x-abcdefghi`.
| 'private-use-too-long'
// Example: `en-a-bbb-a-ccc` (not fatal).
| 'singleton-duplicate'
// Example: `en-i`.
| 'tag-unexpected-content'
// Example: `de-DE-1901-1901` (not fatal).
| 'variant-duplicate'
// Example: `en-GB-abcdefghi`.
| 'variant-too-long'
Schema
Parsed tag (TypeScript type).
Check type to find out which kind of tag it is.
Type
type Schema =
| IrregularSchema
| LanguageSchema
| PrivateUseSchema
| RegularSchema
SchemaInput
Tag to serialize (TypeScript type).
Like Schema,
but with LanguageSchemaInput
instead of LanguageSchema.
Type
type SchemaInput =
| IrregularSchema
| LanguageSchemaInput
| PrivateUseSchema
| RegularSchema
Singleton
Extension singleton (TypeScript type).
Any lowercase ASCII letter or digit except x.
Type
type Singleton =
| '0'
// …
| '9'
| 'a'
// …
| 'w'
| 'y'
| 'z'
parse(tag[, options])
Parse a BCP 47 language tag.
Note: the algorithm is case insensitive. Subtags are returned in their canonical casing (RFC 5646 § 2.1.1).
When a tag does not match the BCP 47 grammar,
parsing stops with a fatal message,
and undefined is returned
(or what was found before the problem, in forgiving mode).
When a tag matches the grammar but breaks another rule,
such as a repeated singleton,
a non-fatal message is emitted and parsing continues.
Parameters
tag(string) — BCP 47 tag to parseoptions(Options, optional) — configuration
Returns
Parsed BCP 47 language tag (Schema),
or undefined when nothing was parsed.
stringify(schema)
Compile a language schema to a BCP 47 language tag.
Parameters
schema(SchemaInput, optional) — tag to serialize
Returns
BCP 47 language tag (string).
Throws
When schema would not make a well-formed tag,
such as an unknown legacy tag,
a region of 3 letters,
or a singleton of x.
Compatibility
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@3,
compatible with Node.js 22.
Security
This package is safe.
Related
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
Contribute
Yes please! See How to Contribute to Open Source.