class_editortranslationparserplugin.rst 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. Description
  12. -----------
  13. ``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_method__parse_file>` method in script.
  14. Add the extracted strings to argument ``msgids`` or ``msgids_context_plural`` if context or plural is used.
  15. 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.
  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, msgids, msgids_context_plural):
  23. var file = File.new()
  24. file.open(path, File.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. using System;
  35. [Tool]
  36. public class CustomParser : EditorTranslationParserPlugin
  37. {
  38. public override void ParseFile(string path, Godot.Collections.Array msgids, Godot.Collections.Array msgidsContextPlural)
  39. {
  40. var file = new File();
  41. file.Open(path, File.ModeFlags.Read);
  42. string text = file.GetAsText();
  43. string[] splitStrs = text.Split(",", false);
  44. foreach (var s in splitStrs)
  45. {
  46. msgids.Add(s);
  47. //GD.Print("Extracted string: " + s)
  48. }
  49. }
  50. public override Godot.Collections.Array GetRecognizedExtensions()
  51. {
  52. return new Godot.Collections.Array{"csv"};
  53. }
  54. }
  55. To add a translatable string associated with context or plural, add it to ``msgids_context_plural``:
  56. .. tabs::
  57. .. code-tab:: gdscript
  58. # This will add a message with msgid "Test 1", msgctxt "context", and msgid_plural "test 1 plurals".
  59. msgids_context_plural.append(["Test 1", "context", "test 1 plurals"])
  60. # This will add a message with msgid "A test without context" and msgid_plural "plurals".
  61. msgids_context_plural.append(["A test without context", "", "plurals"])
  62. # This will add a message with msgid "Only with context" and msgctxt "a friendly context".
  63. msgids_context_plural.append(["Only with context", "a friendly context", ""])
  64. .. code-tab:: csharp
  65. // This will add a message with msgid "Test 1", msgctxt "context", and msgid_plural "test 1 plurals".
  66. msgidsContextPlural.Add(new Godot.Collections.Array{"Test 1", "context", "test 1 Plurals"});
  67. // This will add a message with msgid "A test without context" and msgid_plural "plurals".
  68. msgidsContextPlural.Add(new Godot.Collections.Array{"A test without context", "", "plurals"});
  69. // This will add a message with msgid "Only with context" and msgctxt "a friendly context".
  70. msgidsContextPlural.Add(new Godot.Collections.Array{"Only with context", "a friendly context", ""});
  71. \ **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:`File<class_File>` type.
  72. For example:
  73. .. tabs::
  74. .. code-tab:: gdscript
  75. func _parse_file(path, msgids, msgids_context_plural):
  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 void ParseFile(string path, Godot.Collections.Array msgids, Godot.Collections.Array msgidsContextPlural)
  83. {
  84. var res = ResourceLoader.Load<Script>(path, "Script");
  85. string text = res.SourceCode;
  86. // Parsing logic.
  87. }
  88. public override Godot.Collections.Array GetRecognizedExtensions()
  89. {
  90. return new Godot.Collections.Array{"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. Methods
  94. -------
  95. +---------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  96. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`_get_recognized_extensions<class_EditorTranslationParserPlugin_method__get_recognized_extensions>` **(** **)** |virtual| |const| |
  97. +---------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  98. | void | :ref:`_parse_file<class_EditorTranslationParserPlugin_method__parse_file>` **(** :ref:`String<class_String>` path, :ref:`String[]<class_String>` msgids, :ref:`Array[]<class_Array>` msgids_context_plural **)** |virtual| |
  99. +---------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  100. Method Descriptions
  101. -------------------
  102. .. _class_EditorTranslationParserPlugin_method__get_recognized_extensions:
  103. - :ref:`PackedStringArray<class_PackedStringArray>` **_get_recognized_extensions** **(** **)** |virtual| |const|
  104. Gets the list of file extensions to associate with this parser, e.g. ``["csv"]``.
  105. ----
  106. .. _class_EditorTranslationParserPlugin_method__parse_file:
  107. - void **_parse_file** **(** :ref:`String<class_String>` path, :ref:`String[]<class_String>` msgids, :ref:`Array[]<class_Array>` msgids_context_plural **)** |virtual|
  108. Override this method to define a custom parsing logic to extract the translatable strings.
  109. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  110. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  111. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  112. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  113. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  114. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`