Options
All
  • Public
  • Public/Protected
  • All
Menu

Class TString

Wrap a fat string with methods to coerce it to a specific language and stringify it. TStrings are immutable; all methods that appear to modify a TString return a new one.

To create a new TString call cast or literal.

import { TString } from "interminimal";

const catsDict = {
en: { one: "cat", other: "cats" },
de: { one: "Katze", other: "Katzen" },
cy: {
zero: "cathod",
one: "gath",
two: "gath",
few: "cath",
many: "chath",
other: "cath"
}
};

const counts = [0, 1, 1.5, 2, 3, 6, 42];

// Count the cats in Welsh
const cat = TString.cast(catsDict);
const welshCat = cat.toLang(["cy", "en"]);
for (const count of counts) {
console.log(`${count} ${welshCat.toString(count)}`);
}
// 0 cathod
// 1 gath
// 1.5 cath
// 2 gath
// 3 cath
// 6 chath
// 42 cath

Hierarchy

  • TString

Index

Accessors

  • get language(): string
  • Get the current language of this TString. Throws an error if this is a floating TString with no language selected.

    Returns string

Methods

  • Attempt to translate this TString into one of a list of languages.

    const ts = new TString({
    en: "color",
    de: "Farbe"
    });
    // We'd like British English or French but we'll get "en"
    // (U.S. English) which is the best match for "en-GB"
    console.log(ts.toLang(["en-GB", "fr"]), toString()); // "color"

    Use the Language Stack Calculator to experiment with the search path that is used to find the best language match.

    Parameters

    • langs: LocaleStack

      an array of BCP47 language codes in descending preference order

    Returns TString

    a new TString with its language set to the best match

  • toString(count?: number): string
  • Render this TString as a string. When the TString contains plural forms an appropriate plural will be chosen based on count. The correct plural form is chosen using Intl.PluralRules.

    // Plurals
    const ts = new TString({
    en: { one: "cat", other: "cats" },
    de: { one: "Katze", other: "Katzen" }
    });
    console.log(ts.toLang(["de"]).toString(10)); // "Katzen"

    Parameters

    • Optional count: number

      an optional count to select a plural form

    Returns string

  • Cast a TString or TFatString to a TString. This and literal are the main ways of creating new a TString.

    Parameters

    • obj: TString | TFatString

      either a fat string or an existing TString

    • Optional lang: string

      an optional language

    Returns TString

    a TString which may be obj if obj is already a TString

  • literal(str: string, lang: string): TString
  • Cast a string literal and language into a single-language TString. This and cast are the main ways of creating new a TString.

    const ts = TString.literal("Hello", "en");
    // Try to convert to "de", won't work - we'll get "en" instead
    console.log(ts.toLang["de"].language); // "en"

    Parameters

    • str: string

      a regular string

    • lang: string

      the language of the string

    Returns TString

    a new TString

Generated using TypeDoc