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

    Function inferTag

    • Infers the language of a web page from its address.

      Tells apart the localised versions of a page published on the same site, as https://example.org/en/ and https://example.org/pt, which share a host and differ only in the language their address states. The language is read from the first of the following parts of the address that states one, ignoring any later ones:

      • a lang, language, locale, lng or hl query parameter holding a two- or three-letter code, as in ?lang=en or ?lang=fil, or a language name in any language, as in ?lang=English or ?lang=Inglese
      • a path segment holding a two-letter code, with an optional region, as in /en/, /en-gb/ or /courses/EN/
      • an underscore- or space-separated file name word holding a code or a name of a language with a two-letter code, as in home_en.htm, report_eng.pdf or Guida – Versione Inglese.pdf, ignoring the file extension

      Hosts and the words of hyphenated slugs are never read, so /universite-de-paris isn't taken as German. Codes that identify no language, as und or zxx, are ignored. Path segments and file name words admit fewer codes to keep ordinary words from passing for languages: /iii/, /mal/ and new_report.pdf aren't read as Sichuan Yi, Malayalam or Newari, the languages their three-letter codes would otherwise state.

      Tip

      Restrict languages to the languages a site is published in to rule out codes such as /id/ or /my/, which name records and personal areas far more often than they state Indonesian or Burmese.

      Parameters

      • url: URLLike

        The address of the page; addresses other than absolute http or https URLs state no language

      • Optionallanguages: readonly string[]

        The tags of the languages the page may be in, matched by base language, so that pt-PT admits pt; codes and names stating other languages are ignored, as if absent; if omitted, any language is read

      Returns Optional<string>

      The ISO 639-1 tag of the language url states, or its ISO 639-2 code if it has no two-letter tag, stripped of script and region subtags, or undefined if url states no language, or none admitted by languages

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

      inferTag("https://example.org/en/"); // "en"
      inferTag("https://example.org/page.php?lang=de"); // "de"
      inferTag("https://example.org/home_en.htm"); // "en"
      inferTag("https://example.org/id/1234"); // "id"
      inferTag("https://example.org/id/1234", ["en", "fr"]); // undefined - Indonesian not admitted
      inferTag("https://www.example.de/"); // undefined - hosts never read

      identifyTag for reading languages named in words