BCP 47 Language Tag Parser

1.1.2 · active · verified Sun Apr 19

The `bcp47` package provides a parser for BCP 47 language tags, a critical standard used in internationalization (i18n) for identifying human languages. The current stable version is `1.2.0`, with a rapid succession of recent releases in April 2026, indicating active development or a recent relaunch. This library's core function is to parse a BCP 47 tag and return a structured object containing all extracted components, or `null` if the tag is syntactically invalid, which acts as an implicit validation mechanism. Its key differentiator lies in its focused adherence to the BCP 47 specification, offering a lightweight and precise tool for tasks like content negotiation, managing localization resources, or configuring user interface languages. Unlike broader i18n frameworks, `bcp47` provides a dedicated, minimalist solution for this specific parsing requirement.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing valid and invalid BCP 47 language tags, showing the structured output and the `null` return for invalid input. Also includes stringification.

import { parse, stringify } from 'bcp47';

// Example 1: Parsing a valid language tag
const tag1 = 'en-US-u-attr-value';
const parsedTag1 = parse(tag1);
console.log('Parsed Tag 1:', parsedTag1);
// Expected output: { language: 'en', extlangs: [], script: null, region: 'US', variants: [], extensions: [{ singleton: 'u', privateuse: 'attr-value' }], privateuse: null }

// Example 2: Parsing an invalid language tag
const tag2 = 'invalid-tag-$$';
const parsedTag2 = parse(tag2);
console.log('Parsed Tag 2 (invalid):', parsedTag2);
// Expected output: null

// Example 3: Parsing a tag with script and variant
const tag3 = 'zh-Hans-SG-pinyin';
const parsedTag3 = parse(tag3);
console.log('Parsed Tag 3:', parsedTag3);

// Example 4: Stringifying a parsed object back to a tag
if (parsedTag3) {
  const rebuiltTag = stringify(parsedTag3);
  console.log('Rebuilt Tag 3:', rebuiltTag);
}

view raw JSON →