Socket
Book a DemoInstallSign in
Socket

typesbcp47

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typesbcp47

TypeScript library for parsing BCP47 language tags

1.9.0
latest
Source
npmnpm
Version published
Weekly downloads
372
59.66%
Maintainers
1
Weekly downloads
 
Created
Source

TypesBCP47

TypeScript library for language tags from BCP47

Installation

npm install typesbcp47

Class LanguageUtils provides static methods for parsing and validating language tags from BCP47.

MethodDescription
getTagDescription(tag: string): stringReturns a language description for the tag if the tag is a valid language tag. Returns undefined otherwise.
normalizeCode(code: string): stringReturns a normalized code if the code is valid.
isCJK(code: string): booleanReturns true if the language is Chinese, Japanese, Korean, Vietnamese or Aiunu. Returns false otherwise.
isBiDi(code: string): booleanReturns true if the language is written from right to left (Arabic, Hebrew, Persian, Urdu), false otherwise.
getLanguages(locale: string): Array<Language>Returns an array of Language objects with descriptions in the selected locale.
getLanguage(code: string, locale: string): LanguageReturns a Language object by its code with descriptions in the selected locale if the code is valid. Returns undefined otherwise.
getCommonLanguages(locale: string): Array<Language>Returns an array of most common Language objects with descriptions in the selected locale.

Valid values for locale parameter are en, es and fr.

You can combine these methods to validate language tags:

import { LanguageUtils } from 'typesbcp47';

const tag = 'en-US';
const description = LanguageUtils.getTagDescription(tag);

if (description) {
  console.log(`Language tag ${tag} is valid. Description: ${description}`);
} else {
  console.log(`Language tag ${tag} is invalid.`);
}

// a common problem with language tags is wrong casing for the code
// this can be fixed by normalizing the code

const normalizedCode = LanguageUtils.normalizeCode(tag);

if (normalizedCode) {
    // language exists
    if (tag === normalizedCode) {
        // language tag is OK
        console.log(`Language tag ${tag} is valid.`);
    } else {
        // language tag is not normalized
        console.log(`Language code is incorrect, it should be ${normalizedCode}`)
    }
} else {
  console.log(`Code ${tag} is invalid.`);
}

Keywords

typescript

FAQs

Package last updated on 29 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.