aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Lean/Localization/Scripts/LeanLocalizedBehaviour.cs
blob: 41634645a1440ce05c875950287fed2f522f4d49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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<LeanLocalizedBehaviour>
	{
	}
}
#endif

namespace Lean.Localization
{
	/// <summary>This component simplifies the updating process, extend it if you want to cause a specific object to get localized</summary>
	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<LeanToken> tokens;

		/// <summary>This is the name of the translation this script uses.</summary>
		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<LeanToken>();
				}

				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);

		/// <summary>If you call this then this component will update using the translation for the specified phrase.</summary>
		[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
	}
}