localization_using_gettext.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. .. _doc_localization_using_gettext:
  2. Localization using gettext (PO files)
  3. =====================================
  4. In addition to importing translations in
  5. :ref:`CSV format <doc_localization_using_spreadsheets>`, Godot also
  6. supports loading translation files written in the GNU gettext format
  7. (text-based ``.po`` and compiled ``.mo`` since Godot 4.0).
  8. .. note:: For an introduction to gettext, check out
  9. `A Quick Gettext Tutorial <https://www.labri.fr/perso/fleury/posts/programming/a-quick-gettext-tutorial.html>`_.
  10. It's written with C projects in mind, but much of the advice
  11. also applies to Godot (with the exception of ``xgettext``).
  12. For the complete documentation, see `GNU Gettext <https://www.gnu.org/software/gettext/manual/gettext.html>`_.
  13. Advantages
  14. ----------
  15. - gettext is a standard format, which can be edited using any text editor
  16. or GUI editors such as `Poedit <https://poedit.net/>`_. This can be significant
  17. as it provides a lot of tools for translators, such as marking outdated
  18. strings, finding strings that haven't been translated etc.
  19. - gettext supports plurals and context.
  20. - gettext is supported by translation platforms such as
  21. `Transifex <https://www.transifex.com/>`_ and `Weblate <https://weblate.org/>`_,
  22. which makes it easier for people to collaborate to localization.
  23. - Compared to CSV, gettext files work better with version control systems like Git,
  24. as each locale has its own messages file.
  25. - Multiline strings are more convenient to edit in gettext PO files compared
  26. to CSV files.
  27. Disadvantages
  28. -------------
  29. - gettext PO files have a more complex format than CSV and can be harder to grasp for
  30. people new to software localization.
  31. - People who maintain localization files will have to install gettext tools
  32. on their system. However, as Godot supports using text-based message files
  33. (``.po``), translators can test their work without having to install gettext tools.
  34. - gettext PO files usually use English as the base language. Translators will use
  35. this base language to translate to other languages. You could still user other
  36. languages as the base language, but this is not common.
  37. Installing gettext tools
  38. ------------------------
  39. The command line gettext tools are required to perform maintenance operations,
  40. such as updating message files. Therefore, it's strongly recommended to
  41. install them.
  42. - **Windows:** Download an installer from
  43. `this page <https://mlocati.github.io/articles/gettext-iconv-windows.html>`_.
  44. Any architecture and binary type (shared or static) works;
  45. if in doubt, choose the 64-bit static installer.
  46. - **macOS:** Install gettext either using `Homebrew <https://brew.sh/>`_
  47. with the ``brew install gettext`` command, or using
  48. `MacPorts <https://www.macports.org/>`_ with the
  49. ``sudo port install gettext`` command.
  50. - **Linux:** On most distributions, install the ``gettext`` package from
  51. your distribution's package manager.
  52. For a GUI tool you can get Poedit from its `Official website <https://poedit.net/>`_.
  53. The basic version is open source and available under the MIT license.
  54. Creating the PO template
  55. ------------------------
  56. Automatic generation using the editor
  57. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58. Since Godot 4.0, the editor can generate a PO template automatically from
  59. specified scene and GDScript files. This POT generation also supports translation
  60. contexts and pluralization if used in a script, with the optional second
  61. argument of ``tr()`` and the ``tr_n()`` method.
  62. Open the Project Settings' **Localization > POT Generation** tab, then use the
  63. **Add…** button to specify the path to your project's scenes and scripts that
  64. contain localizable strings:
  65. .. figure:: img/localization_using_gettext_pot_generation.webp
  66. :align: center
  67. :alt: Creating a PO template in the Localization > POT Generation tab of the Project Settings
  68. Creating a PO template in the **Localization > POT Generation** tab of the Project Settings
  69. After adding at least one scene or script, click **Generate POT** in the
  70. top-right corner, then specify the path to the output file. This file can be
  71. placed anywhere in the project directory, but it's recommended to keep it in a
  72. subdirectory such as ``locale``, as each locale will be defined in its own file.
  73. See :ref:`below <doc_localization_using_gettext_gdscript>` for how to add comments for translators
  74. or exclude some strings from being added to the PO template for GDScript files.
  75. You can then move over to
  76. :ref:`creating a messages file from a PO template <doc_localization_using_gettext_messages_file>`.
  77. .. note::
  78. Remember to regenerate the PO template after making any changes to
  79. localizable strings, or after adding new scenes or scripts. Otherwise, newly
  80. added strings will not be localizable and translators won't be able to
  81. update translations for outdated strings.
  82. Manual creation
  83. ~~~~~~~~~~~~~~~
  84. If the automatic generation approach doesn't work out for your needs, you can
  85. create a PO template by hand in a text editor. This file can be placed anywhere
  86. in the project directory, but it's recommended to keep it in a subdirectory, as
  87. each locale will be defined in its own file.
  88. Create a directory named ``locale`` in the project directory. In this directory,
  89. save a file named ``messages.pot`` with the following contents:
  90. ::
  91. # Don't remove the two lines below, they're required for gettext to work correctly.
  92. msgid ""
  93. msgstr ""
  94. # Example of a regular string.
  95. msgid "Hello world!"
  96. msgstr ""
  97. # Example of a string with pluralization.
  98. msgid "There is %d apple."
  99. msgid_plural "There are %d apples."
  100. msgstr[0] ""
  101. msgstr[1] ""
  102. # Example of a string with a translation context.
  103. msgctxt "Actions"
  104. msgid "Close"
  105. msgstr ""
  106. Messages in gettext are made of ``msgid`` and ``msgstr`` pairs.
  107. ``msgid`` is the source string (usually in English), ``msgstr`` will be
  108. the translated string.
  109. .. warning::
  110. The ``msgstr`` value in PO template files (``.pot``) should **always** be
  111. empty. Localization will be done in the generated ``.po`` files instead.
  112. .. _doc_localization_using_gettext_messages_file:
  113. Creating a messages file from a PO template
  114. -------------------------------------------
  115. The ``msginit`` command is used to turn a PO template into a messages file.
  116. For instance, to create a French localization file, use the following command
  117. while in the ``locale`` directory:
  118. .. code-block:: shell
  119. msginit --no-translator --input=messages.pot --locale=fr
  120. The command above will create a file named ``fr.po`` in the same directory
  121. as the PO template.
  122. Alternatively, you can do that graphically using Poedit, or by uploading the
  123. POT file to your web platform of choice.
  124. Loading a messages file in Godot
  125. --------------------------------
  126. To register a messages file as a translation in a project, open the
  127. **Project Settings**, then go to the **Localization** tab.
  128. In **Translations**, click **Add…** then choose the ``.po`` or ``.mo`` file
  129. in the file dialog. The locale will be inferred from the
  130. ``"Language: <code>\n"`` property in the messages file.
  131. .. note:: See :ref:`doc_internationalizing_games` for more information on
  132. importing and testing translations in Godot.
  133. Updating message files to follow the PO template
  134. ------------------------------------------------
  135. After updating the PO template, you will have to update message files so
  136. that they contain new strings, while removing strings that are no longer
  137. present in the PO template. This can be done automatically using the
  138. ``msgmerge`` tool:
  139. .. code-block:: shell
  140. # The order matters: specify the message file *then* the PO template!
  141. msgmerge --update --backup=none fr.po messages.pot
  142. If you want to keep a backup of the original message file (which would be
  143. saved as ``fr.po~`` in this example), remove the ``--backup=none`` argument.
  144. .. note::
  145. After running ``msgmerge``, strings which were modified in the source language
  146. will have a "fuzzy" comment added before them in the ``.po`` file. This comment
  147. denotes that the translation should be updated to match the new source string,
  148. as the translation will most likely be inaccurate until it's updated.
  149. Strings with "fuzzy" comments will **not** be read by Godot until the
  150. translation is updated and the "fuzzy" comment is removed.
  151. Checking the validity of a PO file or template
  152. ----------------------------------------------
  153. It is possible to check whether a gettext file's syntax is valid.
  154. If you open with Poeditor, it will display the appropriate warnings if there's some
  155. syntax errors. You can also verify by running the gettext command below:
  156. .. code-block:: shell
  157. msgfmt fr.po --check
  158. If there are syntax errors or warnings, they will be displayed in the console.
  159. Otherwise, ``msgfmt`` won't output anything.
  160. Using binary MO files (useful for large projects only)
  161. ------------------------------------------------------
  162. For large projects with several thousands of strings to translate or more,
  163. it can be worth it to use binary (compiled) MO message files instead of text-based
  164. PO files. Binary MO files are smaller and faster to read than the equivalent
  165. PO files.
  166. You can generate an MO file with the command below:
  167. .. code-block:: shell
  168. msgfmt fr.po --no-hash -o fr.mo
  169. If the PO file is valid, this command will create an ``fr.mo`` file besides
  170. the PO file. This MO file can then be loaded in Godot as described above.
  171. The original PO file should be kept in version control so you can update
  172. your translation in the future. In case you lose the original PO file and
  173. wish to decompile an MO file into a text-based PO file, you can do so with:
  174. .. code-block:: shell
  175. msgunfmt fr.mo > fr.po
  176. The decompiled file will not include comments or fuzzy strings, as these are
  177. never compiled in the MO file in the first place.
  178. .. _doc_localization_using_gettext_gdscript:
  179. Extracting localizable strings from GDScript files
  180. --------------------------------------------------
  181. The built-in `editor plugin <https://github.com/godotengine/godot/blob/master/modules/gdscript/editor/gdscript_translation_parser_plugin.h>`_
  182. recognizes a variety of patterns in source code to extract localizable strings
  183. from GDScript files, including but not limited to the following:
  184. - ``tr()``, ``tr_n()``, ``atr()``, and ``atr_n()`` calls;
  185. - assigning properties ``text``, ``placeholder_text``, and ``tooltip_text``;
  186. - ``add_tab()``, ``add_item()``, ``set_tab_title()``, and other calls;
  187. - ``FileDialog`` filters like ``"*.png ; PNG Images"``.
  188. .. note::
  189. The argument or right operand must be a constant string, otherwise the plugin
  190. will not be able to evaluate the expression and will ignore it.
  191. If the plugin extracts unnecessary strings, you can ignore them with the ``NO_TRANSLATE`` comment.
  192. You can also provide additional information for translators using the ``TRANSLATORS:`` comment.
  193. These comments must be placed either on the same line as the recognized pattern or precede it.
  194. ::
  195. $CharacterName.text = "???" # NO_TRANSLATE
  196. # NO_TRANSLATE: Language name.
  197. $TabContainer.set_tab_title(0, "Python")
  198. item.text = "Tool" # TRANSLATORS: Up to 10 characters.
  199. # TRANSLATORS: This is a reference to Lewis Carroll's poem "Jabberwocky",
  200. # make sure to keep this as it is important to the plot.
  201. say(tr("He took his vorpal sword in hand. The end?"))
  202. Using context
  203. -------------
  204. The ``context`` parameter can be used to differentiate the situation where a translation
  205. is used, or to differentiate polysemic words (words with multiple meanings).
  206. For example:
  207. ::
  208. tr("Start", "Main Menu")
  209. tr("End", "Main Menu")
  210. tr("Shop", "Main Menu")
  211. tr("Shop", "In Game")
  212. Updating PO files
  213. -----------------
  214. 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
  215. need to update the existing PO files to include the new strings.
  216. First, generate a new POT file containing all the existing strings plus the newly added strings. After that, merge the existing
  217. PO files with the new POT file. There are two ways to do this:
  218. - Use a gettext editor, and it should have an option to update a PO file from a POT file.
  219. - Use the gettext ``msgmerge`` tool:
  220. .. code-block:: shell
  221. # The order matters: specify the message file *then* the PO template!
  222. msgmerge --update --backup=none fr.po messages.pot
  223. If you want to keep a backup of the original message file (which would be saved as ``fr.po~`` in this example),
  224. remove the ``--backup=none`` argument.
  225. POT generation custom plugin
  226. ----------------------------
  227. 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.
  228. This custom plugin will extract the strings and write into the POT file when you hit **Generate POT**. To learn more about how to
  229. create the translation parser plugin, see :ref:`EditorTranslationParserPlugin <class_EditorTranslationParserPlugin>`.