Work with IANA language tags based on BCP 47 standards for validation, parsing, and manipulation of language tags and subtags
npx @tessl/cli install tessl/npm-language-tags@2.1.0A 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.
npm install language-tagsimport { 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 v3import { 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"The language-tags library is built around several key components:
tags, check) for validating language tags against BCP 47 standardssubtags, search, languages) for finding and filtering language subtagsCore 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;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[];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;Access to metadata about the underlying IANA language subtag registry.
function date(): string;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;
}