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 --- .../Lean/Localization/Scripts/LeanToken.cs | 130 +++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 Assets/Packages/Lean/Localization/Scripts/LeanToken.cs (limited to 'Assets/Packages/Lean/Localization/Scripts/LeanToken.cs') diff --git a/Assets/Packages/Lean/Localization/Scripts/LeanToken.cs b/Assets/Packages/Lean/Localization/Scripts/LeanToken.cs new file mode 100644 index 0000000..c6ca7f8 --- /dev/null +++ b/Assets/Packages/Lean/Localization/Scripts/LeanToken.cs @@ -0,0 +1,130 @@ +using UnityEngine; +using System.Collections.Generic; +using Lean.Common; +#if UNITY_EDITOR +using UnityEditor; + +namespace Lean.Localization +{ + [CustomEditor(typeof(LeanToken))] + public class LeanToken_Inspector : LeanInspector + { + protected override void DrawInspector() + { + Draw("value"); + } + } +} +#endif + +namespace Lean.Localization +{ + /// The class stores a token name (e.g. "AGE"), allowing it to be replaced with the token value (e.g. "20"). + /// To use the token in your text, simply include the token name surrounded by braces (e.g. "I am {AGE} years old!") + [ExecuteInEditMode] + [HelpURL(LeanLocalization.HelpUrlPrefix + "LeanToken")] + [AddComponentMenu(LeanLocalization.ComponentPathPrefix + "Token")] + public class LeanToken : LeanSource + { + [SerializeField] + private string value; + + [System.NonSerialized] + private HashSet behaviours; + + [System.NonSerialized] + private static HashSet tempBehaviours = new HashSet(); + + public string Value + { + set + { + if (this.value != value) + { + this.value = value; + + if (behaviours != null) + { + tempBehaviours.Clear(); + + tempBehaviours.UnionWith(behaviours); + + foreach (var behaviour in tempBehaviours) + { + behaviour.UpdateLocalization(); + } + } + } + } + + get + { + return value; + } + } + + public void SetValue(float value) + { + Value = value.ToString(); + } + + public void SetValue(string value) + { + Value = value; + } + + public void SetValue(int value) + { + Value = value.ToString(); + } + + public void Register(LeanLocalizedBehaviour behaviour) + { + if (behaviour != null) + { + if (behaviours == null) + { + behaviours = new HashSet(); + } + + behaviours.Add(behaviour); + } + } + + public void Unregister(LeanLocalizedBehaviour behaviour) + { + if (behaviours != null) + { + behaviours.Remove(behaviour); + } + } + + public void UnregisterAll() + { + if (behaviours != null) + { + foreach (var behaviour in behaviours) + { + behaviour.Unregister(this); + } + + behaviours.Clear(); + } + } + + public override void Compile(string primaryLanguage, string secondaryLanguage) + { + if (string.IsNullOrEmpty(name) == false) + { + LeanLocalization.CurrentTokens.Add(name, this); + } + } + + protected override void OnDisable() + { + base.OnDisable(); + + UnregisterAll(); + } + } +} \ No newline at end of file -- cgit v1.2.3