The GobstonesTranslator class is in charge of translating all keywords and built-in commands and expressions from a source locale to a destination locale, as well as providing an abstract 'Tokens' intermediate output that is locale agnostic.

You can construct an instance of this class (called a translator, for short) by providing a set of GobstonesTranslatorOptions to the constructor, which are then stored in the newly created instance as it's configuration.

The configuration of a translator is private and cannot be changed once constructed.

You can the ask the translator to perform different operations, such as translating from a locale specific Gobstones Localized code to Gobstones Abstract code (see toTokens), or go the other way around (see fromTokens), or event translate between locales (see translate). The actions the translator is allowed to perform depend on the configuration given at construction time to the translator, so for example, a translator that was created without a destination language is not able to translate from one language to another (See constructor and the aforementioned operations for more information)

Hierarchy

  • GobstonesTranslator

Constructors

Properties

from: string

The locale which this translator will assume as input language when given Gobstones Localized code. This is the source locale used in toTokens and as the source locale in translate.

names: BidirectionalMap<string, string>

A set of additional names for this translator used to translate additional words besides the built-in commands, expressions and keywords of the Gobstones Language.

See

names for more info.

registeredLocales: Record<string, LanguageMap>

A set of registered locales for this translator. This is automatically populated with the built-in supported locales, and expanded with the user defined locales given as argument to the constructor.

See

locales for more info on adding custom locales.

to: string

The locale which this translator will assume as output locale when given Gobstones Abstract code. This is the destination locale used in fromTokens and as the destination locale in translate.

tokenPrefix: string

This is the prefix attached or expected to every abstract token when reading or writing to Gobstones Abstract code. This prefix defaults to '$' if none is given, as this symbol is not a valid token of the Gobstones Localized code, and avoids unexpected collisions.

See

the "Caveats" section in the README for more information about collisions.

tokenSuffix: string

This is the suffix attached or expected to every abstract token when reading or writing to Abstract Gobstones Language code. This suffix is defaulted to '$', if none is given, as this symbol is not a valid token of the Gobstones Language, and avoids unexpected collisions.

See

the "Caveats" section in the README for more information about collisions.

Methods

  • Convert a given code containing abstract tokens to a Gobstones Language code in the to language. The input code should be a string containing Gobstones Code with abstract tokens instead of language specific code (Abstract Gobstones Code).

    Returns

    A string containing Gobstones Code in to.

    Throws

    if the translator was configured without a to option.

    Throws

    if the option includeNames was given, but no names option was used when creating this translator.

    Parameters

    • code: string

      The Gobstones Code to translate. Assumed to be Abstract Gobstones Code.

    • Optional options: TranslationOptions

      A set of additional options for the conversion.

    Returns string

  • The map to use when performing a translation. The map to use depends on whether or not the registered names are to be included.

    Returns

    A new map containing all translations that are to be used.

    Parameters

    • useNames: boolean

      Whether or not to include the registered names in the final map.

    • map: Map<string, string>

      The base map with translations from tokens to localized Gobstones Code or vice versa to use in the translation.

    • Optional namesMap: Map<string, string>

    Returns Map<string, string>

  • Retrieve the set of entries constructed from the extension of the given language definition with the given extended language.

    Throws

    if a locale name that is not a built-in supported language, nor a user defined language is given to as the LocaleDefinition.extends option of the language definition.

    Parameters

    • langDefinition: LocaleDefinition

      The language definition that extends another.

    • extendedLanguage: string

      The extended language name.

    Returns [string, string][]

  • Register the given languages in this translator instance.

    Throws

    if a locale name that is not a built-in supported language, nor a user defined language is given to as the LocaleDefinition.extends option in any of the language definitions.

    Throws

    if any of the given languages has a name that has been previously defined.

    Parameters

    • languages: Record<string, LocaleDefinition>

      An object containing language names as keys, and language definitions as their values.

    Returns void

  • Register a language to this instance existing languages.

    Throws

    if a locale name that is not a built-in supported language, nor a user defined language is given to as the LocaleDefinition.extends option of the language definition.

    Throws

    LocaleNameCollision if the given name has been used in a previously registered language.

    Parameters

    • name: string

      The name of the language to register.

    • langDefinition: LocaleDefinition

      The language definition to register.

    Returns void

  • Set a locale to the internal language manager of this library so that error messages are displayed translated to the specific locale.

    Parameters

    • locale: string

      The locale used to display error messages.

    Returns void

  • Convert a given code to a string containing abstract tokens for built-in commands and expressions and language keywords. The input code should be in the language this translator has configured as from.

    Returns

    A string containing Abstract Gobstones Code.

    Throws

    if the translator was configured without a from option.

    Throws

    if the option includeNames was given, but no names option was used when creating this translator.

    Parameters

    • code: string

      The Gobstones Language code to translate. Assumed to be in from.

    • Optional options: TranslationOptions

      A set of additional options for the conversion.

    Returns string

  • Translate a given code from the source language to the destination language. The source language is the one used as from when constructing the translator, and the destination language is that used as to. The input should be Gobstones Language code in the source language.

    Returns

    A string containing Gobstones Code in to.

    Throws

    if the translator was configured without a from option.

    Throws

    if the translator was configured without a to option.

    Throws

    if the option includeNames was given, but no names option was used when creating this translator.

    Parameters

    • code: string

      The Gobstones Language code to translate. Assumed to be in from.

    • Optional options: TranslationOptions

      A set of additional options for the conversion.

    Returns string

  • Translate a code string using a given set of word transformations to perform. The translation can be perform between language specific localized Gobstones Language code to Abstract Gobstones Code, the other way around, or between any to languages. The code is generic enough to just consider the elements on the given map.

    Returns

    The translated code using the rules in the map.

    Parameters

    • code: string

      The code to translate, may be Abstract Gobstones Code or localized Gobstones Code.

    • map: Map<string, string>

      The replacement map, where each key corresponds to a word to translate, and the value the corresponding translation for such a word.

    Returns string