| 1 | = Language = |
| 2 | |
| 3 | == Description == |
| 4 | |
| 5 | The [wiki:Language] is a [wiki:singleton] that handles strings in different languages. It replaces hardcoded strings by labeled strings, where the coder only uses the label and the [wiki:Language] searches for the localised string in the [wiki:ConfigValueContainer 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 [wiki:ConfigValueContainer config-file]. |
| 6 | |
| 7 | 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 [wiki:Language#Adding_a_new_language] for more informations about this. |
| 8 | |
| 9 | == Functions == |
| 10 | |
| 11 | * '''getLanguage()''': Returns a reference to the only existing instance of the [wiki:Language] class. |
| 12 | * '''addEntry('''''label''''', '''''fallback string''''')''': Adds a new entry to the [wiki:Language] by defining a unique label and a fallback string that gets used in case there is no localisation. |
| 13 | * '''getLocalisation('''''label''''')''': Returns the localisation of the given label. |
| 14 | |
| 15 | == Example == |
| 16 | |
| 17 | Piece of code: |
| 18 | {{{ |
| 19 | #!cpp |
| 20 | // Displays the users age |
| 21 | int age = 20; |
| 22 | Language::getLanguage().addEntry("user_age", "Age"); |
| 23 | std::cout << Language::getLanguage().getLocalisation("user_age") << ": " << age << std::endl; |
| 24 | }}} |
| 25 | |
| 26 | Extract of translation_default.lang: |
| 27 | {{{ |
| 28 | user_age=Age |
| 29 | }}} |
| 30 | |
| 31 | Extract of translation_german.lang: |
| 32 | {{{ |
| 33 | user_age=Alter |
| 34 | }}} |
| 35 | |
| 36 | Extract of orxonox.ini: |
| 37 | {{{ |
| 38 | language_="german" |
| 39 | }}} |
| 40 | |
| 41 | Resulting output: |
| 42 | {{{ |
| 43 | Alter: 20 |
| 44 | }}} |
| 45 | |
| 46 | == Adding a new language == |
| 47 | |
| 48 | 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. |
| 49 | |
| 50 | An entry has the following form: |
| 51 | {{{ |
| 52 | label=fallback string |
| 53 | }}} |
| 54 | |
| 55 | Change it to: |
| 56 | {{{ |
| 57 | label=translated string |
| 58 | }}} |
| 59 | |
| 60 | Now you can change the config-value ''language_'' to "''xxxxx''" in orxonox.ini. The translated entries should now be used in the game. |