localization_using_gettext.rst 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. .. _doc_localization_using_gettext:
  2. Localization using gettext
  3. ==========================
  4. In addition to :ref:`doc_importing_translations` in CSV format, Godot
  5. also supports loading translation files written in the GNU gettext
  6. format (text-based ``.po`` and compiled ``.mo`` since Godot 3.5).
  7. .. note:: For an introduction to gettext, check out
  8. `A Quick Gettext Tutorial <https://www.labri.fr/perso/fleury/posts/programming/a-quick-gettext-tutorial.html>`_.
  9. It's written with C projects in mind, but much of the advice
  10. also applies to Godot (with the exception of ``xgettext``).
  11. Advantages
  12. ----------
  13. - gettext is a standard format, which can be edited using any text editor
  14. or GUI editors such as `Poedit <https://poedit.net/>`_.
  15. - gettext is supported by translation platforms such as
  16. `Transifex <https://www.transifex.com/>`_ and `Weblate <https://weblate.org/>`_,
  17. which makes it easier for people to collaborate to localization.
  18. - Compared to CSV, gettext works better with version control systems like Git,
  19. as each locale has its own messages file.
  20. - Multiline strings are more convenient to edit in gettext files compared
  21. to CSV files.
  22. Disadvantages
  23. -------------
  24. - gettext is a more complex format than CSV and can be harder to grasp for
  25. people new to software localization.
  26. - People who maintain localization files will have to install gettext tools
  27. on their system. However, as Godot supports using text-based message files
  28. (``.po``), translators can test their work without having to install gettext tools.
  29. Caveats
  30. -------
  31. - As Godot uses its own PO file parser behind the scenes
  32. (which is more limited than the reference GNU gettext implementation),
  33. some features such as pluralization aren't supported.
  34. Installing gettext tools
  35. ------------------------
  36. The command line gettext tools are required to perform maintenance operations,
  37. such as updating message files. Therefore, it's strongly recommended to
  38. install them.
  39. - **Windows:** Download an installer from
  40. `this page <https://mlocati.github.io/articles/gettext-iconv-windows.html>`_.
  41. Any architecture and binary type (shared or static) works;
  42. if in doubt, choose the 64-bit static installer.
  43. - **macOS:** Install gettext either using `Homebrew <https://brew.sh/>`_
  44. with the ``brew install gettext`` command, or using
  45. `MacPorts <https://www.macports.org/>`_ with the
  46. ``sudo port install gettext`` command.
  47. - **Linux:** On most distributions, install the ``gettext`` package from
  48. your distribution's package manager.
  49. Creating the PO template (POT) manually
  50. ---------------------------------------
  51. Godot currently doesn't support extracting source strings using ``xgettext``,
  52. so the ``.pot`` file must be created manually. This file can be placed anywhere
  53. in the project directory, but it's recommended to keep it in a subdirectory, as
  54. each locale will be defined in its own file.
  55. Create a directory named `locale` in the project directory. In this directory,
  56. save a file named ``messages.pot`` with the following contents:
  57. ::
  58. # Don't remove the two lines below, they're required for gettext to work correctly.
  59. msgid ""
  60. msgstr ""
  61. msgid "Hello world!"
  62. msgstr ""
  63. Messages in gettext are made of ``msgid`` and ``msgstr`` pairs.
  64. ``msgid`` is the source string (usually in English), ``msgstr`` will be
  65. the translated string.
  66. The ``msgstr`` value in PO template files (``.pot``) should **always** be empty.
  67. Localization will be done in the generated ``.po`` files instead.
  68. Creating the PO template (POT) using pybabel
  69. --------------------------------------------
  70. The Python tool pybabel has support for Godot and can be used to automatically
  71. create and update the POT file from your scene files and scripts.
  72. After installing ``babel`` and ``babel-godot``, for example using pip:
  73. .. code-block:: shell
  74. pip3 install babel babel-godot
  75. Write a mapping file (for example ``babelrc``) which will indicate which files
  76. pybabel needs to process (note that we process GDScript as Python, which is
  77. generally sufficient):
  78. .. code-block:: none
  79. [python: **.gd]
  80. encoding = utf-8
  81. [godot_scene: **.tscn]
  82. encoding = utf-8
  83. You can then run pybabel like so:
  84. .. code-block:: shell
  85. pybabel extract -F babelrc -k text -k LineEdit/placeholder_text -k tr -o godot-l10n.pot .
  86. Use the ``-k`` option to specify what needs to be extracted. In this case,
  87. arguments to :ref:`tr() <class_Object_method_tr>` will be translated, as well
  88. as properties named "text" (commonly used by Control nodes) and LineEdit's
  89. "placeholder_text" property.
  90. Creating a messages file from a PO template
  91. -------------------------------------------
  92. The ``msginit`` command is used to turn a PO template into a messages file.
  93. For instance, to create a French localization file, use the following command
  94. while in the ``locale`` directory:
  95. .. code-block:: shell
  96. msginit --no-translator --input=messages.pot --locale=fr
  97. The command above will create a file named ``fr.po`` in the same directory
  98. as the PO template.
  99. Alternatively, you can do that graphically using Poedit, or by uploading the
  100. POT file to your web platform of choice.
  101. Loading a messages file in Godot
  102. --------------------------------
  103. To register a messages file as a translation in a project, open the
  104. **Project Settings**, then go to the **Localization** tab.
  105. In **Translations**, click **Add…** then choose the ``.po`` or ``.mo`` file
  106. in the file dialog. The locale will be inferred from the
  107. ``"Language: <code>\n"`` property in the messages file.
  108. .. note:: See :ref:`doc_internationalizing_games` for more information on
  109. importing and testing translations in Godot.
  110. Updating message files to follow the PO template
  111. ------------------------------------------------
  112. After updating the PO template, you will have to update message files so
  113. that they contain new strings, while removing strings that are no longer
  114. present in the PO template. This can be done automatically using the
  115. ``msgmerge`` tool:
  116. .. code-block:: shell
  117. # The order matters: specify the message file *then* the PO template!
  118. msgmerge --update --backup=none fr.po messages.pot
  119. If you want to keep a backup of the original message file (which would be
  120. saved as ``fr.po~`` in this example), remove the ``--backup=none`` argument.
  121. .. note::
  122. After running ``msgmerge``, strings which were modified in the source language
  123. will have a "fuzzy" comment added before them in the ``.po`` file. This comment
  124. denotes that the translation should be updated to match the new source string,
  125. as the translation will most likely be inaccurate until it's updated.
  126. Strings with "fuzzy" comments will **not** be read by Godot until the
  127. translation is updated and the "fuzzy" comment is removed.
  128. Checking the validity of a PO file or template
  129. ----------------------------------------------
  130. It is possible to check whether a gettext file's syntax is valid by running
  131. the command below:
  132. .. code-block:: shell
  133. msgfmt fr.po --check
  134. If there are syntax errors or warnings, they will be displayed in the console.
  135. Otherwise, ``msgfmt`` won't output anything.
  136. Using binary MO files (useful for large projects only)
  137. ------------------------------------------------------
  138. For large projects with several thousands of strings to translate or more,
  139. it can be worth it to use binary (compiled) MO message files instead of text-based
  140. PO files. Binary MO files are smaller and faster to read than the equivalent
  141. PO files.
  142. You can generate a MO file with the command below:
  143. .. code-block:: shell
  144. msgfmt fr.po --no-hash -o fr.mo
  145. If the PO file is valid, this command will create a ``fr.mo`` file besides
  146. the PO file. This MO file can then be loaded in Godot as described below.
  147. The original PO file should be kept in version control so you can update
  148. your translation in the future. In case you lose the original PO file and
  149. wish to decompile a MO file into a text-based PO file, you can do so with:
  150. .. code-block:: shell
  151. msgunfmt fr.mo > fr.po
  152. The decompiled file will not include comments or fuzzy strings, as these are
  153. never compiled in the MO file in the first place.