Language And Translation Tools

Project DVN

This document describes how to use multiple languages, as well translation tools with DVN.

DVN supports dom parsing, json parsing, CSV and markdown, which makes it ideal to handle translation documents directly without having to convert between data formats.

Contents


Multiple Language Support

DVN supports multiple languages out of the box.

Simple specify your languages in settings.json like below:

"languages": {
    "LANGUAGEKEY": "LANGUAGENAME/TEXT"
}

Example:

"languages": {
    "EN": "English",
    "JP": "日本語"
}

There are some things to note tho when you enable multiple languages.

Scripts no longer live in data/game/scripts, they now live in data/game/scripts_LANGUAGEKEY, ex. data/game/scripts_JP

Bundling will no longer produce data/scripts.dvn it now produces a bundle for each language ex. data/scripts.JP.dvn

When you enable multiple languages in DVN then it will automatically prompt the user to select a language the first time they run the game. Once the user has selected a language then they will no longer be prompted.

Selecting a language sets the language value in settings.json.

Example:

"language": "JP"

Using the built-in DOM parser

DVN includes a full DOM parser capable of reading custom markup, XML and HTML-like documents. You can use CSS selectors to extract translation keys, text blocks, tags, or language variations directly from structured files.

Please see the previous article (DOM, HTML & Markdown) for an example.

Translation workflow

This section explains how to extract original text from scripts, prepare it for translators, and re-import translated versions back into a project.

Exporting Scripts (Prepare for translations)

To export scripts we need to grab all scene graph nodes and simply create a json file with the text in it.

First we need this type:

public final class Translation
{
    public:
    final:
    string from;
    string to;
    bool isOption; // Perhaps useful for the translators in how the translated text is expressed.
}

Then we need to extract the scene graph and all its nodes:

public override void loadedGameScripts(SceneEntry[string] scenes, SaveFile saveFile)
{
    Translation[] translations;

    foreach (k,scene; scenes)
    {
        if (scene.text && scene.text.length)
        {
            auto translation = new Translation;
            translation.from = scene.text;
            translation.to = "";

            translations ~= translation;
        }
        
        if (scene.options && scene.options.length)
        {
            foreach (option; scene.options)
            {
                auto translation = new Translation;
                translation.from = option.text;
                translation.to = "";
                translation.isOption = true;

                translations ~= translation;
            }
        }
    }
}

Then we need to serialize it as a json string.

string serializedJson;
if (!serializeJsonSafe(translations, serializedJson, true))
{
    return;
}

And at last we can write the json to a file.

import std.file : write;

write("translations.json", serializedJson);

Importing Translations

Instead of replacing the whole script with the translation, we can simply import the translations and do it at runtime:

First we will import all translations when the engine is ready.

string[string] translationMap;

public override void engineReady(Application app, Window[] windows)
{
    import std.file : readText,exists;

    if (!exists("translations.json")) return;

    string text = readText("translations.json");

    string[] errorMessages;
    Translation[] translations;
    if (!deserializeJsonSafe!(Translation[])(text, translations, errorMessages))
    {
        throw new Exception(errorMessages[0]);
    }

    foreach (translation; translations)
    {
        translationMap[translation.from] = translation.to;
    }
}

Next up we will simply replace every scene graph node's text.

public override void loadedGameScripts(SceneEntry[string] scenes, SaveFile saveFile)
{
    if (!translationMap) return;

    foreach (k,scene; scenes)
    {
        if (scene.text && scene.text.length)
        {
            scene.text = translationMap.get(scene.text, scene.text);
        }

        if (scene.options && scene.options.length)
        {
            foreach (option; scene.options)
            {
                option.text = translationMap.get(option.text, option.text);
            }
        }
    }
}

With all the above your application should automatically translate scenes.

Conclusion

DVN treats translations as data - not a plugin - making localization as flexible as the rest of the engine.