Localize variables inserted in dialogue

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Swing Wren
Posts: 31
Joined: Fri Apr 10, 2020 2:30 am

Localize variables inserted in dialogue

Post by Swing Wren »

Hello,

What would be the recommended way to localize variables inserted in dialogue?

Code: Select all

You make we laugh, your rank of [var=PlayerRank] is not enough to pass through the door
(PlayerRank may be things like student, master...)

Maybe at this point its wiser to just use custom tags and replace/translate the written text before its displayed? :|
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: Localize variables inserted in dialogue

Post by Tony Li »

Hi,

You could process the text yourself in an OnConversationLine method.

However, it might be easier to register a localization function with Lua and use a [lua(code)] markup tag. The example below adds a function LocalizeVar("variableName") that calls DialogueManager.GetLocalizedText(), which looks up the localized version of the variable value from the Text Table asset assigned to the Dialogue Manager's Display Settings > Localization Settings. You could change it to pull localizations from somewhere else such as i2 Localization or Unity's Localization package.

You'd use it like this:

Code: Select all

You make me laugh. Your rank of [lua(LocalizeVar("PlayerRank"))] is not enough to pass through the door

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

// Add to Dialogue Manager.
public class TestLocalize : MonoBehaviour
{
    private void Awake()
    {
        Lua.RegisterFunction(nameof(LocalizeVar), this, SymbolExtensions.GetMethodInfo(() => LocalizeVar(string.Empty)));
    }

    private string LocalizeVar(string variableName)
    {
        string baseValue = DialogueLua.GetVariable(variableName).asString;
        return DialogueManager.GetLocalizedText(baseValue);
    }
}
Post Reply