Simutrans-Squirrel-API
A script showing translatable texts.

Let us inspect another scenario script file. We will have a look into the pak64 Pharmacy-max scenario. The objective is to get the pharmacy selling as much medicine as possible.

We will learn how to make the translation of the script's output possible.

We do not care about the meta information, and jump right into the get_rule_text method.
function get_rule_text(pl)
{
return ttext("You can build everything you like. The sky (and your bank account) is the limit.")
}
This function does not return a plain string, it sends the string into an instance of ttext. When the simutrans program now fetches the text, it will magically translate it. This magic works only if there are translation files present.

These files must be placed in a directory with the same name as the scenario, in our case at pharmacy-max/ The translation files itself are plain text files with the naming convention iso.tab, where iso refers to the ISO abbreviation of the language (en, de, cz etc).

These text files have a simple line-oriented structure. Lets look into de.tab:

You can build everything you like. The sky (and your bank account) is the limit.
Alles ist erlaubt. Der Himmel (und das Bankkonto) ist die Grenze.

The first line is the string as it appears in the script, it will be mapped to the string in the following line. A German user thus will read 'Alles ist erlaubt. Der Himmel (und das Bankkonto) ist die Grenze.'.

Similarly, the methods get_info_text and get_goal_text are implemented. Some interesting stuff however happens in get_result_text :

function get_result_text(pl)
{
local con = get_medicine_consumption()
local text = ttext("The pharmacy sold {med} units of medicine per month.")
Do you see the strange {med} string in the text? Here, you can see variable substitution in action:
text.med = con
That means the text now gets to know the precise value of {med}.

When this text is transferred to simutrans (or when text.to_string() is called) the following happens:

  1. First the translation of the string is searched, which can contain this {med} as well.
  2. Then the occurrences of {med} are replaced by the concrete number. The user will then hear 'The pharmacy sold 11 units of medicine per month.'.

Now a second string is created, which features some html-like tags.

local text2 = ""
if ( con >=120 )
text2 = ttext("<it>Congratulation!</it><br> <br> You won the scenario!")
else
text2 = ttext("Still too few units.")
return text.tostring() + "<br> <br>" + text2.tostring()
}
Both of the strings are translated by means of calls to ttext::to_string and the result is returned.

That's it. The remaining parts of this script are plain routine.