How do I output translation messages in C#?

Godot Version

4

Question

The title says it all. I’ve looked into the documentation (Internationalizing games — Godot Engine (stable) documentation in English) but there’s no code samples for C#.

I’ve imported a spreadsheet with a simple language string:

keys,en
engineer_encounter_1_1,Hello traveler

I’ve tried instantiating a Translation object and using the GetMessage() method, but to no avail. Running out of ideas at this point.

public partial class DialogEngineerFirstEncounter
    {
        private readonly Translation _translation = new();

        public DialogEngineerFirstEncounter()
        {
            TranslationServer.SetLocale("en");
        }

        public List<DialogItem> GetDialogItems()
        {
            return new() {
                new DialogItem() {
                    Type = "speech",
                    Text = _translation.GetMessage("engineer_encounter_1_1"),
                },
            };
        }
    }

is getting new and blank?
your keys automatically translated in any label or other text related nodes.
if you needed get translated text in code i believe you can use
TranslationServer.Translate("KEY");

I found out that the Tr function is available in GodotObject.

For anyone that may have the same question as me, here’s what I did to make the function available:

    public partial class DialogEngineerFirstEncounter : GodotObject
    {
        private readonly List<DialogItem> _dialogItems;

        public DialogEngineerFirstEncounter()
        {
            _dialogItems = new() {
                new DialogItem() {
                    Type = "speech",
                    Text = Tr("engineer_encounter_1_1"),
                }
            };
        }
    }

By extetnding my class from GodotObject, the Tr() function will be available as a public function. You could create your own class and expose a static function that uses Tr, but for my case it wasn’t necessary.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.