or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdsearch-discovery.mdsubtag-operations.mdtag-operations.md
tile.json

tessl/npm-language-tags

Work with IANA language tags based on BCP 47 standards for validation, parsing, and manipulation of language tags and subtags

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/language-tags@2.1.x

To install, run

npx @tessl/cli install tessl/npm-language-tags@2.1.0

index.mddocs/

Language Tags

A comprehensive JavaScript library for working with IANA language tags based on BCP 47 (RFC 5646) standards. This library enables validation, parsing, and manipulation of language tags and subtags with case-insensitive lookups and proper formatting according to RFC conventions.

Package Information

  • Package Name: language-tags
  • Package Type: npm
  • Language: JavaScript (ES Modules)
  • Installation: npm install language-tags
  • Minimum Node Version: 22+

Core Imports

import { tags, check, subtags, filter, search, languages, language, region, type, types, date } from "language-tags";

For legacy CommonJS usage:

const { tags, check, subtags, filter, search, languages, language, region, type, types, date } = require("language-tags");

Default import also provides access to all functions as properties (legacy support):

import tags from "language-tags";

// All functions available as properties for backward compatibility
tags.check("en-US");           // same as check("en-US")
tags.search("French");         // same as search("French") 
tags.subtags("en");           // same as subtags("en")
tags.filter(["en", "xyz"]);   // same as filter(["en", "xyz"])
tags.languages("zh");         // same as languages("zh")
tags.language("en");          // same as language("en")
tags.region("US");            // same as region("US")
tags.type("en", "language");  // same as type("en", "language")
tags.types("en");             // same as types("en")
tags.date();                  // same as date()

// Note: Legacy property access is planned for removal in v3

Basic Usage

import { tags, check, search } from "language-tags";

// Validate a language tag
const isValid = check("en-US");
console.log(isValid); // true

// Get detailed tag information
const tag = tags("en-US");
console.log(tag.valid()); // true
console.log(tag.language().descriptions()); // ["English"]
console.log(tag.region().descriptions()); // ["United States"]

// Search for tags by description
const results = search("French");
console.log(results[0].subtag); // "fr"

Architecture

The language-tags library is built around several key components:

  • Validation Functions: Core functions (tags, check) for validating language tags against BCP 47 standards
  • Lookup Functions: Utilities (subtags, search, languages) for finding and filtering language subtags
  • Tag Class: Comprehensive class representing a complete language tag with validation and formatting
  • Subtag Class: Individual subtag representation with type-specific functionality
  • Registry Data: Integration with IANA language subtag registry for up-to-date standards compliance

Capabilities

Tag Validation

Core functionality for validating and working with complete language tags. Provides comprehensive validation against BCP 47 standards with detailed error reporting.

function tags(tag: string): Tag;
function check(tag: string): boolean;

Tag Operations

Subtag Management

Utilities for working with individual language subtags including lookup, filtering, and type checking. Essential for analyzing tag components and building valid tags.

function subtags(subtag: string | string[]): Subtag[];
function filter(subtags: string[]): string[];
function types(subtag: string): string[];

Subtag Operations

Search and Discovery

Advanced search functionality for finding tags and subtags by description with support for regular expressions and case-insensitive matching.

function search(query: string | RegExp, all?: boolean): (Tag | Subtag)[];
function languages(macrolanguage: string): Subtag[];
function language(subtag: string): Subtag | null;
function region(subtag: string): Subtag | null;
function type(subtag: string, type: string): Subtag | null;

Search and Discovery

Registry Information

Access to metadata about the underlying IANA language subtag registry.

function date(): string;

Types

class Tag {
  constructor(tag: string);
  
  // Validation
  valid(): boolean;
  errors(): Error[];
  
  // Component access
  subtags(): Subtag[];
  language(): Subtag | null;
  region(): Subtag | null;
  script(): Subtag | null;
  find(type: string): Subtag | null;
  
  // Information
  type(): string;
  descriptions(): string[];
  preferred(): Tag | null;
  deprecated(): string | null;
  added(): string | undefined;
  
  // Formatting
  format(): string;
  
  // Error constants
  static ERR_DEPRECATED: number;
  static ERR_NO_LANGUAGE: number;
  static ERR_UNKNOWN: number;
  static ERR_TOO_LONG: number;
  static ERR_EXTRA_REGION: number;
  static ERR_EXTRA_EXTLANG: number;
  static ERR_EXTRA_SCRIPT: number;
  static ERR_DUPLICATE_VARIANT: number;
  static ERR_WRONG_ORDER: number;
  static ERR_SUPPRESS_SCRIPT: number;
  static ERR_SUBTAG_DEPRECATED: number;
  static ERR_EXTRA_LANGUAGE: number;
}

class Subtag {
  constructor(subtag: string, type: string);
  
  // Information
  type(): string;
  descriptions(): string[];
  preferred(): Subtag | null;
  deprecated(): string | null;
  added(): string | undefined;
  comments(): string[];
  scope(): string | null;
  script(): Subtag | null;
  
  // Formatting
  format(): string;
  toString(): string;
  
  // Error constants
  static ERR_NONEXISTENT: number;
  static ERR_TAG: number;
}