BITKit/Src/Unity/Scripts/Config/TranslateSO.cs

59 lines
1.7 KiB
C#
Raw Normal View History

2024-03-31 23:31:00 +08:00
using System;
2023-06-05 19:57:17 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2024-03-31 23:31:00 +08:00
#if RH_SerializedDictionary
2023-06-05 19:57:17 +08:00
using RotaryHeart.Lib.SerializableDictionary;
2024-03-31 23:31:00 +08:00
#endif
using AYellowpaper.SerializedCollections;
2023-06-05 19:57:17 +08:00
namespace BITKit
{
public sealed class TranslateSO : ScriptableObject
{
2024-03-31 23:31:00 +08:00
#if RH_SerializedDictionary
2023-06-05 19:57:17 +08:00
public SerializableDictionaryBase<string, string> dictionary = new();
2024-03-31 23:31:00 +08:00
#else
public SerializedDictionary<string, string> dictionary = new();
#endif
2023-06-05 19:57:17 +08:00
public string Get(string text, bool isLocalization = true)
{
foreach (var word in dictionary)
{
var value = word.Value;
if (isLocalization)
{
2024-03-31 23:31:00 +08:00
try
{
value = DI.Get<ITranslator>().Translate(value);
}
catch (NullReferenceException){}
2023-06-05 19:57:17 +08:00
}
text = text.Replace(word.Key, value);
}
return text;
}
public string GetAt(string key, bool isLocalization = true)
{
2024-03-31 23:31:00 +08:00
2023-06-05 19:57:17 +08:00
if (dictionary.TryGetValue(key, out var value))
{
2024-03-31 23:31:00 +08:00
try
{
if (isLocalization)
{
var translater = DI.Get<ITranslator>();
value = translater.Translate(value);
}
}
catch (NullReferenceException){}
2023-06-05 19:57:17 +08:00
return value;
}
else
{
/* if (isLocalization)
value = translater.Translate(value); */
return key;
}
}
}
}