class_editortranslationparserplugin.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/EditorTranslationParserPlugin.xml.
  6. .. _class_EditorTranslationParserPlugin:
  7. EditorTranslationParserPlugin
  8. =============================
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. Plugin for adding custom parsers to extract strings that are to be translated from custom files (.csv, .json etc.).
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. **EditorTranslationParserPlugin** is invoked when a file is being parsed to extract strings that require translation. To define the parsing and string extraction logic, override the :ref:`_parse_file<class_EditorTranslationParserPlugin_private_method__parse_file>` method in script.
  15. Add the extracted strings to argument ``msgids`` or ``msgids_context_plural`` if context or plural is used.
  16. When adding to ``msgids_context_plural``, you must add the data using the format ``["A", "B", "C"]``, where ``A`` represents the extracted string, ``B`` represents the context, and ``C`` represents the plural version of the extracted string. If you want to add only context but not plural, put ``""`` for the plural slot. The idea is the same if you only want to add plural but not context. See the code below for concrete examples.
  17. The extracted strings will be written into a POT file selected by user under "POT Generation" in "Localization" tab in "Project Settings" menu.
  18. Below shows an example of a custom parser that extracts strings from a CSV file to write into a POT.
  19. .. tabs::
  20. .. code-tab:: gdscript
  21. @tool
  22. extends EditorTranslationParserPlugin
  23. func _parse_file(path, msgids, msgids_context_plural):
  24. var file = FileAccess.open(path, FileAccess.READ)
  25. var text = file.get_as_text()
  26. var split_strs = text.split(",", false)
  27. for s in split_strs:
  28. msgids.append(s)
  29. #print("Extracted string: " + s)
  30. func _get_recognized_extensions():
  31. return ["csv"]
  32. .. code-tab:: csharp
  33. using Godot;
  34. [Tool]
  35. public partial class CustomParser : EditorTranslationParserPlugin
  36. {
  37. public override void _ParseFile(string path, Godot.Collections.Array<string> msgids, Godot.Collections.Array<Godot.Collections.Array> msgidsContextPlural)
  38. {
  39. using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
  40. string text = file.GetAsText();
  41. string[] splitStrs = text.Split(",", allowEmpty: false);
  42. foreach (string s in splitStrs)
  43. {
  44. msgids.Add(s);
  45. //GD.Print($"Extracted string: {s}");
  46. }
  47. }
  48. public override string[] _GetRecognizedExtensions()
  49. {
  50. return new string[] { "csv" };
  51. }
  52. }
  53. To add a translatable string associated with context or plural, add it to ``msgids_context_plural``:
  54. .. tabs::
  55. .. code-tab:: gdscript
  56. # This will add a message with msgid "Test 1", msgctxt "context", and msgid_plural "test 1 plurals".
  57. msgids_context_plural.append(["Test 1", "context", "test 1 plurals"])
  58. # This will add a message with msgid "A test without context" and msgid_plural "plurals".
  59. msgids_context_plural.append(["A test without context", "", "plurals"])
  60. # This will add a message with msgid "Only with context" and msgctxt "a friendly context".
  61. msgids_context_plural.append(["Only with context", "a friendly context", ""])
  62. .. code-tab:: csharp
  63. // This will add a message with msgid "Test 1", msgctxt "context", and msgid_plural "test 1 plurals".
  64. msgidsContextPlural.Add(new Godot.Collections.Array{"Test 1", "context", "test 1 Plurals"});
  65. // This will add a message with msgid "A test without context" and msgid_plural "plurals".
  66. msgidsContextPlural.Add(new Godot.Collections.Array{"A test without context", "", "plurals"});
  67. // This will add a message with msgid "Only with context" and msgctxt "a friendly context".
  68. msgidsContextPlural.Add(new Godot.Collections.Array{"Only with context", "a friendly context", ""});
  69. \ **Note:** If you override parsing logic for standard script types (GDScript, C#, etc.), it would be better to load the ``path`` argument using :ref:`ResourceLoader.load<class_ResourceLoader_method_load>`. This is because built-in scripts are loaded as :ref:`Resource<class_Resource>` type, not :ref:`FileAccess<class_FileAccess>` type. For example:
  70. .. tabs::
  71. .. code-tab:: gdscript
  72. func _parse_file(path, msgids, msgids_context_plural):
  73. var res = ResourceLoader.load(path, "Script")
  74. var text = res.source_code
  75. # Parsing logic.
  76. func _get_recognized_extensions():
  77. return ["gd"]
  78. .. code-tab:: csharp
  79. public override void _ParseFile(string path, Godot.Collections.Array<string> msgids, Godot.Collections.Array<Godot.Collections.Array> msgidsContextPlural)
  80. {
  81. var res = ResourceLoader.Load<Script>(path, "Script");
  82. string text = res.SourceCode;
  83. // Parsing logic.
  84. }
  85. public override string[] _GetRecognizedExtensions()
  86. {
  87. return new string[] { "gd" };
  88. }
  89. To use **EditorTranslationParserPlugin**, register it using the :ref:`EditorPlugin.add_translation_parser_plugin<class_EditorPlugin_method_add_translation_parser_plugin>` method first.
  90. .. rst-class:: classref-reftable-group
  91. Methods
  92. -------
  93. .. table::
  94. :widths: auto
  95. +---------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  96. | |void| | :ref:`_get_comments<class_EditorTranslationParserPlugin_private_method__get_comments>`\ (\ msgids_comment\: :ref:`Array<class_Array>`\[:ref:`String<class_String>`\], msgids_context_plural_comment\: :ref:`Array<class_Array>`\[:ref:`String<class_String>`\]\ ) |virtual| |
  97. +---------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  98. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`_get_recognized_extensions<class_EditorTranslationParserPlugin_private_method__get_recognized_extensions>`\ (\ ) |virtual| |const| |
  99. +---------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  100. | |void| | :ref:`_parse_file<class_EditorTranslationParserPlugin_private_method__parse_file>`\ (\ path\: :ref:`String<class_String>`, msgids\: :ref:`Array<class_Array>`\[:ref:`String<class_String>`\], msgids_context_plural\: :ref:`Array<class_Array>`\[:ref:`Array<class_Array>`\]\ ) |virtual| |
  101. +---------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  102. .. rst-class:: classref-section-separator
  103. ----
  104. .. rst-class:: classref-descriptions-group
  105. Method Descriptions
  106. -------------------
  107. .. _class_EditorTranslationParserPlugin_private_method__get_comments:
  108. .. rst-class:: classref-method
  109. |void| **_get_comments**\ (\ msgids_comment\: :ref:`Array<class_Array>`\[:ref:`String<class_String>`\], msgids_context_plural_comment\: :ref:`Array<class_Array>`\[:ref:`String<class_String>`\]\ ) |virtual| :ref:`🔗<class_EditorTranslationParserPlugin_private_method__get_comments>`
  110. If overridden, called after :ref:`_parse_file<class_EditorTranslationParserPlugin_private_method__parse_file>` to get comments for the parsed entries. This method should fill the arrays with the same number of elements and in the same order as :ref:`_parse_file<class_EditorTranslationParserPlugin_private_method__parse_file>`.
  111. .. rst-class:: classref-item-separator
  112. ----
  113. .. _class_EditorTranslationParserPlugin_private_method__get_recognized_extensions:
  114. .. rst-class:: classref-method
  115. :ref:`PackedStringArray<class_PackedStringArray>` **_get_recognized_extensions**\ (\ ) |virtual| |const| :ref:`🔗<class_EditorTranslationParserPlugin_private_method__get_recognized_extensions>`
  116. Gets the list of file extensions to associate with this parser, e.g. ``["csv"]``.
  117. .. rst-class:: classref-item-separator
  118. ----
  119. .. _class_EditorTranslationParserPlugin_private_method__parse_file:
  120. .. rst-class:: classref-method
  121. |void| **_parse_file**\ (\ path\: :ref:`String<class_String>`, msgids\: :ref:`Array<class_Array>`\[:ref:`String<class_String>`\], msgids_context_plural\: :ref:`Array<class_Array>`\[:ref:`Array<class_Array>`\]\ ) |virtual| :ref:`🔗<class_EditorTranslationParserPlugin_private_method__parse_file>`
  122. Override this method to define a custom parsing logic to extract the translatable strings.
  123. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  124. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  125. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  126. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  127. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  128. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  129. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  130. .. |void| replace:: :abbr:`void (No return value.)`