@grafctory/core - v0.15.0
    Preparing search index...

    Module language

    BCP 47 language tags and RFC 4647 basic language ranges, with language detection and localised text in language maps.

    Language Tags

    A value can be checked for a well-formed language tag before it reaches an API that expects one:

    import { isTag } from "@grafctory/core/language";

    if (isTag(value)) {
    // value is typed as Tag
    }

    Basic Language Ranges

    A value can be checked in the same way for a well-formed basic language range, wildcard included:

    import { isTagRange } from "@grafctory/core/language";

    if (isTagRange(value)) {
    // value is typed as TagRange
    }

    Matching

    A tag can be tested against the ranges a client accepts, to select the content that answers a request:

    import { matchTag } from "@grafctory/core/language";

    matchTag("de-CH", "de"); // true - Swiss German matches German range

    Naming

    A tag can be shown to readers as the name of its language, written in a language they understand:

    import { nameTag } from "@grafctory/core/language";

    nameTag("zh-Hans"); // "Simplified Chinese" - named in English by default
    nameTag("zh-Hans", "it"); // "cinese semplificato" - named in Italian

    Identifying

    A language named in words rather than in a tag can be read as a tag, whatever language the name is written in:

    import { identifyTag } from "@grafctory/core/language";

    identifyTag("German"); // "de"
    identifyTag("tedesco"); // "de" - named in Italian

    Inferring

    The localised versions of a web page can be told apart by the language their address states:

    import { inferTag } from "@grafctory/core/language";

    inferTag("https://example.org/en/"); // "en"
    inferTag("https://example.org/page.php?lang=de"); // "de"

    Language Maps

    Localised text can be cleaned up in one pass, whether each language carries one label or several:

    import { localised } from "@grafctory/core/language";

    const trimmed = localised(value => value.trim());

    trimmed({ en: " Name ", it: " " }); // { en: "Name" } - emptied languages dropped
    TagPattern

    Regular expression matching BCP 47 language tags.

    TagRangePattern

    Regular expression matching RFC 4647 basic language ranges.

    Tag

    Language tag as defined by BCP 47/RFC 5646 § 2.1.

    TagRange

    Basic language range as defined by RFC 4647 § 2.1.

    Localised

    Language map holding localised text, one entry per language.

    isTag

    Checks if a value is a valid language tag.

    isTagRange

    Checks if a value is a valid basic language range.

    matchTag

    Checks if a language tag matches a basic language range.

    nameTag

    Names the language identified by a language tag.

    identifyTag

    Identifies the language a name refers to.

    inferTag

    Infers the language of a web page from its address.

    localised

    Creates a function that transforms every text of a Localised language map.