localization_using_gettext.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. (``.po``) format.
  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 doesn't use compiled message object files
  28. (``.mo``), translators can test their work without having to install
  29. gettext tools.
  30. Caveats
  31. -------
  32. - As Godot uses its own PO file parser behind the scenes
  33. (which is more limited than the reference GNU gettext implementation),
  34. some features such as pluralization aren't supported.
  35. Installing gettext tools
  36. ------------------------
  37. The command line gettext tools are required to perform maintenance operations,
  38. such as updating message files. Therefore, it's strongly recommended to
  39. install them.
  40. - **Windows:** Download an installer from
  41. `this page <https://mlocati.github.io/articles/gettext-iconv-windows.html>`_.
  42. Any architecture and binary type (shared or static) works;
  43. if in doubt, choose the 64-bit static installer.
  44. - **macOS:** Use `Homebrew <https://brew.sh/>`_ to install gettext with the
  45. ``brew install gettext`` command.
  46. - **Linux:** On most distributions, install the ``gettext`` package from
  47. your distribution's package manager.
  48. Creating the PO template (POT) manually
  49. ---------------------------------------
  50. Godot currently doesn't support extracting source strings using ``xgettext``,
  51. so the ``.pot`` file must be created manually. This file can be placed anywhere
  52. in the project directory, but it's recommended to keep it in a subdirectory, as
  53. each locale will be defined in its own file.
  54. Create a directory named `locale` in the project directory. In this directory,
  55. save a file named ``messages.pot`` with the following contents:
  56. ::
  57. # Don't remove the two lines below, they're required for gettext to work correctly.
  58. msgid ""
  59. msgstr ""
  60. msgid "Hello world!"
  61. msgstr ""
  62. Messages in gettext are made of ``msgid`` and ``msgstr`` pairs.
  63. ``msgid`` is the source string (usually in English), ``msgstr`` will be
  64. the translated string.
  65. The ``msgstr`` value in PO template files (``.pot``) should **always** be empty.
  66. Localization will be done in the generated ``.po`` files instead.
  67. Creating the PO template (POT) using pybabel
  68. --------------------------------------------
  69. The Python tool pybabel has support for Godot and can be used to automatically
  70. create and update the POT file from your scene files and scripts.
  71. After installing ``babel`` and ``babel-godot``, for example using pip:
  72. .. code-block:: shell
  73. pip install babel babel-godot
  74. Write a mapping file (for example ``babelrc``) which will indicate which files
  75. pybabel needs to process (note that we process GDScript as Python, which is
  76. generally sufficient):
  77. .. code-block:: none
  78. [python: **.gd]
  79. encoding = utf-8
  80. [godot_scene: **.tscn]
  81. encoding = utf-8
  82. You can then run pybabel like so:
  83. .. code-block:: shell
  84. pybabel extract -F babelrc -k text -k LineEdit/placeholder_text -k tr -o godot-l10n.pot .
  85. Use the ``-k`` option to specify what needs to be extracted. In this case,
  86. arguments to :ref:`tr() <class_Object_method_tr>` will be translated, as well
  87. as properties named "text" (commonly used by Control nodes) and LineEdit's
  88. "placeholder_text" property.
  89. Creating a messages file from a PO template
  90. -------------------------------------------
  91. The ``msginit`` command is used to turn a PO template into a messages file.
  92. For instance, to create a French localization file, use the following command
  93. while in the ``locale`` directory:
  94. .. code-block:: shell
  95. msginit --no-translator --input=messages.pot --locale=fr
  96. The command above will create a file named ``fr.po`` in the same directory
  97. as the PO template.
  98. Alternatively, you can do that graphically using Poedit, or by uploading the
  99. POT file to your web platform of choice.
  100. Loading a messages file in Godot
  101. --------------------------------
  102. To register a messages file as a translation in a project, open the
  103. **Project Settings**, then go to the **Localization** tab.
  104. In **Translations**, click **Add…** then choose the ``.po`` file
  105. in the file dialog. The locale will be inferred from the
  106. ``"Language: <code>\n"`` property in the messages file.
  107. .. note:: See :ref:`doc_internationalizing_games` for more information on
  108. importing and testing translations in Godot.
  109. Updating message files to follow the PO template
  110. ------------------------------------------------
  111. After updating the PO template, you will have to update message files so
  112. that they contain new strings, while removing strings that are no longer
  113. present in the PO template removed in the PO template. This can be done
  114. automatically using the ``msgmerge`` tool:
  115. .. code-block:: shell
  116. # The order matters: specify the message file *then* the PO template!
  117. msgmerge --update --backup=none fr.po messages.pot
  118. If you want to keep a backup of the original message file (which would be
  119. saved as ``fr.po~`` in this example), remove the ``--backup=none`` argument.
  120. Checking the validity of a PO file or template
  121. ----------------------------------------------
  122. It is possible to check whether a gettext file's syntax is valid by running
  123. the command below:
  124. .. code-block:: shell
  125. msgfmt fr.po --check
  126. If there are syntax errors or warnings, they will be displayed in the console.
  127. Otherwise, ``msgfmt`` won't output anything.