Auto language switching script

Using Naninovel C# APIs: adding custom actor implementations or commands, overriding engine services, integrating with other systems, etc.
Post Reply
Animal
Posts: 5
Joined: 14 Jul 2022 07:28

Auto language switching script

Post by Animal »

Auto language switching script from Adventure Creator to NaniNovel

Code: Select all

using System;
using UnityEngine;
using AC;
using Naninovel;

public class NaninovelLanguage : MonoBehaviour
{

    private void OnEnable()
    {
        EventManager.OnInitialiseScene += OnInitialiseScene;
        EventManager.OnChangeLanguage += OnChangeLanguage;
    }

    private void OnDisable()
    {
        EventManager.OnInitialiseScene += OnInitialiseScene;
        EventManager.OnChangeLanguage += OnChangeLanguage;
    }

    private void Awake()
    {
        if (!Engine.Initialized)
        {
            Engine.OnInitializationFinished += TrySetNaninovelLanguage;
        }
    }

    void OnInitialiseScene()
    {
        SetNaninovelLanguage(Options.GetLanguageName());
    }

    void OnChangeLanguage(int language)
    {
        SetNaninovelLanguage(Options.GetLanguageName());
    }

    private void TrySetNaninovelLanguage()
    {
        SetNaninovelLanguage(Options.GetLanguageName());
        Engine.OnInitializationFinished -= TrySetNaninovelLanguage;
    }

    private void SetNaninovelLanguage(string language)
    {
        if (!string.IsNullOrWhiteSpace(language) && Engine.Initialized)
        {
            var languageCode = Options.GetLanguageName().ToLower();
            if (languageCode == "russian")
            {
                languageCode = "ru";
            }
            else if (languageCode == "english")
            {
                languageCode = "en";
            }
            
            Debug.Log($"NN_Lang: {language}");
            Engine.GetService<ILocalizationManager>().SelectLocaleAsync(languageCode);
        }
    }

}
Post Reply