localization_using_gettext.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 4.0).
  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. Installing gettext tools
  30. ------------------------
  31. The command line gettext tools are required to perform maintenance operations,
  32. such as updating message files. Therefore, it's strongly recommended to
  33. install them.
  34. - **Windows:** Download an installer from
  35. `this page <https://mlocati.github.io/articles/gettext-iconv-windows.html>`_.
  36. Any architecture and binary type (shared or static) works;
  37. if in doubt, choose the 64-bit static installer.
  38. - **macOS:** Install gettext either using `Homebrew <https://brew.sh/>`_
  39. with the ``brew install gettext`` command, or using
  40. `MacPorts <https://www.macports.org/>`_ with the
  41. ``sudo port install gettext`` command.
  42. - **Linux:** On most distributions, install the ``gettext`` package from
  43. your distribution's package manager.
  44. Creating the PO template
  45. ------------------------
  46. Automatic generation using the editor
  47. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  48. Since Godot 4.0, the editor can generate a PO template automatically from
  49. specified scene and GDScript files. This POT generation also supports translation
  50. contexts and pluralization if used in a script, with the optional second
  51. argument of ``tr()`` and the ``tr_n()`` method.
  52. Open the Project Settings' **Localization > POT Generation** tab, then use the
  53. **Add…** button to specify the path to your project's scenes and scripts that
  54. contain localizable strings:
  55. .. figure:: img/localization_using_gettext_pot_generation.webp
  56. :align: center
  57. :alt: Creating a PO template in the Localization > POT Generation tab of the Project Settings
  58. Creating a PO template in the **Localization > POT Generation** tab of the Project Settings
  59. After adding at least one scene or script, click **Generate POT** in the
  60. top-right corner, then specify the path to the output file. This file can be
  61. placed anywhere in the project directory, but it's recommended to keep it in a
  62. subdirectory such as ``locale``, as each locale will be defined in its own file.
  63. See :ref:`below <doc_localization_using_gettext_gdscript>` for how to add comments for translators
  64. or exclude some strings from being added to the PO template for GDScript files.
  65. You can then move over to
  66. :ref:`creating a messages file from a PO template <doc_localization_using_gettext_messages_file>`.
  67. .. note::
  68. Remember to regenerate the PO template after making any changes to
  69. localizable strings, or after adding new scenes or scripts. Otherwise, newly
  70. added strings will not be localizable and translators won't be able to
  71. update translations for outdated strings.
  72. Manual creation
  73. ^^^^^^^^^^^^^^^
  74. If the automatic generation approach doesn't work out for your needs, you can
  75. create a PO template by hand in a text editor. This file can be placed anywhere
  76. in the project directory, but it's recommended to keep it in a subdirectory, as
  77. each locale will be defined in its own file.
  78. Create a directory named ``locale`` in the project directory. In this directory,
  79. save a file named ``messages.pot`` with the following contents:
  80. ::
  81. # Don't remove the two lines below, they're required for gettext to work correctly.
  82. msgid ""
  83. msgstr ""
  84. # Example of a regular string.
  85. msgid "Hello world!"
  86. msgstr ""
  87. # Example of a string with pluralization.
  88. msgid "There is %d apple."
  89. msgid_plural "There are %d apples."
  90. msgstr[0] ""
  91. msgstr[1] ""
  92. # Example of a string with a translation context.
  93. msgctxt "Actions"
  94. msgid "Close"
  95. msgstr ""
  96. Messages in gettext are made of ``msgid`` and ``msgstr`` pairs.
  97. ``msgid`` is the source string (usually in English), ``msgstr`` will be
  98. the translated string.
  99. .. warning::
  100. The ``msgstr`` value in PO template files (``.pot``) should **always** be
  101. empty. Localization will be done in the generated ``.po`` files instead.
  102. .. _doc_localization_using_gettext_messages_file:
  103. Creating a messages file from a PO template
  104. -------------------------------------------
  105. The ``msginit`` command is used to turn a PO template into a messages file.
  106. For instance, to create a French localization file, use the following command
  107. while in the ``locale`` directory:
  108. .. code-block:: shell
  109. msginit --no-translator --input=messages.pot --locale=fr
  110. The command above will create a file named ``fr.po`` in the same directory
  111. as the PO template.
  112. Alternatively, you can do that graphically using Poedit, or by uploading the
  113. POT file to your web platform of choice.
  114. Loading a messages file in Godot
  115. --------------------------------
  116. To register a messages file as a translation in a project, open the
  117. **Project Settings**, then go to the **Localization** tab.
  118. In **Translations**, click **Add…** then choose the ``.po`` or ``.mo`` file
  119. in the file dialog. The locale will be inferred from the
  120. ``"Language: <code>\n"`` property in the messages file.
  121. .. note:: See :ref:`doc_internationalizing_games` for more information on
  122. importing and testing translations in Godot.
  123. Updating message files to follow the PO template
  124. ------------------------------------------------
  125. After updating the PO template, you will have to update message files so
  126. that they contain new strings, while removing strings that are no longer
  127. present in the PO template. This can be done automatically using the
  128. ``msgmerge`` tool:
  129. .. code-block:: shell
  130. # The order matters: specify the message file *then* the PO template!
  131. msgmerge --update --backup=none fr.po messages.pot
  132. If you want to keep a backup of the original message file (which would be
  133. saved as ``fr.po~`` in this example), remove the ``--backup=none`` argument.
  134. .. note::
  135. After running ``msgmerge``, strings which were modified in the source language
  136. will have a "fuzzy" comment added before them in the ``.po`` file. This comment
  137. denotes that the translation should be updated to match the new source string,
  138. as the translation will most likely be inaccurate until it's updated.
  139. Strings with "fuzzy" comments will **not** be read by Godot until the
  140. translation is updated and the "fuzzy" comment is removed.
  141. Checking the validity of a PO file or template
  142. ----------------------------------------------
  143. It is possible to check whether a gettext file's syntax is valid by running
  144. the command below:
  145. .. code-block:: shell
  146. msgfmt fr.po --check
  147. If there are syntax errors or warnings, they will be displayed in the console.
  148. Otherwise, ``msgfmt`` won't output anything.
  149. Using binary MO files (useful for large projects only)
  150. ------------------------------------------------------
  151. For large projects with several thousands of strings to translate or more,
  152. it can be worth it to use binary (compiled) MO message files instead of text-based
  153. PO files. Binary MO files are smaller and faster to read than the equivalent
  154. PO files.
  155. You can generate an MO file with the command below:
  156. .. code-block:: shell
  157. msgfmt fr.po --no-hash -o fr.mo
  158. If the PO file is valid, this command will create a ``fr.mo`` file besides
  159. the PO file. This MO file can then be loaded in Godot as described above.
  160. The original PO file should be kept in version control so you can update
  161. your translation in the future. In case you lose the original PO file and
  162. wish to decompile an MO file into a text-based PO file, you can do so with:
  163. .. code-block:: shell
  164. msgunfmt fr.mo > fr.po
  165. The decompiled file will not include comments or fuzzy strings, as these are
  166. never compiled in the MO file in the first place.
  167. .. _doc_localization_using_gettext_gdscript:
  168. Extracting localizable strings from GDScript files
  169. --------------------------------------------------
  170. The built-in `editor plugin <https://github.com/godotengine/godot/blob/master/modules/gdscript/editor/gdscript_translation_parser_plugin.h>`_
  171. recognizes a variety of patterns in source code to extract localizable strings
  172. from GDScript files, including but not limited to the following:
  173. - ``tr()``, ``tr_n()``, ``atr()``, and ``atr_n()`` calls;
  174. - assigning properties ``text``, ``placeholder_text``, and ``tooltip_text``;
  175. - ``add_tab()``, ``add_item()``, ``set_tab_title()``, and other calls;
  176. - ``FileDialog`` filters like ``"*.png ; PNG Images"``.
  177. .. note::
  178. The argument or right operand must be a constant string, otherwise the plugin
  179. will not be able to evaluate the expression and will ignore it.
  180. If the plugin extracts unnecessary strings, you can ignore them with the ``NO_TRANSLATE`` comment.
  181. You can also provide additional information for translators using the ``TRANSLATORS:`` comment.
  182. These comments must be placed either on the same line as the recognized pattern or precede it.
  183. ::
  184. $CharacterName.text = "???" # NO_TRANSLATE
  185. # NO_TRANSLATE: Language name.
  186. $TabContainer.set_tab_title(0, "Python")
  187. item.text = "Tool" # TRANSLATORS: Up to 10 characters.
  188. # TRANSLATORS: This is a reference to Lewis Carroll's poem "Jabberwocky",
  189. # make sure to keep this as it is important to the plot.
  190. say(tr("He took his vorpal sword in hand. The end?"))