Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→is-language-codenext

is-language-code5.2.2

Is given string a language code (as per IANA)

Downloads per monthChangelogMIT LicensePlayground
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — ISLANGCODE()
  • DEPRECATED…
  • API — VERSION
  • Changelog

No dependencies whatsoever. This package declares no dependencies or devDependencies.

Permalink to InstallationInstallation

Permalink to Quick TakeQuick Take

Permalink to ExamplesExamples

  • Match registered tags without regard to letter case
  • Explain a language tag containing two region subtags
  • Explain an extended language subtag with the wrong prefix
  • Validate an extended language subtag after its registered prefix
  • Validate multiple uniquely prefixed extensions
  • Accept an IANA-grandfathered language tag
  • Reject text that does not resemble a language tag
  • Validate a private-use language tag
  • Explain why a repeated variant is invalid
  • Validate language, script, region, and variant subtags together
  • Explain an extension singleton without extension content
  • Various examples
Open is-language-code playground

Purpose

is-language-code checks whether a value is a valid BCP 47 language tag. It follows RFC 5646opens in a new tab and validates registered subtags against the IANA Language Subtag Registryopens in a new tab.

Language tags appear in places such as the HTML hreflang attribute:

<link rel="alternate" href="https://example.com" hreflang="es-ES" />

A regular expression can check a tag’s broad shape, but it cannot confirm that each subtag is registered or appears in the right position. This package checks both. It also returns a human-readable reason when a value is invalid. For example, de-419-DE is invalid because it contains two region subtags.

API — isLangCode()

The main function isLangCode() is imported like this:

The function accepts one optional value:

function isLangCode(str?: unknown):
  | { res: true; message: null }
  | { res: false; message: string };

Pass the candidate language tag as str. An omitted value, a non-string value, or an empty string returns an invalid result instead of throwing.

The function returns a plain object:

KeyTypeDescription
res
Type: boolean
resbooleanWhether the value is a valid language tag.
message
Type: string or null
messagestring or nullThe reason an invalid value failed, or null.
isLangCode("zh-Hans-CN");
// => { res: true, message: null }

isLangCode("de-419-DE");
// => { res: false, message: 'Two region subtags, "419" and "de".' }

isLangCode(null);
// => { res: false, message: "Not a string given." }

Language tags are case-insensitive. Diagnostic messages normalize referenced subtags to lowercase, so en-US-POSIX produces:

{
  res: false,
  message: 'Unrecognised subtag, "posix".'
}

Treat message as an explanation for people, not as a stable error code. Branch on res in application logic.

Deprecated language subtags

The IANA registry retains some deprecated language subtags for compatibility. They are valid inputs, but their preferred replacements should be used for new content:

Valid deprecated subtagPreferred subtag
iw
iwhe
ji
jiyi
in
inid

This function validates tags; it does not rewrite a valid deprecated subtag to its preferred replacement.

API — version

You can import version:

Permalink to changelogChangelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab