Class Translator

The Translator Class provides translations of phrases used in ortac systems across multiple locales.

import {Translator} from '@ortac/libs-js';

const translator = new Translator([{pattern: './**‍/localeFiles*.json'}]);

console.log(translator.lookup('en-US', '_metres')); // => 'meters'

The Translator Class uses locale files per the ortac specification, currently supporting v1.0.0. The Class also provides for the ability to trace where the source of a particular translation has come from, and can also log all phrases passed into the translator.

Hierarchy

  • Translator

Constructors

  • Parameters

    • globs: {
          options?: GlobOptionsWithFileTypesUnset;
          pattern: string | string[];
      }[]

      glob array of glob patterns and options representing files to load

    Returns Translator

    Remarks

    The order of globs/files pass in the array is important. Files are loaded in the order found with later files overwriting the dictionary value from earlier sources

    Example

    import {Translator} from '@ortac/libs-js';

    const translator = new Translator([{pattern: './localeFiles*.json'}]);

    console.log(translator.lookup('en-US', '_metres')); // => 'meters'

Methods

  • Clears all entries from the log

    Parameters

    • Optional locale: string

      the locale to clear the logs for, or all locales if omitted

    Returns void

  • Clears the cache for the specified locale

    Parameters

    • locale: string

      BCP47 format

    Returns void

  • Gets the log containing a unique list of calls made to this dictionary

    Parameters

    • Optional locale: string

      the locale to get the logs for, or all locales if omitted

    Returns string[]

    array of phrases passed in for translation in the order first passed (ie no duplications)

    Remarks

    The returned array will always be an array of unique phrases even if the same phrase has been looked up in multiple locales

  • Parameters

    • locale: string

      BCP47 format

    • phrase: string

      the phrase to lookup in this translator

    Returns string

    translated phrase

  • Starts logging each call to this translator object

    Returns void

  • Stops logging each call to this translator object

    Returns void

    Remarks

    This does not empty the log - use clearLog() method

  • Returns a trace explaining which source(s) have been used by the translator on a particular phrase

    Parameters

    • locale: string

      BCP47 format

    • phrase: string

      the phrase to get a trace for

    Returns string

    a multiline string, one line per source with each successive source overriding previous sources for the phrase

Generated using TypeDoc