Skip to main content

TextCache

A class that can be used to cache a simple key/value CSV file for easy text lookups. Text lookups will be automatically translated to the game's current language, if a translation is available.

Index

Constructors

constructor

  • new TextCache(path: string, _build_cache?: boolean): TextCache
  • Creates a new TextCache from the provided CSV file path.


    Parameters

    • path: string

      The path to the CSV lookup file for this TextCache instance

    • _build_cache: boolean = true

    Returns TextCache

Properties

cache

cache: Record<string, string>

language

language: string

loaded

loaded: boolean

path

path: string

rebuildListeners

rebuildListeners: (cache?: TextCache) => void[]

Methods

buildCache

  • buildCache(): Promise<void>
  • Kicks off a build of the text lookup cache


    Returns Promise<void>

buildTranslations

  • buildTranslations(lines: string[][], translations: string[]): string[][]
  • Maps lines of a CSV to equivalent CSV lines with values translated according to the corresponding translation file


    Parameters

    • lines: string[][]

      An array of string arrays corresponding to lines in the CSV file

    • translations: string[]

      An array of strings in translation file format (with EN and translated values on alternate lines)

    Returns string[][]

    • An array of string arrays corresponding to lines in the CSV file with the values translated to the current game language

cacheLines

  • cacheLines(lines: string[][]): void
  • Stores the contents of a CSV file in the TextCache's internal cache


    Parameters

    • lines: string[][]

      An array of string arrays corresponding to lines in the CSV file

    Returns void

    • Nothing

fetchCsv

  • fetchCsv(): Promise<string[][]>
  • Fetches and parses the CSV file for this TextCache


    Returns Promise<string[][]>

    • A promise resolving to an array of string arrays, corresponding to lines of CSV values in the CSV file.

fileName

  • fileName(): string
  • Return the basename of the cached file


    Returns string

get

  • get(key: string): string
  • Looks up a string from this TextCache. If the cache contains a value for the provided key and a translation is available, the return value will be automatically translated. Otherwise the EN string will be used. If the cache does not contain a value for the requested key, the key will be returned.


    Parameters

    • key: string

      The text key to lookup

    Returns string

    • The text value corresponding to the provided key, translated into the current language, if available

log

  • log(msg: any): void
  • Parameters

    • msg: any

    Returns void

onRebuild

  • onRebuild(callback: (cache?: TextCache) => void, immediate?: boolean): Function
  • Adds a callback function as a rebuild listener. Rebuild listeners will be called whenever the cache has completed a rebuild (either after initial construction, or after a language change).


    Parameters

    • callback: (cache?: TextCache) => void

      The callback to register

      • optionalimmediate: boolean = true

        Whether or not the callback should be called on registration

      Returns Function

      • A callback function which can be used to unsubscribe the added listener

    translate

    • translate(lines: string[][]): Promise<string[][]>
    • Translates the contents of a CSV file into the current game language


      Parameters

      • lines: string[][]

        An array of string arrays corresponding to lines in the CSV file

      Returns Promise<string[][]>

      • A promise resolving to an array of string arrays corresponding to lines in the CSV file with the values translated to the current game language

    staticbuildAsync

    • Creates a new TextCache from the provided CSV file path asynchronously, promising its return after the cache has been build.


      Parameters

      • path: string

        The path to the CSV lookup file for this TextCache instance

      Returns Promise<TextCache>