Types/LocalisedString: Difference between revisions

From Official Factorio Wiki
Jump to navigation Jump to search
(Added second paragraph, an explanation of what the localised string means in the more general context.)
(remove inconsistently applied formatting of key, shorten "why" for locale string cause otherwise the whole mp/determinism thing would also need to be explained (may be done in the future, but not here). Display to players is already covered by examples)
Line 1: Line 1:
Localised strings are a way to support translation of in-game text. It is an array where the first element is the ''key'' and the remaining elements are parameters that will be substituted for placeholders in the template designated by the key.
Localised strings are a way to support translation of in-game text. They offer a language-independent code representation of the text that should be shown to players.


When presented to a player, what appears to ''each'' player is determined by their Language (as chosen by game setting). As Language is set per-player, and Factorio must reach the same results for every player, a localised string offers a player-independent '''code''' representation. The Factorio API Docs provide the means to expose a translation, during gameplay, to everyone. Stated a different way, any place a translation is visible is not available for code to access "as it appears to be", because it would result in different outcomes for different players. The localised string provides an abstract representation, preventing inconsistencies.
A localised string is an array where the first element is the key and the remaining elements are parameters that will be substituted for placeholders in the template designated by the key.


The ''key'' identifies the string template. For example, "gui-alert-tooltip.attack" (for the template "__1__ objects are being damaged"; see the file data/core/locale/en.cfg). In the data stage, this key cannot be longer than 200 characters.  
The key identifies the string template. For example, "gui-alert-tooltip.attack" (for the template "__1__ objects are being damaged"; see the file data/core/locale/en.cfg). In the data stage, this key cannot be longer than 200 characters.  


The template can contain placeholders such as __1__ or __2__. These will be replaced by the respective parameter in the LocalisedString. The parameters themselves can be other localised strings, which will be processed recursively in the same fashion. Localised strings cannot be recursed deeper than 20 levels and cannot have more than 20 parameters.
The template can contain placeholders such as __1__ or __2__. These will be replaced by the respective parameter in the LocalisedString. The parameters themselves can be other localised strings, which will be processed recursively in the same fashion. Localised strings cannot be recursed deeper than 20 levels and cannot have more than 20 parameters.
Line 12: Line 12:


== Examples ==
== Examples ==
In the English translation, this will print "No ammo"; in the Czech translation, it will print "Bez munice":
For a player using the English translation, this will print "No ammo". For a player using the Czech translation, it will print "Bez munice":
<syntaxhighlight lang="lua">game.player.print({"description.no-ammo"})</syntaxhighlight>
<syntaxhighlight lang="lua">game.player.print({"description.no-ammo"})</syntaxhighlight>
The description.no-ammo template contains no placeholders, so no further parameters are necessary.
The description.no-ammo template contains no placeholders, so no further parameters are necessary.

Revision as of 16:39, 18 January 2023

Localised strings are a way to support translation of in-game text. They offer a language-independent code representation of the text that should be shown to players.

A localised string is an array where the first element is the key and the remaining elements are parameters that will be substituted for placeholders in the template designated by the key.

The key identifies the string template. For example, "gui-alert-tooltip.attack" (for the template "__1__ objects are being damaged"; see the file data/core/locale/en.cfg). In the data stage, this key cannot be longer than 200 characters.

The template can contain placeholders such as __1__ or __2__. These will be replaced by the respective parameter in the LocalisedString. The parameters themselves can be other localised strings, which will be processed recursively in the same fashion. Localised strings cannot be recursed deeper than 20 levels and cannot have more than 20 parameters.

As a special case, when the key is just the empty string, all the parameters will be concatenated (after processing, if any are localised strings). If there is only one parameter, it will be used as is.

Furthermore, when an API function expects a localised string, it will also accept a regular string (i.e. not a table) which will not be translated, or a number or boolean which will be converted to its textual representation.

Examples

For a player using the English translation, this will print "No ammo". For a player using the Czech translation, it will print "Bez munice":

game.player.print({"description.no-ammo"})

The description.no-ammo template contains no placeholders, so no further parameters are necessary.

In the English translation, this will print "Durability: 5/9"; in the Japanese one, it will print "耐久度: 5/9":

game.player.print({"description.durability", 5, 9})

This will print "hello" in all translations:

game.player.print({"", "hello"})

This will print "Iron plate: 60" in the English translation and "Eisenplatte: 60" in the German translation.

game.print({"", {"item-name.iron-plate"}, ": ", 60})

See also