Language is a singleton that handles strings in different languages. It replaces hardcoded strings by labeled strings, where the coder only uses the label and the Language searches for the localised string in the configured language-file. Every string has a fallback-value that is used if there is no localised version or no language was configured. The language can be configured by changing the language_ entry in the config-file.
A new language-entry can be added with the AddLanguageEntry(label, fallbackstring) macro and the localisation can be retrieved with GetLocalisation(label).
All labels and the corresponding fallback strings are written to translation_default.lang. If the fallback-string gets changed in the code, translation_default.lang changes too. If a new label gets added in the code, it gets added to translation_default.lang. You can't change labels or fallback-strings in this file, it will be set back to the default. You can use translation_default.lang as a sample for a new language file. See the chapter at the end for more information about this.
- AddLanguageEntry(label, fallbackstring): Adds a new entry to the Language by defining a unique label and a fallback string that gets used in case there is no localisation.
- GetLocalisation(label): Returns the localisation of the given label.
- getLanguage(): Returns a reference to the only existing instance of the Language class.
- addEntry(label, fallbackstring): Adds a new entry to the Language by defining a unique label and a fallback string that gets used in case there is no localisation.
- getLocalisation(label): Returns the localisation of the given label.
Piece of code:
// Displays the users age int age = 20; AddLanguageEntry("user_age", "Age"); std::cout << GetLocalisation("user_age") << ": " << age << std::endl;
Extract of translation_default.lang:
Extract of translation_german.lang:
Extract of orxonox.ini:
Adding a new language
Simply copy translation_default.lang and rename it to translation_xxxxx.lang where xxxxx is the name of your new language. Then translate all entries in the file.
An entry has the following form:
Change it to:
Now you can change the config-value language_ to "xxxxx" in orxonox.ini. The translated entries should now be used in the game.
Attention: Language entries are only added to the file if they are at least once executed. Otherwise you wont find them in the file. If you need a complete translation, you have to search the code for all entries. If you know a better solution, just tell me.