internationalizing_games.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. .. _doc_internationalizing_games:
  2. Internationalizing games
  3. ========================
  4. Introduction
  5. ------------
  6. Sería excelente que el mundo hablara solo un idioma (It would be great if the
  7. world spoke only one language). Unfortunately for
  8. us developers, that is not the case. While indie or niche games usually
  9. do not need localization, games targeting a more massive market
  10. often require localization. Godot offers many tools to make this process
  11. more straightforward, so this tutorial is more like a collection of
  12. tips and tricks.
  13. Localization is usually done by specific studios hired for the job. Despite the
  14. huge amount of software and file formats available for this, the most common way
  15. to do localization to this day is still with spreadsheets. The process of
  16. creating the spreadsheets and importing them is already covered in the
  17. :ref:`doc_importing_translations` tutorial. If you haven't read the Importing
  18. translations page before, we recommend you give it a read before reading this
  19. page.
  20. .. note:: We will be using the official demo as an example; you can
  21. `download it from the Asset Library <https://godotengine.org/asset-library/asset/134>`_.
  22. Configuring the imported translation
  23. ------------------------------------
  24. Translations can get updated and re-imported when they change, but
  25. they still have to be added to the project. This is done in
  26. **Project → Project Settings → Localization**:
  27. .. image:: img/localization_dialog.png
  28. The above dialog is used to add or remove translations project-wide.
  29. Localizing resources
  30. --------------------
  31. It is also possible to instruct Godot to use alternate versions of
  32. assets (resources) depending on the current language. This can be used for
  33. localized images such as in-game billboards or localized voices.
  34. The **Remaps** tab can be used for this:
  35. .. image:: img/localization_remaps.png
  36. Select the resource to be remapped then add some alternatives for each locale.
  37. .. note::
  38. The resource remapping system isn't supported for DynamicFonts. To use
  39. different fonts depending on the language's script, use the DynamicFont
  40. fallback system instead, which lets you define as many fallback fonts as you
  41. want.
  42. The upside of the DynamicFont fallback system is that it works regardless of
  43. the current language, making it ideal for things like multiplayer chat where
  44. the text language may not match the client's language.
  45. Converting keys to text
  46. -----------------------
  47. Some controls, such as :ref:`Button <class_Button>` and :ref:`Label <class_Label>`,
  48. will automatically fetch a translation if their text matches a translation key.
  49. For example, if a label's text is "MAIN_SCREEN_GREETING1" and that key exists
  50. in the current translation, then the text will automatically be translated.
  51. This automatic translation behavior may be undesirable in certain cases. For
  52. instance, when using a Label to display a player's name, you most likely don't
  53. want the player's name to be translated if it matches a translation key. To
  54. disable automatic translation on a specific node, disable **Localization > Auto
  55. Translate** in the inspector.
  56. In code, the :ref:`Object.tr() <class_Object_method_tr>` function can be used.
  57. This will just look up the text in the translations and convert it if found:
  58. ::
  59. level.text = tr("LEVEL_5_NAME")
  60. status.text = tr("GAME_STATUS_%d" % status_index)
  61. .. note::
  62. If no text is displayed after changing the language, try to use a different
  63. font. The default project font only supports a subset of the Latin-1 character set,
  64. which cannot be used to display languages like Russian or Chinese.
  65. A good resource for multilingual fonts is `Noto Fonts <https://www.google.com/get/noto/>`__.
  66. Make sure to download the correct variation if you're using a less common
  67. language.
  68. Once you've downloaded the font, load the TTF file into a DynamicFont
  69. resource and use it as a custom font of your Control node. For better
  70. reusability, associate a new a Theme resource to your root Control node and
  71. define the DynamicFont as the Default Font in the theme.
  72. Placeholders
  73. ^^^^^^^^^^^^
  74. To feature placeholders in your translated strings, use
  75. :ref:`doc_gdscript_printf` or the equivalent feature in C#. This lets
  76. translators move the location of the placeholder in the string freely, which
  77. allows translations to sound more natural. Named placeholders with the
  78. ``String.format()`` function should be used whenever possible, as they also
  79. allow translators to choose the *order* in which placeholders appear:
  80. ::
  81. # The placeholder's locations can be changed, but not their order.
  82. # This will probably not suffice for some target languages.
  83. message.text = tr("%s picked up the %s") % ["Ogre", "Sword"]
  84. # The placeholder's locations and order can be changed.
  85. # Additionally, this form gives more context for translators to work with.
  86. message.text = tr("{character} picked up the {weapon}").format({character = "Ogre", weapon = "Sword"})
  87. Translation contexts
  88. ^^^^^^^^^^^^^^^^^^^^
  89. If you're using plain English as source strings (rather than message codes
  90. ``LIKE_THIS``), you may run into ambiguities when you have to translate the same
  91. English string to different strings in certain target languages. You can
  92. optionally specify a *translation context* to resolve this ambiguity and allow
  93. target languages to use different strings, even though the source string is
  94. identical:
  95. ::
  96. # "Close", as in an action (to close something).
  97. button.set_text(tr("Close", "Actions"))
  98. # "Close", as in a distance (opposite of "far").
  99. distance_label.set_text(tr("Close", "Distance"))
  100. Pluralization
  101. ^^^^^^^^^^^^^
  102. Most languages require different strings depending on whether an object is in
  103. singular or plural form. However, hardcoding the "is plural" condition depending
  104. on whether there is more than 1 object is not valid in all languages.
  105. Some languages have more than two plural forms, and the rules on the number of
  106. objects required for each plural form vary. Godot offers support for
  107. *pluralization* so that the target locales can handle this automatically.
  108. Pluralization is meant to be used with positive (or zero) integer numbers only.
  109. Negative and floating-point values usually represent physical entities for which
  110. singular and plural don't clearly apply.
  111. ::
  112. var num_apples = 5
  113. label.text = tr_n("There is %d apple", "There are %d apples", num_apples) % num_apples
  114. This can be combined with a context if needed:
  115. ::
  116. var num_jobs = 1
  117. label.text = tr_n("%d job", "%d jobs", num_jobs, "Task Manager") % num_jobs
  118. .. note::
  119. Providing pluralized translations is only supported with
  120. :ref:`doc_localization_using_gettext`, not CSV.
  121. Making controls resizable
  122. -------------------------
  123. The same text in different languages can vary greatly in length. For
  124. this, make sure to read the tutorial on :ref:`doc_size_and_anchors`, as
  125. dynamically adjusting control sizes may help.
  126. :ref:`Container <class_Container>` can be useful, as well as the text wrapping
  127. options available in :ref:`Label <class_Label>`.
  128. To check whether your UI can accommodate translations with longer strings than
  129. the original, you can enable *pseudolocalization* in the advanced Project
  130. Settings. This will replace all your localizable strings with longer versions of
  131. themselves, while also replacing some characters in the original strings with
  132. accented versions (while still being readable). Placeholders are kept as-is,
  133. so that they keep working when pseudolocalization is enabled.
  134. For example, the string ``Hello world, this is %s!`` becomes
  135. ``[Ĥéłłô ŵôŕłd́, ŧh̀íš íš %s!]`` when pseudolocalization is enabled.
  136. While looking strange at first, pseudolocalization has several benefits:
  137. - It lets you spot non-localizable strings quickly, so you can go over them and
  138. make them localizable (if it makes sense to do so).
  139. - It lets you check UI elements that can't fit long strings. Many languages will
  140. feature much longer translations than the source text, so it's important to
  141. ensure your UI can accommodate longer-than-usual strings.
  142. - It lets you check whether your font contains all the characters required to
  143. support various languages. However, since the goal of pseudolocalization is to
  144. keep the original strings readable, it's not an effective test for checking
  145. whether a font can support :abbr:`CJK (Chinese, Japanese, Korean)` or
  146. right-to-left languages.
  147. The project settings allow you to tune pseudolocalization behavior, so that you
  148. can disable parts of it if desired.
  149. TranslationServer
  150. -----------------
  151. Godot has a server handling low-level translation management
  152. called the :ref:`TranslationServer <class_TranslationServer>`.
  153. Translations can be added or removed during run-time;
  154. the current language can also be changed at run-time.
  155. .. _doc_internationalizing_games_bidi:
  156. Bidirectional text and UI Mirroring
  157. -----------------------------------
  158. Arabic and Hebrew are written from right to left (except for the numbers and Latin
  159. words mixed in), and the user interface for these languages should be mirrored as well.
  160. In some languages the shape of a glyph changes depending on the surrounding characters.
  161. Support for bidirectional writing systems and UI mirroring is transparent, you don't
  162. usually need to change anything or have any knowledge of the specific writing system.
  163. For RTL languages, Godot will automatically do the following changes to the UI:
  164. - Mirrors left/right anchors and margins.
  165. - Swaps left and right text alignment.
  166. - Mirrors horizontal order of the child controls in the containers, and items in Tree/ItemList controls.
  167. - Uses mirrored order of the internal control elements (e.g. OptionButton dropdown button, checkbox alignment, List column order, Tree item icons and connecting line alignment, e.t.c.), in some cases mirrored controls use separate theme styles.
  168. - Coordinate system is not mirrored, and non-UI nodes (sprites, e.t.c) are not affected.
  169. It is possible to override text and control layout direction by using the following control properties:
  170. - ``text_direction``, sets the base text direction. When set to "auto", direction depends on the first strong directional character in the text according to the Unicode Bidirectional Algorithm,
  171. - ``language``, overrides current project locale.
  172. - ``structured_text_bidi_override property`` and ``_structured_text_parser callback``, enables special handling for structured text.
  173. - ``layout_direction``, overrides control mirroring.
  174. .. image:: img/ui_mirror.png
  175. .. seealso::
  176. You can see how right-to-left typesetting works in action using the
  177. `BiDI and Font Features demo project <https://github.com/godotengine/godot-demo-projects/tree/master/gui/bidi_and_font_features>`__.
  178. Adding break iterator data to exported project
  179. ----------------------------------------------
  180. Some languages are written without spaces, and word and line breaking requires more than rules over character sequences.
  181. Godot includes ICU rule and dictionary based, break iterator data, but this data is not included into exported projects by default.
  182. To include it go to **Project → Project Settings → Localization → Text Server Data** and click **Install support data...**. Break iterator data is about 4 MB large.
  183. .. image:: img/icu_data.png
  184. Structured text BiDi override
  185. -----------------------------
  186. Unicode BiDi algorithm is designed to work with natural text and it's incapable of
  187. handling text with the higher level order, like file names, URIs, email addresses,
  188. regular expressions or source code.
  189. .. image:: img/bidi_override.png
  190. For example, the path for this shown directory structure will be displayed incorrectly
  191. (top "LineEdit" control). "File" type structured text override splits text into segments,
  192. then BiDi algorithm is applied to each of them individually to correctly display directory
  193. names in any language and preserve correct order of the folders (bottom "LineEdit" control).
  194. Custom callbacks provide a way to override BiDi for the other types of structured text.
  195. Localizing numbers
  196. ------------------
  197. Controls specifically designed for number input or output (e.g. ProgressBar, SpinBox)
  198. will use localized numbering system automatically, for the other control
  199. :ref:`TextServer.format_number(string, language) <class_TextServer_method_format_number>`
  200. can be used to convert Western Arabic numbers (0..9) to the localized numbering system
  201. and :ref:`TextServer.parse_number(string, language) <class_TextServer_method_parse_number>`
  202. to convert it back.
  203. Localizing icons and images
  204. ---------------------------
  205. Icons with left and right pointing arrows which may need to be reversed for Arabic
  206. and Hebrew locales, in case they indicate movement or direction (e.g. back/forward
  207. buttons). Otherwise, they can remain the same.
  208. Testing translations
  209. --------------------
  210. You may want to test a project's translation before releasing it. Godot provides two ways
  211. to do this.
  212. First, in the Project Settings, under **Input Devices > Locale**, there is a **Test**
  213. property. Set this property to the locale code of the language you want to test. Godot will
  214. run the project with that locale when the project is run (either from the editor or when
  215. exported).
  216. .. image:: img/locale_test.png
  217. Keep in mind that since this is a project setting, it will show up in version control when
  218. it is set to a non-empty value. Therefore, it should be set back to an empty value before
  219. committing changes to version control.
  220. Translations can also be tested when :ref:`running Godot from the command line <doc_command_line_tutorial>`.
  221. For example, to test a game in French, the following argument can be
  222. supplied:
  223. .. code-block:: shell
  224. godot --language fr
  225. Translating the project name
  226. ----------------------------
  227. The project name becomes the app name when exporting to different
  228. operating systems and platforms. To specify the project name in more
  229. than one language, create a new setting ``application/name`` in the **Project
  230. Settings** and append the locale identifier to it.
  231. For instance, for Spanish, this would be ``application/name_es``:
  232. .. image:: img/localized_name.png
  233. If you are unsure about the language code to use, refer to the
  234. :ref:`list of locale codes <doc_locales>`.