Skip to content
wooormPublic

About

Parse and stringify BCP 47 language tags

Topics

Resources

Stars

76 stars

Watchers

1 watching

Forks

Repository files navigation

bcp-47

Build Coverage Downloads Size

Parse and stringify BCP 47 language tags.

Contents

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 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. In Node.js (version 14.14+), install with npm:

npm install bcp-47

In browsers with esm.sh:

<script type="module">
  import {parse, stringify} from 'https://esm.sh/bcp-47@2?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: [],
  irregular: null,
  language: 'hy',
  privateuse: [],
  region: 'IT',
  regular: null,
  script: 'Latn',
  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, Options, Schema, and Warning.

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 as co and phonebk
  • singleton (string) — single character that is not x, such as u

Options

Configuration for parse (TypeScript type).

Fields
  • forgiving (boolean, default: false) — keep what was found before an error, instead of returning an empty schema
  • normalize (boolean, default: true) — replace deprecated tags with their preferred value, such as i-klingon with tlh
  • warning (Warning, optional) — called when an error is found

Schema

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).

Fields
  • extendedLanguageSubtags (Array<string>) — extended language subtags, such as yue in zh-yue-HK
  • extensions (Array<Extension>) — extensions, such as u-co-phonebk in de-DE-u-co-phonebk
  • irregular (string, optional) — irregular legacy tag, such as i-default
  • language (string, optional) — ISO 639 language code or registered language subtag, such as en or cmn
  • privateuse (Array<string>) — private-use subtags, such as foo in en-x-foo
  • region (string, optional) — ISO 3166-1 or UN M49 code, such as CN in cmn-Hans-CN or 419 in es-419
  • regular (string, optional) — regular legacy tag, such as zh-min
  • script (string, optional) — ISO 15924 script code, such as Latn in hy-Latn-IT-arevela
  • variants (Array<string>) — variant subtags, such as rozaj and biske in sl-rozaj-biske

Warning

Called when an error occurs (TypeScript type).

Parameters
  • reason (string) — reason for failure in English
  • code (number) — code for failure
  • offset (number) — index of place where the error occurred in the tag
Returns

Nothing (undefined).

Codes
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(tag[, options])

Parse a BCP 47 language tag.

👉 Note: the algorithm is case insensitive.

Parameters
  • tag (string, required) — BCP 47 tag to parse
  • options (Options, optional) — configuration
Returns

Parsed BCP 47 language tag (Schema).

Throws

When tag is null or undefined.

stringify(schema)

Compile a language schema to a BCP 47 language tag.

Parameters
Returns

BCP 47 language tag (string).

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@2, compatible with Node.js 14.14.

Security

This package is safe.

Related

Contribute

Yes please! See How to Contribute to Open Source.

License

MIT © Titus Wormer

About

Parse and stringify BCP 47 language tags

Topics

Resources

Stars

76 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages