- Localization using gettext
- Advantages
- Disadvantages
- Caveats
- Installing gettext tools
- Creating the PO template (POT) manually
- Creating the PO template (POT) using pybabel
- Creating a messages file from a PO template
- Loading a messages file in Godot
- Updating message files to follow the PO template
- Checking the validity of a PO file or template
- Using binary MO files (useful for large projects only)
Localization using gettext
Importing translations in CSV format, Godot also supports loading translation files written in the GNU gettext format (text-based .po and compiled .mo since Godot 3.5).
Note
A Quick Gettext Tutorial. It’s written with C projects in mind, but much of the advice also applies to Godot (with the exception of xgettext).
Advantages
- Poedit.
- Transifex and Weblate, which makes it easier for people to collaborate to localization.
- Compared to CSV, gettext works better with version control systems like Git, as each locale has its own messages file.
- Multiline strings are more convenient to edit in gettext files compared to CSV files.
Disadvantages
- gettext is a more complex format than CSV and can be harder to grasp for people new to software localization.
.po), translators can test their work without having to install gettext tools.Caveats
-Installing gettext tools
The command line gettext tools are required to perform maintenance operations, such as updating message files. Therefore, it’s strongly recommended to install them.- Windows: Download an installer from this page. Any architecture and binary type (shared or static) works; if in doubt, choose the 64-bit static installer.
- macOS: Install gettext either using Homebrew with the
brew install gettextcommand, or using MacPorts with thesudo port install gettextcommand. - Linux: On most distributions, install the
gettextpackage from your distribution’s package manager.Creating the PO template (POT) manually
xgettext, so the.potfile must be created manually. This file can be placed anywhere in the project directory, but it’s recommended to keep it in a subdirectory, as each locale will be defined in its own file.messages.potwith the following contents:# Don't remove the two lines below, they're required for gettext to work correctly.msgid ""msgstr ""msgid "Hello world!"msgstr ""
msgidandmsgstrpairs.msgidis the source string (usually in English),msgstrwill be the translated string.msgstrvalue in PO template files (.pot) should always be empty. Localization will be done in the generated.pofiles instead.Creating the PO template (POT) using pybabel
The Python tool pybabel has support for Godot and can be used to automatically create and update the POT file from your scene files and scripts.babelandbabel-godot, for example using pip:pip3 install babel babel-godot
babelrc) which will indicate which files pybabel needs to process (note that we process GDScript as Python, which is generally sufficient):
You can then run pybabel like so:[python: **.gd]encoding = utf-8[godot_scene: **.tscn]encoding = utf-8
pybabel extract -F babelrc -k text -k LineEdit/placeholder_text -k tr -o godot-l10n.pot .
-koption to specify what needs to be extracted. In this case, arguments to tr() will be translated, as well as properties named “text” (commonly used by Control nodes) and LineEdit’s “placeholder_text” property.Creating a messages file from a PO template
msginitcommand is used to turn a PO template into a messages file. For instance, to create a French localization file, use the following command while in thelocaledirectory:msginit --no-translator --input=messages.pot --locale=fr
fr.poin the same directory as the PO template. Alternatively, you can do that graphically using Poedit, or by uploading the POT file to your web platform of choice.Loading a messages file in Godot
Project Settings, then go to the Localization tab. In Translations, click Add… then choose the.poor.mofile in the file dialog. The locale will be inferred from the"Language: <code>\n"property in the messages file. Note Internationalizing games for more information on importing and testing translations in Godot.Updating message files to follow the PO template
msgmergetool:# The order matters: specify the message file *then* the PO template!msgmerge --update --backup=none fr.po messages.pot
fr.po~in this example), remove the--backup=noneargument. Notemsgmerge, strings which were modified in the source language will have a “fuzzy” comment added before them in the.pofile. This comment denotes that the translation should be updated to match the new source string, as the translation will most likely be inaccurate until it’s updated. not be read by Godot until the translation is updated and the “fuzzy” comment is removed.Checking the validity of a PO file or template
It is possible to check whether a gettext file’s syntax is valid by running the command below:msgfmt fr.po --check
msgfmtwon’t output anything.Using binary MO files (useful for large projects only)
For large projects with several thousands of strings to translate or more, it can be worth it to use binary (compiled) MO message files instead of text-based PO files. Binary MO files are smaller and faster to read than the equivalent PO files. You can generate a MO file with the command below:msgfmt fr.po --no-hash -o fr.mo
fr.mofile besides the PO file. This MO file can then be loaded in Godot as described below. The original PO file should be kept in version control so you can update your translation in the future. In case you lose the original PO file and wish to decompile a MO file into a text-based PO file, you can do so with:
The decompiled file will not include comments or fuzzy strings, as these are never compiled in the MO file in the first place.msgunfmt fr.mo > fr.po
