Skip to content

Instantly share code, notes, and snippets.

@Artoria2e5
Last active August 29, 2015 14:26
Show Gist options
  • Save Artoria2e5/023968769a3e49b7bfd1 to your computer and use it in GitHub Desktop.
Save Artoria2e5/023968769a3e49b7bfd1 to your computer and use it in GitHub Desktop.
Don't Starve (Together) Translator internals and mod API

Translator

The Translator class, located in data/scripts/translator.lua, provides a Gettext-based translator system. It also provides a variable LanguageTranslator of this type.

The functions, unless specicially stated, are class functions.

Don't Starve uses a custom and simplified Gettext parser and thus may not work as expected with gettext editors like POedit and Emacs. It only escapes \n, \r and ". Not even \ is escaped, causing problems when you want a literal \n (luckily the need for it would be quite rare unless someone is writing a programming tutorials mod or so.)

The one-liner ksh/bash command tr '\a\b\f\t\v' $'\a\b\f\t\v' < foo.po > foo_dst.po should be able to translate common escape characters produced by the editor. For other escapes, try asking your editor to store them in raw UTF-8 literals.

Constructor

The class constructer initializes a class Translator, with these properties:

  • languages: {}, {}, k-v of lang to k-v of msgid to msgstr. msgid and msgstr are still escaped forms.
  • defaultlang: nil, string.
  • dbfile: nil, io.open("debugfile.txt", "w") file descripter.

LoadPOFile(fname, lang)

This function loads a gettext po file of the given language. Note that it parses the gettext file by itself. It will finally set the defaultlang to the currently supplied lang, which should be taken special care of.

GetTranslatedString(strid, lang)

This function returns ConvertEscapeCharactersToRaw(self.languages[lang][strid]). If nothing is found in any step of it, it returns nil.

ConvertEscapeCharactersToString(str)

This functions uses naive gsubs to convert \\n, \\r, \\" to \n, \r and " respectively.

ConvertEscapeCharactersToRaw(str)

This function does the reverse, and is naive too.

Other functions defined in this file

TranslateStringTable(tbl)

This function is called by strings.lua and translates the STRINGS table to the supplied tbl, using the local function DoTranslateStringTable("STRINGS", tbl).

local DoTranslateStringTable(base, tbl)

This function takes the base as the top node, and recurses to each of its node to perform translation. When the node is not a table, it tries to get the translated string and add its value and position on tree to tbl if it's not nil or "".

ModUtils

This content is meant to be the complement of http://dontstarveapi.com/utility/modutil/, with Translator content added.

I am just too lazy to figure out where it should be put.

LoadPOFile(path, lang)

Loads a PO file into the LanguageTranslator which came from require "translator". Everything else should be done automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment