From 85553832ead1a96f88726cd2b35cb6ff1d8b8cc8 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Sat, 24 Aug 2019 15:24:57 -0400 Subject: Attempt number 2 on localization --- .../Localization/Scripts/LeanLocalizedBehaviour.cs | 121 +++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 Assets/Packages/Lean/Localization/Scripts/LeanLocalizedBehaviour.cs (limited to 'Assets/Packages/Lean/Localization/Scripts/LeanLocalizedBehaviour.cs') diff --git a/Assets/Packages/Lean/Localization/Scripts/LeanLocalizedBehaviour.cs b/Assets/Packages/Lean/Localization/Scripts/LeanLocalizedBehaviour.cs new file mode 100644 index 0000000..4163464 --- /dev/null +++ b/Assets/Packages/Lean/Localization/Scripts/LeanLocalizedBehaviour.cs @@ -0,0 +1,121 @@ +using UnityEngine; +using UnityEngine.Serialization; +using System.Collections.Generic; +using Lean.Common; +#if UNITY_EDITOR +using UnityEditor; + +namespace Lean.Localization +{ + [CanEditMultipleObjects] + [CustomEditor(typeof(LeanLocalizedBehaviour), true)] + public class LeanLocalizedBehaviour_Inspector : LeanInspector + { + } +} +#endif + +namespace Lean.Localization +{ + /// This component simplifies the updating process, extend it if you want to cause a specific object to get localized + public abstract class LeanLocalizedBehaviour : MonoBehaviour + { + [Tooltip("The name of the phrase we want to use for this localized component")] + [SerializeField] + [LeanTranslationName] + [FormerlySerializedAs("phraseName")] + [FormerlySerializedAs("translationTitle")] + private string translationName; + + [System.NonSerialized] + private HashSet tokens; + + /// This is the name of the translation this script uses. + public string TranslationName + { + set + { + if (translationName != value) + { + translationName = value; + + UpdateLocalization(); + } + } + + get + { + return translationName; + } + } + + public void Register(LeanToken token) + { + if (token != null) + { + if (tokens == null) + { + tokens = new HashSet(); + } + + tokens.Add(token); + } + } + + public void Unregister(LeanToken token) + { + if (tokens != null) + { + tokens.Remove(token); + } + } + + public void UnregisterAll() + { + if (tokens != null) + { + foreach (var token in tokens) + { + token.Unregister(this); + } + + tokens.Clear(); + } + } + + // This gets called every time the translation needs updating + // NOTE: translation may be null if it can't be found + public abstract void UpdateTranslation(LeanTranslation translation); + + /// If you call this then this component will update using the translation for the specified phrase. + [ContextMenu("Update Localization")] + public void UpdateLocalization() + { + UpdateTranslation(LeanLocalization.GetTranslation(translationName)); + } + + protected virtual void OnEnable() + { + LeanLocalization.OnLocalizationChanged += UpdateLocalization; + + UpdateLocalization(); + } + + protected virtual void OnDisable() + { + LeanLocalization.OnLocalizationChanged -= UpdateLocalization; + + UnregisterAll(); + } + +#if UNITY_EDITOR + protected virtual void OnValidate() + { + if (isActiveAndEnabled == true) + { + UpdateLocalization(); + } + } +#endif + } +} \ No newline at end of file -- cgit v1.2.3