class_editortranslationparserplugin.rst 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. The return value should be an :ref:`Array<class_Array>` of :ref:`PackedStringArray<class_PackedStringArray>`\ s, one for each extracted translatable string. Each entry should contain ``[msgid, msgctxt, msgid_plural, comment]``, where all except ``msgid`` are optional. Empty strings will be ignored.
  16. The extracted strings will be written into a POT file selected by user under "POT Generation" in "Localization" tab in "Project Settings" menu.
  17. Below shows an example of a custom parser that extracts strings from a CSV file to write into a POT.
  18. .. tabs::
  19. .. code-tab:: gdscript
  20. @tool
  21. extends EditorTranslationParserPlugin
  22. func _parse_file(path):
  23. var ret: Array[PackedStringArray] = []
  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. ret.append(PackedStringArray([s]))
  29. #print("Extracted string: " + s)
  30. return ret
  31. func _get_recognized_extensions():
  32. return ["csv"]
  33. .. code-tab:: csharp
  34. using Godot;
  35. [Tool]
  36. public partial class CustomParser : EditorTranslationParserPlugin
  37. {
  38. public override Godot.Collections.Array<string[]> _ParseFile(string path)
  39. {
  40. Godot.Collections.Array<string[]> ret;
  41. using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
  42. string text = file.GetAsText();
  43. string[] splitStrs = text.Split(",", allowEmpty: false);
  44. foreach (string s in splitStrs)
  45. {
  46. ret.Add([s]);
  47. //GD.Print($"Extracted string: {s}");
  48. }
  49. return ret;
  50. }
  51. public override string[] _GetRecognizedExtensions()
  52. {
  53. return ["csv"];
  54. }
  55. }
  56. To add a translatable string associated with a context, plural, or comment:
  57. .. tabs::
  58. .. code-tab:: gdscript
  59. # This will add a message with msgid "Test 1", msgctxt "context", msgid_plural "test 1 plurals", and comment "test 1 comment".
  60. ret.append(PackedStringArray(["Test 1", "context", "test 1 plurals", "test 1 comment"]))
  61. # This will add a message with msgid "A test without context" and msgid_plural "plurals".
  62. ret.append(PackedStringArray(["A test without context", "", "plurals"]))
  63. # This will add a message with msgid "Only with context" and msgctxt "a friendly context".
  64. ret.append(PackedStringArray(["Only with context", "a friendly context"]))
  65. .. code-tab:: csharp
  66. // This will add a message with msgid "Test 1", msgctxt "context", msgid_plural "test 1 plurals", and comment "test 1 comment".
  67. ret.Add(["Test 1", "context", "test 1 plurals", "test 1 comment"]);
  68. // This will add a message with msgid "A test without context" and msgid_plural "plurals".
  69. ret.Add(["A test without context", "", "plurals"]);
  70. // This will add a message with msgid "Only with context" and msgctxt "a friendly context".
  71. ret.Add(["Only with context", "a friendly context"]);
  72. \ **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:
  73. .. tabs::
  74. .. code-tab:: gdscript
  75. func _parse_file(path):
  76. var res = ResourceLoader.load(path, "Script")
  77. var text = res.source_code
  78. # Parsing logic.
  79. func _get_recognized_extensions():
  80. return ["gd"]
  81. .. code-tab:: csharp
  82. public override Godot.Collections.Array<string[]> _ParseFile(string path)
  83. {
  84. var res = ResourceLoader.Load<Script>(path, "Script");
  85. string text = res.SourceCode;
  86. // Parsing logic.
  87. }
  88. public override string[] _GetRecognizedExtensions()
  89. {
  90. return ["gd"];
  91. }
  92. To use **EditorTranslationParserPlugin**, register it using the :ref:`EditorPlugin.add_translation_parser_plugin()<class_EditorPlugin_method_add_translation_parser_plugin>` method first.
  93. .. rst-class:: classref-reftable-group
  94. Methods
  95. -------
  96. .. table::
  97. :widths: auto
  98. +--------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------+
  99. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`_get_recognized_extensions<class_EditorTranslationParserPlugin_private_method__get_recognized_extensions>`\ (\ ) |virtual| |const| |
  100. +--------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------+
  101. | :ref:`Array<class_Array>`\[:ref:`PackedStringArray<class_PackedStringArray>`\] | :ref:`_parse_file<class_EditorTranslationParserPlugin_private_method__parse_file>`\ (\ path\: :ref:`String<class_String>`\ ) |virtual| |
  102. +--------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------+
  103. .. rst-class:: classref-section-separator
  104. ----
  105. .. rst-class:: classref-descriptions-group
  106. Method Descriptions
  107. -------------------
  108. .. _class_EditorTranslationParserPlugin_private_method__get_recognized_extensions:
  109. .. rst-class:: classref-method
  110. :ref:`PackedStringArray<class_PackedStringArray>` **_get_recognized_extensions**\ (\ ) |virtual| |const| :ref:`🔗<class_EditorTranslationParserPlugin_private_method__get_recognized_extensions>`
  111. Gets the list of file extensions to associate with this parser, e.g. ``["csv"]``.
  112. .. rst-class:: classref-item-separator
  113. ----
  114. .. _class_EditorTranslationParserPlugin_private_method__parse_file:
  115. .. rst-class:: classref-method
  116. :ref:`Array<class_Array>`\[:ref:`PackedStringArray<class_PackedStringArray>`\] **_parse_file**\ (\ path\: :ref:`String<class_String>`\ ) |virtual| :ref:`🔗<class_EditorTranslationParserPlugin_private_method__parse_file>`
  117. Override this method to define a custom parsing logic to extract the translatable strings.
  118. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  119. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  120. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  121. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  122. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  123. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  124. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  125. .. |void| replace:: :abbr:`void (No return value.)`