Work with IANA language tags based on BCP 47 standards for validation, parsing, and manipulation of language tags and subtags
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core functionality for validating and working with complete language tags according to BCP 47 (RFC 5646) standards.
Creates a Tag object for comprehensive validation and analysis of language tags.
/**
* Check whether a hyphen-separated tag is valid and well-formed. Always returns a Tag object.
* @param {string} tag - The language tag to validate (e.g., "en-US", "zh-Hans-CN")
* @returns {Tag} Tag instance for detailed analysis
*/
function tags(tag: string): Tag;Usage Examples:
import { tags } from "language-tags";
// Validate various tag formats
const simple = tags("en");
console.log(simple.valid()); // true
console.log(simple.format()); // "en"
const complex = tags("zh-hans-cn");
console.log(complex.valid()); // true
console.log(complex.format()); // "zh-Hans-CN"
const invalid = tags("invalid-tag-format");
console.log(invalid.valid()); // false
console.log(invalid.errors()); // Array of Error objectsQuick validation shortcut that returns a boolean result.
/**
* Shortcut for tags(tag).valid(). Returns true if the tag is valid, false otherwise.
* @param {string} tag - The language tag to check
* @returns {boolean} True if valid, false otherwise
*/
function check(tag: string): boolean;Usage Examples:
import { check } from "language-tags";
// Quick validation checks
console.log(check("en-US")); // true
console.log(check("fr-CA")); // true
console.log(check("invalid")); // false
console.log(check("en-XYZ")); // false (XYZ is not a valid region)Methods for checking tag validity and retrieving validation errors.
/**
* Check if the tag is valid according to BCP 47 standards
* @returns {boolean} True if valid, false otherwise
*/
valid(): boolean;
/**
* Get detailed validation errors if the tag is invalid
* @returns {Error[]} Array of Error objects with validation details
*/
errors(): Error[];Methods for accessing individual components of the language tag.
/**
* Get all subtags that make up this tag
* @returns {Subtag[]} Array of Subtag objects
*/
subtags(): Subtag[];
/**
* Get the language subtag (first component)
* @returns {Subtag|null} Language subtag or null if not found
*/
language(): Subtag | null;
/**
* Get the region subtag
* @returns {Subtag|null} Region subtag or null if not found
*/
region(): Subtag | null;
/**
* Get the script subtag
* @returns {Subtag|null} Script subtag or null if not found
*/
script(): Subtag | null;
/**
* Find a subtag of the specified type
* @param {string} type - Type to search for ("language", "region", "script", "variant", etc.)
* @returns {Subtag|null} Matching subtag or null if not found
*/
find(type: string): Subtag | null;Methods for retrieving metadata about the tag.
/**
* Get the tag type
* @returns {string} "grandfathered", "redundant", or "tag"
*/
type(): string;
/**
* Get descriptions for the tag
* @returns {string[]} Array of description strings
*/
descriptions(): string[];
/**
* Get the preferred tag if this tag is deprecated
* @returns {Tag|null} Preferred Tag object or null if not deprecated
*/
preferred(): Tag | null;
/**
* Get the deprecation date if the tag is deprecated
* @returns {string|null} Date string or null if not deprecated
*/
deprecated(): string | null;
/**
* Get the date when this tag was added to the registry
* @returns {string|undefined} Date string in YYYY-MM-DD format or undefined if not available
*/
added(): string | undefined;Methods for proper case formatting according to RFC conventions.
/**
* Format the tag according to RFC 5646 case conventions
* @returns {string} Properly formatted tag string
*/
format(): string;Usage Examples:
import { tags } from "language-tags";
const tag = tags("zh-hans-cn");
// Component access
console.log(tag.language().format()); // "zh"
console.log(tag.script().format()); // "Hans"
console.log(tag.region().format()); // "CN"
// Information
console.log(tag.type()); // "tag"
console.log(tag.valid()); // true
console.log(tag.format()); // "zh-Hans-CN"
// All subtags
const subtags = tag.subtags();
subtags.forEach(subtag => {
console.log(`${subtag.type()}: ${subtag.format()}`);
});
// Output:
// language: zh
// script: Hans
// region: CNThe Tag class provides constants for different validation error types:
static ERR_DEPRECATED = 1; // Tag is deprecated
static ERR_NO_LANGUAGE = 2; // No language subtag found
static ERR_UNKNOWN = 3; // Unknown subtag
static ERR_TOO_LONG = 4; // Subtag too long
static ERR_EXTRA_REGION = 5; // Multiple region subtags
static ERR_EXTRA_EXTLANG = 6; // Multiple extlang subtags
static ERR_EXTRA_SCRIPT = 7; // Multiple script subtags
static ERR_DUPLICATE_VARIANT = 8; // Duplicate variant subtags
static ERR_WRONG_ORDER = 9; // Subtags in wrong order
static ERR_SUPPRESS_SCRIPT = 10; // Script should be suppressed
static ERR_SUBTAG_DEPRECATED = 11; // Contains deprecated subtag
static ERR_EXTRA_LANGUAGE = 12; // Multiple language subtagsUsage Examples:
import { tags, Tag } from "language-tags";
const invalidTag = tags("en-US-US"); // Multiple regions
const errors = invalidTag.errors();
errors.forEach(error => {
if (error.code === Tag.ERR_EXTRA_REGION) {
console.log("Multiple region subtags detected");
console.log(`Tag: ${error.tag}`);
}
});