OctoCore 0.0.1
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
namespace OctoCore.Localization
|
||||
{
|
||||
public enum OctoLanguages {
|
||||
English,
|
||||
French,
|
||||
//Spanish,
|
||||
//German,
|
||||
//Chinese,
|
||||
//Japanese,
|
||||
//Korean,
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f177e44a5bc17824ab1fa6e1d4b1158c
|
||||
@@ -0,0 +1,160 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace OctoCore.Localization
|
||||
{
|
||||
/// <summary>
|
||||
/// Localization module for managing text and audio languages.
|
||||
/// Handles language switching and provides localized text retrieval.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class OctoLocalization : IOctoModule
|
||||
{
|
||||
public OctoLanguages WrittenLanguage;
|
||||
public OctoLanguages AudioLanguage;
|
||||
|
||||
private Action OnLanguageWrittenChange;
|
||||
private Action OnLanguageAudioChange;
|
||||
|
||||
private static Dictionary<OctoLanguages, string> _languagesKey = new Dictionary<OctoLanguages, string>();
|
||||
|
||||
public void Init(OctoSettings settings)
|
||||
{
|
||||
InitLanguagesKey();
|
||||
|
||||
// Load written language
|
||||
|
||||
if (settings.Languages.SaveWritten)
|
||||
{
|
||||
object language;
|
||||
Enum.TryParse(typeof(OctoLanguages), OctoCore.Instance.Save.Get(settings.Languages.SaveWritten), true, out language);
|
||||
if (language == null)
|
||||
{
|
||||
language = settings.Languages.Default;
|
||||
Debug.Log($"[OctoLocalization] Default language settled: {language.ToString()}");
|
||||
}
|
||||
|
||||
OctoCore.Instance.Localization.SetLanguageWritten((OctoLanguages)language);
|
||||
}
|
||||
|
||||
// Load audio language
|
||||
|
||||
if (settings.Languages.SaveAudio)
|
||||
{
|
||||
object language;
|
||||
Enum.TryParse(typeof(OctoLanguages), OctoCore.Instance.Save.Get(settings.Languages.SaveAudio), true, out language);
|
||||
if (language == null)
|
||||
language = settings.Languages.Default;
|
||||
|
||||
OctoCore.Instance.Localization.SetLanguageAudio((OctoLanguages)language);
|
||||
}
|
||||
|
||||
// Callback OctoEvents
|
||||
|
||||
if (OctoEvents.Instance)
|
||||
OctoEvents.Instance.SetLocalizationInitialized();
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void OnSceneChanged(Scene current, Scene next)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void OnQuit()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void SetLanguageWritten(OctoLanguages language) { SetLanguage(language, false); }
|
||||
|
||||
public void SetLanguageAudio(OctoLanguages language) { SetLanguage(language, true); }
|
||||
|
||||
private void SetLanguage(OctoLanguages language, bool audio = false)
|
||||
{
|
||||
if (audio)
|
||||
{
|
||||
OctoCore.Instance.Localization.AudioLanguage = language;
|
||||
OctoCore.Instance.Save.Set(OctoCore.Instance.Settings.Languages.SaveAudio, language.ToString());
|
||||
OnLanguageAudioChange?.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
OctoCore.Instance.Localization.WrittenLanguage = language;
|
||||
OctoCore.Instance.Save.Set(OctoCore.Instance.Settings.Languages.SaveWritten, language.ToString());
|
||||
OnLanguageWrittenChange?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public void UnsubscribeOnLanguageWrittenChange(Action handler)
|
||||
{
|
||||
SubscribeOnLanguageWrittenChange(handler, false);
|
||||
}
|
||||
|
||||
public void SubscribeOnLanguageWrittenChange(Action handler, bool subscribe = true)
|
||||
{
|
||||
if (subscribe) OnLanguageWrittenChange += handler;
|
||||
else OnLanguageWrittenChange -= handler;
|
||||
}
|
||||
|
||||
private static void InitLanguagesKey()
|
||||
{
|
||||
foreach (OctoLanguages lang in Enum.GetValues(typeof(OctoLanguages)))
|
||||
_languagesKey[lang] = lang.ToString();
|
||||
}
|
||||
|
||||
public static string GetLocalizedText(OctoLocalizedTextData data, bool audio = false)
|
||||
{
|
||||
OctoLanguages language = 0;
|
||||
if (OctoCore.Instance)
|
||||
language = audio ? OctoCore.Instance.Localization.AudioLanguage : OctoCore.Instance.Localization.WrittenLanguage;
|
||||
|
||||
return GetLocalizedText(data, language);
|
||||
}
|
||||
|
||||
public static string GetLocalizedText(OctoLocalizedTextData data, OctoLanguages language)
|
||||
{
|
||||
if (data == null || data.Languages == null)
|
||||
{
|
||||
if (OctoCore.Instance.Development.Log)
|
||||
Debug.Log($"[OctoLocalization] Data is null");
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var dict = data.Languages.Dictionary;
|
||||
|
||||
if (_languagesKey.Count == 0 || !_languagesKey.ContainsKey(language))
|
||||
InitLanguagesKey();
|
||||
|
||||
if (dict != null)
|
||||
{
|
||||
if (dict.TryGetValue(_languagesKey[language], out string localizedText) && !string.IsNullOrEmpty(localizedText))
|
||||
{
|
||||
return localizedText;
|
||||
}
|
||||
|
||||
// Use default if language not found or empty
|
||||
|
||||
if (dict.TryGetValue(_languagesKey[OctoCore.Instance.Settings.Languages.Default], out string fallbackText) && !string.IsNullOrEmpty(fallbackText))
|
||||
{
|
||||
if (OctoCore.Instance.Development.Log)
|
||||
Debug.Log($"[OctoLocalization] Use default language for: {data.name}");
|
||||
|
||||
return fallbackText;
|
||||
}
|
||||
}
|
||||
|
||||
if (OctoCore.Instance.Development.Log)
|
||||
Debug.Log($"[OctoLocalization] Empty text for: {data.name}");
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 01370154729dc1344af3e501de37cdcb
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace OctoCore.Localization
|
||||
{
|
||||
public class OctoLocalizationProxy : MonoBehaviour
|
||||
{
|
||||
public void SetLanguageWritten(string language)
|
||||
{
|
||||
if (!OctoCore.Instance) return;
|
||||
|
||||
object value;
|
||||
Enum.TryParse(typeof(OctoLanguages), language, true, out value);
|
||||
if (value == null)
|
||||
value = OctoCore.Instance.Settings.Languages.Default;
|
||||
|
||||
OctoCore.Instance.Localization.SetLanguageWritten((OctoLanguages)value);
|
||||
}
|
||||
|
||||
public void SetLanguageAudio(string language)
|
||||
{
|
||||
if (!OctoCore.Instance) return;
|
||||
|
||||
object value;
|
||||
Enum.TryParse(typeof(OctoLanguages), language, true, out value);
|
||||
if (value == null)
|
||||
value = OctoCore.Instance.Settings.Languages.Default;
|
||||
|
||||
OctoCore.Instance.Localization.SetLanguageAudio((OctoLanguages)value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 25b90d3d1039b994aa7a8950d3a9b046
|
||||
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace OctoCore.Localization
|
||||
{
|
||||
public class OctoLocalizedText : MonoBehaviour
|
||||
{
|
||||
[SerializeField] OctoLocalizedTextData data;
|
||||
[SerializeField] Text text;
|
||||
[SerializeField] TextMeshPro textMeshPro;
|
||||
[SerializeField] TextMeshProUGUI textMeshProUGUI;
|
||||
|
||||
Action localize;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void OnValidate()
|
||||
{
|
||||
if (Application.isPlaying)
|
||||
return;
|
||||
|
||||
bool setDirty = false;
|
||||
if (!text && !textMeshPro && !textMeshProUGUI)
|
||||
{
|
||||
text = GetComponent<Text>();
|
||||
textMeshPro = GetComponent<TextMeshPro>();
|
||||
textMeshProUGUI = GetComponent<TextMeshProUGUI>();
|
||||
|
||||
if (text || textMeshPro || textMeshProUGUI)
|
||||
setDirty = true;
|
||||
}
|
||||
|
||||
if (text && text.transform != transform)
|
||||
{
|
||||
text = null;
|
||||
setDirty = true;
|
||||
}
|
||||
|
||||
if (textMeshPro && textMeshPro.transform != transform)
|
||||
{
|
||||
textMeshPro = null;
|
||||
setDirty = true;
|
||||
}
|
||||
|
||||
if (textMeshProUGUI && textMeshProUGUI.transform != transform)
|
||||
{
|
||||
textMeshProUGUI = null;
|
||||
setDirty = true;
|
||||
}
|
||||
|
||||
if (setDirty)
|
||||
UnityEditor.EditorUtility.SetDirty(this);
|
||||
|
||||
Localize();
|
||||
}
|
||||
#endif
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (!OctoCore.Instance)
|
||||
{
|
||||
Debug.LogWarning($"[OctoLocalizedText] OctoCore.Instance not found");
|
||||
return;
|
||||
}
|
||||
|
||||
Localize();
|
||||
OctoCore.Instance.Localization.SubscribeOnLanguageWrittenChange(localize = Localize);
|
||||
|
||||
if (OctoCore.Instance.Development.Log && data)
|
||||
Debug.Log($"{name} subscribed on language written change");
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (!OctoCore.Instance) return;
|
||||
|
||||
if (localize != null)
|
||||
{
|
||||
OctoCore.Instance.Localization.UnsubscribeOnLanguageWrittenChange(localize);
|
||||
localize = null;
|
||||
|
||||
if (OctoCore.Instance.Development.Log && data)
|
||||
Debug.Log($"{name} unsubscribed on language written change");
|
||||
}
|
||||
}
|
||||
|
||||
public void Localize()
|
||||
{
|
||||
if (!data) return;
|
||||
|
||||
string localizedText = OctoLocalization.GetLocalizedText(data);
|
||||
|
||||
if (text)
|
||||
{
|
||||
text.text = localizedText;
|
||||
}
|
||||
else if (textMeshPro)
|
||||
{
|
||||
textMeshPro.text = localizedText;
|
||||
}
|
||||
else if (textMeshProUGUI)
|
||||
{
|
||||
textMeshProUGUI.text = localizedText;
|
||||
//LayoutRebuilder.ForceRebuildLayoutImmediate(textMeshProUGUI.rectTransform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1023fdbb836c50f48bd4b52575ed3689
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using OctoCore.Save;
|
||||
|
||||
namespace OctoCore.Localization
|
||||
{
|
||||
[CreateAssetMenu(fileName = "text_", menuName = "Octo/Localization/Text", order = 0)]
|
||||
public class OctoLocalizedTextData : ScriptableObject
|
||||
{
|
||||
public OctoSerializableDictionary<string, string> Languages = new OctoSerializableDictionary<string, string>();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void OnValidate()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
bool modified = false;
|
||||
var oldValues = new Dictionary<string, string>(Languages.Dictionary);
|
||||
|
||||
var newDict = new Dictionary<string, string>();
|
||||
|
||||
foreach (string lang in Enum.GetNames(typeof(OctoLanguages)))
|
||||
{
|
||||
if (oldValues.TryGetValue(lang, out string oldText))
|
||||
{
|
||||
newDict[lang] = oldText;
|
||||
}
|
||||
else
|
||||
{
|
||||
newDict[lang] = string.Empty;
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
Languages.Dictionary.Clear();
|
||||
foreach (var kvp in newDict)
|
||||
Languages.Dictionary[kvp.Key] = kvp.Value;
|
||||
|
||||
if (modified)
|
||||
UnityEditor.EditorUtility.SetDirty(this);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cbde7b14ca8fbf24c917c47aa3c51676
|
||||
Reference in New Issue
Block a user