TouhouLS/TouhouStation/Assets/Code/Localization/Localization.cs

86 lines
2.2 KiB
C#
Raw Normal View History

2020-08-22 05:29:00 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public enum LocalizationLanguage { English, Japanese, Spanish };
public class Localization : ScriptableObject {
const string path = "Assets/Resources/Localization.asset";
public static LocalizationLanguage SelectedLanguage;
public static readonly int LanguageCount = System.Enum.GetNames(typeof(LocalizationLanguage)).Length;
public static readonly System.Array Langauges = typeof(LocalizationLanguage).GetEnumValues();
private static Localization _Instance;
public static Localization Instance{
get {
if (_Instance == null) {
#if UNITY_EDITOR
_Instance = AssetDatabase.LoadAssetAtPath<Localization>(path);
if (_Instance == null) {
_Instance = ScriptableObject.CreateInstance<Localization>();
AssetDatabase.CreateAsset(_Instance, path);
}
#else
_Instance = Resources.Load<Localization>("Localization");
#endif
}
return _Instance;
}
}
public static void RebuildKeyDictionary(){
if (keyDictionary == null) keyDictionary = new Dictionary<string, string>();
else keyDictionary.Clear();
var locals = Instance.GetLocals();
foreach (var l in locals){
keyDictionary.Add(l.key, l.text);
}
}
private static Dictionary<string, string> keyDictionary;
public static string GetString(string key){
if (keyDictionary == null){
RebuildKeyDictionary();
}
if (keyDictionary.ContainsKey(key)) return keyDictionary[key];
else return "E-R-R-O-R";
}
[System.Serializable]
public struct Local{
public string key;
public string text;
}
public Local[] englishLocals;
public Local[] japaneseLocals;
public Local[] spanishLocals;
public Local[] GetLocals(LocalizationLanguage language){
switch (language) {
case LocalizationLanguage.English:
return englishLocals;
case LocalizationLanguage.Japanese:
return japaneseLocals;
case LocalizationLanguage.Spanish:
return spanishLocals;
}
return englishLocals;
}
public Local[] GetLocals() => GetLocals(SelectedLanguage);
}