localization_using_gettext.rst 6.5 KB

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