54 lines
1.6 KiB
C#
54 lines
1.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using RotaryHeart.Lib.SerializableDictionary;
|
|
using Newtonsoft.Json;
|
|
using System.Linq;
|
|
namespace BITKit
|
|
{
|
|
public class UnityTranslator : MonoBehaviour, ITranslator
|
|
{
|
|
public string langs = "zh-cns";
|
|
public SerializableDictionaryBase<string, TranslateSO> dictionary = new();
|
|
public event Action<string> OnChangeLanguage;
|
|
public event Func<string[]> OnCollageAllSourceText;
|
|
|
|
public string Translate(string rawString)
|
|
{
|
|
if (dictionary.TryGetValue(langs, out var so))
|
|
{
|
|
return so.Get(rawString, false);
|
|
}
|
|
return rawString;
|
|
}
|
|
public void Set(string value)
|
|
{
|
|
if (dictionary.ContainsKey(value))
|
|
{
|
|
langs = value;
|
|
BIT4Log.Log<UnityTranslator>($"已设置语言为:{value}");
|
|
OnChangeLanguage?.Invoke(value);
|
|
}
|
|
}
|
|
void Awake()
|
|
{
|
|
DI.Register<ITranslator>(this);
|
|
//GameObject.DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
public string[] GetAllSourceText()
|
|
{
|
|
var value = OnCollageAllSourceText.GetInvocationList()
|
|
.SelectMany(x => x.Method.Invoke(x.Target, null) as string[])
|
|
.ToArray();
|
|
return value;
|
|
}
|
|
public void LogAllSourceText()
|
|
{
|
|
BITAppForUnity.ThrowIfNotPlaying();
|
|
var json = JsonConvert.SerializeObject(GetAllSourceText(),Formatting.Indented);
|
|
Debug.Log(json);
|
|
}
|
|
}
|
|
} |