| 1 | # Localization (translation) demo |
| 2 | |
| 3 | Demo how to use localization (translate your game into multiple languages) with Castle Game Engine. |
| 4 | |
| 5 | We use `CastleLocalizationGetText` unit, which in turn uses FPC `GetText` unit. See CGE [manual about text and localization](https://castle-engine.io/manual_text.php). |
| 6 | |
| 7 |  |
| 8 | |
| 9 | ## Creating translations |
| 10 | |
| 11 | 1. To translate strings used in Pascal code: |
| 12 | |
| 13 | 1. Place strings in `resourcestring`. You can use English text in code, or you can use internal translation identifiers (never to be seen by normal users) -- both approaches are possible. |
| 14 | |
| 15 | 2. Create initial `game.pot` file (that describes the possible strings to translate). |
| 16 | |
| 17 | _Note that this step is optional_. If you want, you can work without any `game.pot` file, and just create translations by manually creating files like `game.pl.po` for each language. The syntax of PO files is trivial, see `po_files` subdirectory here. |
| 18 | |
| 19 | The `game.pot` can serve a basis for translations. You can create it using strings from the source code: |
| 20 | |
| 21 | * Compile the game (`castle-engine compile`). |
| 22 | |
| 23 | * Use `rstconv` (distributed with FPC) like this: `rstconv -i castle-engine-output/compilation/x86_64-linux/game.rsj -o po_files/game.pot` |
| 24 | |
| 25 | * Note that FPC will create one `xxx.rsj` file for each unit. But this should not limit you. It's normal to put all the strings from the *complete* application into a single `xxx.pot` file. In general, the format of the `.pot` and .po` files (they are the same) is trivial, they are simple text files that can be concatenated together etc. |
| 26 | |
| 27 | 2. To translate user interface: |
| 28 | |
| 29 | 1. Design it using the CGE editor, and write as `xxx.castle-user-interface` files. (More complex scenarios can also be handled using CastleLocalizationGetText utilities, like TranslateDesign.) |
| 30 | |
| 31 | 2. Generate the `user_interface.pot` file using `GenerateGetTextPo`. See the trivial utility inside `po_files/generator/po_generator.lpr` in this example. |
| 32 | |
| 33 | 3. Then translate the PO files. |
| 34 | |
| 35 | * For each `xxxx.pot`, you create a file like `xxxx.ll.po` inserting the 2-latter character code indicating a language. |
| 36 | |
| 37 | You can create the `.po` file from `.pot` just by copying it -- it's the same file format, the `.pot` (PO Template) extension is just a way to indicate _"this is a basis for translation"_. |
| 38 | |
| 39 | Or you can create the `.po` by calling `msginit --locale=pl --input=game.pot --no-translator --output-file=game.pl.po`. This creates `game.pl.po`, with the initial translated strings having contents from `game.pot`. This makes sense if `game.pot` contains English text, and it's a good starting point for a new translation. |
| 40 | |
| 41 | * E.g. you copy `game.pot` to `game.pl.po` to translate to Polish the resourcestrings, and you copy `user_interface.pot` to `user_interface.pl.po` to translate to Polish the user interface designed using the CGE Editor. `pl` stands for a Polish translation, `de` for German, `en` for English etc. |
| 42 | |
| 43 | * Edit the `game.pl.po` using a normal text editor. Or use a specialized editor like https://poedit.net/ |
| 44 | |
| 45 | * Note: You cannot force something to be empty by translating it to an empty string. `GetText` treats `msgstr ""` as indicating "not translated", and the `msgfmt` will not even place this mapping in MO file. In turn, the text will be left in the original (English) version. See https://github.com/grosser/gettext_i18n_rails/issues/81 and links from it to other similar issues. To make something empty, for now it's simplest to translate it to a space character. |
| 46 | |
| 47 | 4. Generate .mo file: `msgfmt po_files/game.pl.po --output-file=data/locale/game.pl.mo`. We have a trivial script here `update_translations.sh` doing that. You need to rerun it after every modification to `po_files`. |
| 48 | |
| 49 | ## Using translations from Pascal code |
| 50 | |
| 51 | * Call in Pascal `CastleTranslateResourceStrings('castle-data:/locale/game.pl.mo');` to use the Polish translation for _resource strings_ (Pascal String constants declared inside `resourcestring`). This simply updates all `resourcestring` contents to the Polish versions. |
| 52 | |
| 53 | * Call `TranslateAllDesigns('castle-data:/locale/user_interface.pl.mo');` to translate all user interface to Polish. |
| 54 | |
| 55 | * If needed, you can also translate custom things, using more PO files. |
| 56 | |
| 57 | Load them using `LoadGetTextMo` and use the `Translate` method, see FPC docs of [TMOFile](https://www.freepascal.org/docs-html/fcl/gettext/tmofile.html) and the [TMOFile.Translate](https://www.freepascal.org/docs-html/fcl/gettext/tmofile.translate.html) method. Example: |
| 58 | |
| 59 | ```delphi |
| 60 | var |
| 61 | Language: String; |
| 62 | Translations: TCastleMOFile; |
| 63 | begin |
| 64 | Language := 'pl'; |
| 65 | Translations := LoadGetTextMo('castle-data:/locale/game.' + Language + '.mo'); |
| 66 | Writeln('Translated: ', Translations.Translate('message identifier to translate')); |
| 67 | FreeAndNil(Translations); |
| 68 | end. |
| 69 | ``` |
| 70 | |
| 71 | ## Fonts |
| 72 | |
| 73 | Note that we also adjust font in this application. |
| 74 | We load a font with additional German, Polish, Russian and Ukrainian characters. |
| 75 | See the [manual about text and fonts](https://castle-engine.io/manual_text.php). |
| 76 | |
| 77 | This is no longer strictly necessary with latest engine [that includes most common Unicode characters in the default font](https://wp.me/p9IgYW-1bL). |
| 78 | |
| 79 | ## Lazarus references |
| 80 | |
| 81 | While we don't use Lazarus LCL code in CGE, but this mechanism is consistent with how Lazarus application can be localized. So a lot of Lazarus documentation apply also to us: |
| 82 | |
| 83 | * http://wiki.lazarus.freepascal.org/Step-by-step_instructions_for_creating_multi-language_applications |
| 84 | * http://wiki.lazarus.freepascal.org/Translations_/_i18n_/_localizations_for_programs |
| 85 | * http://wiki.lazarus.freepascal.org/Everything_else_about_translations |
| 86 | |
| 87 | See also resources about gettext: |
| 88 | |
| 89 | * https://en.wikipedia.org/wiki/Gettext |
| 90 | |
| 91 | All the tools and editors for gettext files should work fine. |
| 92 | There are even online services that take a ready PO file for translating. |
| 93 | |
| 94 | ## Building |
| 95 | |
| 96 | Using [Castle Game Engine](https://castle-engine.io/). |
| 97 | |
| 98 | Compile by: |
| 99 | |
| 100 | - [CGE editor](https://castle-engine.io/editor). Just use menu items _"Compile"_ or _"Compile And Run"_. |
| 101 | |
| 102 | - Or use [CGE command-line build tool](https://castle-engine.io/build_tool). Run `castle-engine compile` in this directory. |
| 103 | |
| 104 | - Or use [Lazarus](https://www.lazarus-ide.org/). Open in Lazarus `localization_test.lpi` file and compile / run from Lazarus. Make sure to first register [CGE Lazarus packages](https://castle-engine.io/lazarus). |