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);
}
}
}