EditorTranslationParserPlugin

package
v0.0.0-...-357ca8a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 23 Imported by: 0

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

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 Any

type Any interface {
	gd.IsClass
	AsEditorTranslationParserPlugin() Instance
}

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

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 (self *Extension[T]) AsEditorTranslationParserPlugin() Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsRefCounted

func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted

type ID

type ID Object.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.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Implementation

type Implementation = implementation

Implementation implements Interface with empty methods.

type Instance

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 New

func New() Instance

func (Instance) AsEditorTranslationParserPlugin

func (self Instance) AsEditorTranslationParserPlugin() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) ID

func (self Instance) ID() ID

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

type Interface

type Interface interface {
	// Override this method to define a custom parsing logic to extract the translatable strings.
	ParseFile(path string) [][]string
	// Gets the list of file extensions to associate with this parser, e.g. ["csv"].
	GetRecognizedExtensions() []string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL