TagoIO SDK for JavaScript and TypeScript
    Preparing search index...

    Class Dictionary

    Multi-language support for TagoIO applications

    This class provides internationalization (i18n) functionality for TagoIO applications, allowing you to manage translations and apply language-specific content dynamically. Supports expression parsing, language switching, and translation management.

    import { Dictionary } from "@tago-io/sdk";

    const dictionary = new Dictionary({
    token: "your-token",
    language: "en"
    });

    // Apply translations to a string
    const translated = await dictionary.applyToString(
    "Welcome #DICT.GREETING#!",
    { language: "pt" }
    );
    // Parse translation expressions
    const expressions = dictionary.getExpressionsFromString("#DICT.HELLO# #DICT.WORLD#");

    // Resolve specific expression
    const value = await dictionary.resolveExpression({
    expression: { scope: "DICT", key: "HELLO" },
    language: "es"
    });
    Index

    Constructors

    Properties

    language: string
    runURL?: string

    Methods

    • Apply the dictionary over a string, parsing the expressions in the string and replacing them with the values found for the respective keys inside the dictionary for a language.

      Always returns a string. Return the translated string if there are dictionary expressions, the raw string with no changes if there are no expressions, and an empty string if rawString is undefined.

      Parameters

      • rawString: string

        String with words and/or expressions

      • Optional_options: IApplyToStringOptions

        Object containing options for the dictionary

      Returns Promise<string>

      Translated string with all expressions replaced

      const dictionary = new Dictionary({ language: "en-US", token: "my-token" });
      const result = await dictionary.applyToString("Words are ignored #TEST.DICT_KEY#");
    • Get all (and only) the expressions in a string and their parameters if applicable, ignoring normal words and phrases.

      Parameters

      • rawString: string

        String with words and/or expressions

      Returns Promise<IParsedExpression[]>

      Array of parsed expressions found in the string

      const dictionary = new Dictionary({ language: "en-US", token: "my-token" });
      const expressions = await dictionary.getExpressionsFromString("Words are ignored #TEST.DICT_KEY#");
    • Get the language data for a dictionary.

      Parameters

      • dictionary: string

        ID or Slug of the dictionary

      • language: string = ...

        Language code (defaults to instance language)

      Returns Promise<LanguageData>

      Language data or null if not found

      Error if parameters are missing

    • Get value from a key in a specific dictionary for a language.

      Parameters

      • language: string

        Name of the language (locale code)

      • dictionary: string

        Name of the dictionary

      • key: string

        Name of the key

      Returns Promise<string>

      The translated value or the expression if not found

      Error if parameters are missing

      const dictionary = new Dictionary({ language: "en-US", token: "my-token" });
      const value = await dictionary.getValueFromKey("en-US", "TEST", "OK_BUTTON_LABEL");
    • Parse an expression and extract the names of the dictionary, the key, and any arguments that are passed in the expression.

      Returns null if the value passed is not parseable

      Parameters

      • expression: string

        String expression

      Returns IParsedExpression

      Parsed expression or null if invalid

      const dictionary = new Dictionary({ language: "en-US", token: "my-token" });
      const value = dictionary.parseExpression("#TAGORUN.WELCOME_TEXT,Hello");
    • Resolve an expression in a language, replacing the parameters in the dictionary value with the arguments passed in the expression.

      Parameters

      Returns Promise<string>

      Resolved string with parameters replaced

      const dictionary = new Dictionary({ language: "en-US", token: "my-token" });
      const value = await dictionary.resolveExpression({
      language: "en-US",
      expression: {
      dictionary: "TEST",
      key: "SOME_KEY",
      params: ["first parameter"],
      },
      });