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

prevOpen Source→is-language-codenext

is-language-code5.2.0

Is given string a language code (as per IANA)

Downloads per monthChangelogMIT LicensePlayground
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — ISLANGCODE()
  • API — VERSION
  • BY THE WAY
  • Changelog

Installation

Quick Take

Examples

  • 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

This program tells, is a given string a valid language tag.

It is based on RFC #5646opens in a new tab “Tags for Identifying Languages” which was released in 2009 and as of Jan 2020 it is still currentopens in a new tab.

Language tags are used in many places, for example, in HTML attribute hreflang:

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

It’s impossible to properly match the spec using regex only — you can validate that allowed characters are in allowed places but you can’t validate the meaning those characters have. The position of subtag and arrangement matters. Also, this program returns explanations why it deemed the input not to be a language tag.

For example, de-419-DE is wrong because it contains two region tags, 419 and DE.

Existing regex-based solutions like ietf-language-tag-regexopens in a new tab don’t have much of a logic besides enforcing subtag order and subtag length, for example, it reports any string, longer than two characters, as a valid language tag. We, on other hand, validate each value against known IANA-registered names.

API — isLangCode()

The main function isLangCode() is imported like this:

It’s a function which takes one input argument:

Theoretically, the input string is optional — if the input is not a string or an empty string, a false will be returned. The program is liberal and never throws errors.

Function returns a plain object:

{
  res: boolean,
  message: null | string
}
Key’s nameTypeDescription
res
Type: boolean
resbooleanAnswers, is this a valid language code
message
Type: null or string
messagenull or stringExplains what’s wrong if the answer is negative

For example,

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

or

{
  res: true,
  message: null
}

Non-string or empty-string inputs always yield false, program does not throw.

Language tags are not case-sensitive (there exist conventions for the capitalization of some of the subtags but they don’t carry meaning). For performance reasons, all references of the input uses lowercase, even if you entered in uppercase. For example, en-US-POSIX would get reported as lowercase “posix”:

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

API — version

You can import version:

By the way

Back in 1989, code iw was replaced with he so we won’t include iw. Similar way, ji and in are not included.

The following codes have been added in 1989 (nothing later): ug (Uigur), iu (Inuktitut, also called Eskimo), za (Zhuang), he (Hebrew, replacing iw), yi (Yiddish, replacing ji), and id (Indonesian, replacing in). — https://www.ietf.org/rfc/rfc1766.txtopens in a new tab

Changelog

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