Skip to content

Commit 4fdeac0

Browse files
authored
Merge pull request #3935 from SkyLucilfer/POtuto
Update i18n gettext tutorial to include POT generation, plurals and context support
2 parents db5cb61 + db02411 commit 4fdeac0

File tree

1 file changed

+64
-8
lines changed

1 file changed

+64
-8
lines changed

tutorials/i18n/localization_using_gettext.rst

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _doc_localization_using_gettext:
22

3-
Localization using gettext
4-
==========================
3+
Localization using gettext (PO files)
4+
=====================================
55

66
In addition to importing translations in
77
:ref:`CSV format <doc_localization_using_spreadsheets>`, Godot also
@@ -13,27 +13,35 @@ supports loading translation files written in the GNU gettext format
1313
It's written with C projects in mind, but much of the advice
1414
also applies to Godot (with the exception of ``xgettext``).
1515

16+
For the complete documentation, see `GNU Gettext <https://www.gnu.org/software/gettext/manual/gettext.html>`_.
17+
1618
Advantages
1719
----------
1820

1921
- gettext is a standard format, which can be edited using any text editor
20-
or GUI editors such as `Poedit <https://poedit.net/>`_.
22+
or GUI editors such as `Poedit <https://poedit.net/>`_. This can be significant
23+
as it provides a lot of tools for translators, such as marking outdated
24+
strings, finding strings that haven't been translated etc.
25+
- gettext supports plurals and context.
2126
- gettext is supported by translation platforms such as
2227
`Transifex <https://www.transifex.com/>`_ and `Weblate <https://weblate.org/>`_,
2328
which makes it easier for people to collaborate to localization.
24-
- Compared to CSV, gettext works better with version control systems like Git,
29+
- Compared to CSV, gettext files work better with version control systems like Git,
2530
as each locale has its own messages file.
26-
- Multiline strings are more convenient to edit in gettext files compared
31+
- Multiline strings are more convenient to edit in gettext PO files compared
2732
to CSV files.
2833

2934
Disadvantages
3035
-------------
3136

32-
- gettext is a more complex format than CSV and can be harder to grasp for
37+
- gettext PO files have a more complex format than CSV and can be harder to grasp for
3338
people new to software localization.
3439
- People who maintain localization files will have to install gettext tools
3540
on their system. However, as Godot supports using text-based message files
3641
(``.po``), translators can test their work without having to install gettext tools.
42+
- gettext PO files usually use English as the base language. Translators will use
43+
this base language to translate to other languages. You could still user other
44+
languages as the base language, but this is not common.
3745

3846
Installing gettext tools
3947
------------------------
@@ -53,6 +61,9 @@ install them.
5361
- **Linux:** On most distributions, install the ``gettext`` package from
5462
your distribution's package manager.
5563

64+
For a GUI tool you can get Poedit from its `Official website <https://poedit.net/>`_.
65+
The basic version is open source and available under the MIT license.
66+
5667
Creating the PO template
5768
------------------------
5869

@@ -193,8 +204,10 @@ saved as ``fr.po~`` in this example), remove the ``--backup=none`` argument.
193204
Checking the validity of a PO file or template
194205
----------------------------------------------
195206

196-
It is possible to check whether a gettext file's syntax is valid by running
197-
the command below:
207+
It is possible to check whether a gettext file's syntax is valid.
208+
209+
If you open with Poeditor, it will display the appropriate warnings if there's some
210+
syntax errors. You can also verify by running the gettext command below:
198211

199212
.. code-block:: shell
200213
@@ -266,3 +279,46 @@ These comments must be placed either on the same line as the recognized pattern
266279
# TRANSLATORS: This is a reference to Lewis Carroll's poem "Jabberwocky",
267280
# make sure to keep this as it is important to the plot.
268281
say(tr("He took his vorpal sword in hand. The end?"))
282+
283+
Using context
284+
-------------
285+
286+
The ``context`` parameter can be used to differentiate the situation where a translation
287+
is used, or to differentiate polysemic words (words with multiple meanings).
288+
289+
For example:
290+
291+
::
292+
293+
tr("Start", "Main Menu")
294+
tr("End", "Main Menu")
295+
tr("Shop", "Main Menu")
296+
tr("Shop", "In Game")
297+
298+
Updating PO files
299+
-----------------
300+
301+
Some time or later, you'll add new content to our game, and there will be new strings that need to be translated. When this happens, you'll
302+
need to update the existing PO files to include the new strings.
303+
304+
First, generate a new POT file containing all the existing strings plus the newly added strings. After that, merge the existing
305+
PO files with the new POT file. There are two ways to do this:
306+
307+
- Use a gettext editor, and it should have an option to update a PO file from a POT file.
308+
309+
- Use the gettext ``msgmerge`` tool:
310+
311+
.. code-block:: shell
312+
313+
# The order matters: specify the message file *then* the PO template!
314+
msgmerge --update --backup=none fr.po messages.pot
315+
316+
If you want to keep a backup of the original message file (which would be saved as ``fr.po~`` in this example),
317+
remove the ``--backup=none`` argument.
318+
319+
POT generation custom plugin
320+
----------------------------
321+
322+
If you have any extra file format to deal with, you could write a custom plugin to parse and and extract the strings from the custom file.
323+
This custom plugin will extract the strings and write into the POT file when you hit **Generate POT**. To learn more about how to
324+
create the translation parser plugin, see :ref:`EditorTranslationParserPlugin <class_EditorTranslationParserPlugin>`.

0 commit comments

Comments
 (0)