Skip to content
0.2.0-dev Download ZIP
141 lines ·6.0 KB Raw
1<?xml version="1.0" encoding="UTF-8" ?>
2<class name="EditorTranslationParserPlugin" inherits="RefCounted" api_type="editor" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3 <brief_description>
4 Plugin for adding custom parsers to extract strings that are to be translated from custom files (.csv, .json etc.).
5 </brief_description>
6 <description>
7 [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 [method _parse_file] method in script.
8 The return value should be an [Array] of [PackedStringArray]s, one for each extracted translatable string. Each entry should contain [code][msgid, msgctxt, msgid_plural, comment, source_line][/code], where all except [code]msgid[/code] are optional. Empty strings will be ignored.
9 The extracted strings will be written into a translation template file selected by user under "Template Generation" in "Localization" tab in "Project Settings" menu.
10 Below shows an example of a custom parser that extracts strings from a CSV file to write into a template.
11 [codeblocks]
12 [gdscript]
13 @tool
14 extends EditorTranslationParserPlugin
15
16 func _parse_file(path):
17 var ret: Array[PackedStringArray] = []
18 var file = FileAccess.open(path, FileAccess.READ)
19 var text = file.get_as_text()
20 var split_strs = text.split(",", false)
21 for s in split_strs:
22 ret.append(PackedStringArray([s]))
23 #print("Extracted string: " + s)
24
25 return ret
26
27 func _get_recognized_extensions():
28 return ["csv"]
29 [/gdscript]
30 [csharp]
31 using Godot;
32
33 [Tool]
34 public partial class CustomParser : EditorTranslationParserPlugin
35 {
36 public override Godot.Collections.Array&lt;string[]&gt; _ParseFile(string path)
37 {
38 Godot.Collections.Array&lt;string[]&gt; ret;
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 ret.Add([s]);
45 //GD.Print($"Extracted string: {s}");
46 }
47 return ret;
48 }
49
50 public override string[] _GetRecognizedExtensions()
51 {
52 return ["csv"];
53 }
54 }
55 [/csharp]
56 [/codeblocks]
57 To add a translatable string associated with a context, plural, comment, or source line:
58 [codeblocks]
59 [gdscript]
60 # This will add a message with msgid "Test 1", msgctxt "context", msgid_plural "test 1 plurals", comment "test 1 comment", and source line "7".
61 ret.append(PackedStringArray(["Test 1", "context", "test 1 plurals", "test 1 comment", "7"]))
62 # This will add a message with msgid "A test without context" and msgid_plural "plurals".
63 ret.append(PackedStringArray(["A test without context", "", "plurals"]))
64 # This will add a message with msgid "Only with context" and msgctxt "a friendly context".
65 ret.append(PackedStringArray(["Only with context", "a friendly context"]))
66 [/gdscript]
67 [csharp]
68 // This will add a message with msgid "Test 1", msgctxt "context", msgid_plural "test 1 plurals", comment "test 1 comment", and source line "7".
69 ret.Add(["Test 1", "context", "test 1 plurals", "test 1 comment", "7"]);
70 // This will add a message with msgid "A test without context" and msgid_plural "plurals".
71 ret.Add(["A test without context", "", "plurals"]);
72 // This will add a message with msgid "Only with context" and msgctxt "a friendly context".
73 ret.Add(["Only with context", "a friendly context"]);
74 [/csharp]
75 [/codeblocks]
76 [b]Note:[/b] If you override parsing logic for standard script types (GDScript, C#, etc.), it would be better to load the [code]path[/code] argument using [method ResourceLoader.load]. This is because built-in scripts are loaded as [Resource] type, not [FileAccess] type. For example:
77 [codeblocks]
78 [gdscript]
79 func _parse_file(path):
80 var res = ResourceLoader.load(path, "Script")
81 var text = res.source_code
82 # Parsing logic.
83
84 func _get_recognized_extensions():
85 return ["gd"]
86 [/gdscript]
87 [csharp]
88 public override Godot.Collections.Array&lt;string[]&gt; _ParseFile(string path)
89 {
90 var res = ResourceLoader.Load&lt;Script&gt;(path, "Script");
91 string text = res.SourceCode;
92 // Parsing logic.
93 }
94
95 public override string[] _GetRecognizedExtensions()
96 {
97 return ["gd"];
98 }
99 [/csharp]
100 [/codeblocks]
101 Alternatively, the plugin can directly modify the final list of strings, by implementing [method _customize_strings].
102 To use [EditorTranslationParserPlugin], register it using the [method EditorPlugin.add_translation_parser_plugin] method first.
103 </description>
104 <tutorials>
105 </tutorials>
106 <methods>
107 <method name="_customize_strings" qualifiers="virtual const">
108 <return type="PackedStringArray[]" />
109 <param index="0" name="strings" type="PackedStringArray[]" />
110 <description>
111 Called after parsing all files. You can modify the [param strings] array to add or remove entries from the final list of strings, then return it after modifications. Each entry is a [PackedStringArray] like explained in the [EditorTranslationParserPlugin]'s description.
112 [codeblock]
113 @tool
114 extends EditorTranslationParserPlugin
115
116 func _customize_strings(strings):
117 # Add new string.
118 strings.append(["Test 1", "context", "test 1 plurals", "test 1 comment"])
119
120 # Remove all strings that begin with $.
121 strings = strings.filter(func(s): return not s[0].begins_with("$"))
122
123 return strings
124 [/codeblock]
125 </description>
126 </method>
127 <method name="_get_recognized_extensions" qualifiers="virtual const">
128 <return type="PackedStringArray" />
129 <description>
130 Gets the list of file extensions to associate with this parser, e.g. [code]["csv"][/code].
131 </description>
132 </method>
133 <method name="_parse_file" qualifiers="virtual">
134 <return type="PackedStringArray[]" />
135 <param index="0" name="path" type="String" />
136 <description>
137 Override this method to define a custom parsing logic to extract the translatable strings.
138 </description>
139 </method>
140 </methods>
141</class>