Documentation
¶
Overview ¶
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 ParseFile method in script.
The return value should be an slice of []strings, 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.
The extracted strings will be written into a POT file selected by user under "POT Generation" in "Localization" tab in "Project Settings" menu.
Below shows an example of a custom parser that extracts strings from a CSV file to write into a POT.
package main import ( "strings" "graphics.gd/classdb/EditorTranslationParserPlugin" "graphics.gd/classdb/FileAccess" ) type CustomParser struct { EditorTranslationParserPlugin.Extension[CustomParser] } func (c *CustomParser) ParseFile(path string) [][]string { var ret [][]string var file = FileAccess.Open(path, FileAccess.Read) var text = file.GetAsText() for s := range strings.SplitSeq(text, ",") { ret = append(ret, []string{s}) } return ret } func (c *CustomParser) GetRecognizedExtensions() []string { return []string{"csv"} }
To add a translatable string associated with a context, plural, or comment:
package main func ExampleEditorTranslationParserPlugin() { ret := [][]string{} // This will add a message with msgid "Test 1", msgctxt "context", msgid_plural "test 1 plurals", and comment "test 1 comment". ret = append(ret, []string{"Test 1", "context", "test 1 plurals", "test 1 comment"}) // This will add a message with msgid "A test without context" and msgid_plural "plurals". ret = append(ret, []string{"A test without context", "", "plurals"}) // This will add a message with msgid "Only with context" and msgctxt "a friendly context". ret = append(ret, []string{"Only with context", "a friendly context"}) _ = ret }
Note: If you override parsing logic for standard script types (GDScript, C#, etc.), it would be better to load the path argument using ResourceLoader.Load. This is because built-in scripts are loaded as Resource type, not FileAccess type. For example:
package main import ( "graphics.gd/classdb/EditorTranslationParserPlugin" "graphics.gd/classdb/ResourceLoader" "graphics.gd/classdb/Script" "graphics.gd/variant/Object" ) type MyEditorTranslationParserPlugin struct { EditorTranslationParserPlugin.Extension[MyEditorTranslationParserPlugin] } func (m *MyEditorTranslationParserPlugin) ParseFile(path string) [][]string { var res = Object.To[Script.Instance](ResourceLoader.Load(path, "Script")) var text = res.SourceCode() // Parsing logic. _ = text return nil } func (m *MyEditorTranslationParserPlugin) GetRecognizedExtensions() []string { return []string{"gd"} }
To use EditorTranslationParserPlugin, register it using the EditorPlugin.AddTranslationParserPlugin method first.
Index ¶
- type Advanced
- type Any
- type Extension
- type ID
- type Implementation
- type Instance
- func (self Instance) AsEditorTranslationParserPlugin() Instance
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsRefCounted() [1]gd.RefCounted
- func (self Instance) ID() ID
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) Virtual(name string) reflect.Value
- type Interface
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Advanced ¶
type Advanced = class
Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.
type Extension ¶
Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this [Extension]See Interface for methods that can be overridden by T.
func (*Extension[T]) AsEditorTranslationParserPlugin ¶
func (*Extension[T]) AsRefCounted ¶
func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted
type ID ¶
ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.
type Implementation ¶
type Implementation = implementation
Implementation implements Interface with empty methods.
type Instance ¶
type Instance [1]gdclass.EditorTranslationParserPlugin
Instance of the class with convieniently typed arguments and results.
var Nil Instance
Nil is a nil/null instance of the class. Equivalent to the zero value.
func (Instance) AsEditorTranslationParserPlugin ¶
func (Instance) AsRefCounted ¶
func (self Instance) AsRefCounted() [1]gd.RefCounted