aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Lean/Localization/Scripts/LeanPhrase.cs
blob: 9c329e473f90dbdd991009c5a56523ce2092fbc5 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using UnityEngine;
using System.Collections.Generic;
using Lean.Common;
#if UNITY_EDITOR
using UnityEditor;

namespace Lean.Localization
{
	[CustomEditor(typeof(LeanPhrase))]
	public class LeanPhrase_Inspector : LeanInspector<LeanPhrase>
	{
		private static List<string> languageNames = new List<string>();

		private static List<LeanPhrase.Entry> entries = new List<LeanPhrase.Entry>();

		protected override void DrawInspector()
		{
			entries.Clear();
			entries.AddRange(Target.Entries);

			languageNames.Clear();
			languageNames.AddRange(LeanLocalization.CurrentLanguages.Keys);

			Target.Data = (LeanPhrase.DataType)GUILayout.Toolbar((int)Target.Data, new string[] { "Text", "Object", "Sprite" });

			EditorGUILayout.Separator();

			foreach (var languageName in languageNames)
			{
				var entry = default(LeanPhrase.Entry);

				if (Target.TryFindTranslation(languageName, ref entry) == true)
				{
					DrawEntry(entry, false);

					entries.Remove(entry);
				}
				else
				{
					EditorGUILayout.BeginHorizontal();
						EditorGUILayout.LabelField(languageName, EditorStyles.boldLabel);
						if (GUILayout.Button("Create", EditorStyles.miniButton, GUILayout.Width(45.0f)) == true)
						{
							Undo.RecordObject(Target, "Create Translation");

							Target.AddEntry(languageName);

							Dirty();
						}
					EditorGUILayout.EndHorizontal();
				}

				EditorGUILayout.Separator();
			}

			if (entries.Count > 0)
			{
				foreach (var entry in entries)
				{
					DrawEntry(entry, true);
				}
			}
		}

		private void DrawEntry(LeanPhrase.Entry entry, bool unexpected)
		{
			EditorGUILayout.BeginHorizontal();
				EditorGUILayout.LabelField(entry.Language, EditorStyles.boldLabel);
				if (GUILayout.Button("Remove", EditorStyles.miniButton, GUILayout.Width(55.0f)) == true)
				{
					Undo.RecordObject(Target, "Remove Translation");

					Target.RemoveTranslation(entry.Language);

					Dirty();
				}
			EditorGUILayout.EndHorizontal();

			if (unexpected == true)
			{
				EditorGUILayout.HelpBox("Your LeanLocalization component doesn't define the " + entry.Language + " language.", MessageType.Warning);
			}

			Undo.RecordObject(Target, "Modified Translation");

			EditorGUI.BeginChangeCheck();
			
			switch (Target.Data)
			{
				case LeanPhrase.DataType.Text:
					entry.Text = EditorGUILayout.TextArea(entry.Text ?? "", GUILayout.MinHeight(40.0f));
				break;
				case LeanPhrase.DataType.Object:
					entry.Object = EditorGUILayout.ObjectField(entry.Object, typeof(Object), true);
				break;
				case LeanPhrase.DataType.Sprite:
					entry.Object = EditorGUILayout.ObjectField(entry.Object, typeof(Sprite), true);
				break;
			}

			if (EditorGUI.EndChangeCheck() == true)
			{
				Dirty(); LeanLocalization.UpdateTranslations();
			}

			EditorGUILayout.Separator();
		}

		[MenuItem("Assets/Create/Lean/Localization/Lean Phrase")]
		private static void CreatePhrase()
		{
			var instance = new GameObject("New Phrase").AddComponent<LeanPhrase>();
			var path     = AssetDatabase.GetAssetPath(Selection.activeObject);

			if (string.IsNullOrEmpty(path) == true)
			{
				path = "Assets";
			}

			path = AssetDatabase.GenerateUniqueAssetPath(path + "/New Phrase.asset");

			AssetDatabase.CreateAsset(instance, path);

			Selection.activeObject = instance;
		}
	}
}
#endif

namespace Lean.Localization
{
	/// <summary>This contains data about each phrase, which is then translated into different languages.</summary>
	[ExecuteInEditMode]
	[DisallowMultipleComponent]
	[HelpURL(LeanLocalization.HelpUrlPrefix + "LeanPhrase")]
	[AddComponentMenu(LeanLocalization.ComponentPathPrefix + "Phrase")]
	public class LeanPhrase : LeanSource
	{
		public enum DataType
		{
			Text,
			Object,
			Sprite
		}

		[System.Serializable]
		public class Entry
		{
			/// <summary>The language of this translation.</summary>
			public string Language;

			/// <summary>The translated text.</summary>
			public string Text;

			/// <summary>The translated object (e.g. language specific texture).</summary>
			public Object Object;
		}

		public DataType Data { set { data = value; } get { return data; } } [SerializeField] private DataType data;

		/// <summary>This list stores all translations of this phrase in each language.</summary>
		[SerializeField]
		[UnityEngine.Serialization.FormerlySerializedAs("translations")]
		private List<Entry> entries;

		public List<Entry> Entries
		{
			get
			{
				if (entries == null)
				{
					entries = new List<Entry>();
				}

				return entries;
			}
		}

		public void Clear()
		{
			if (entries != null)
			{
				entries.Clear();
			}
		}

		public override void Compile(string primaryLanguage, string secondaryLanguage)
		{
			var translation = LeanLocalization.RegisterTranslation(name);

			if (entries != null)
			{
				for (var i = entries.Count - 1; i >= 0; i--)
				{
					var entry = entries[i];

					translation.Register(entry.Language, this);

					if (entry.Language == primaryLanguage)
					{
						Compile(translation, entry, true);
					}
					else if (entry.Language == secondaryLanguage && translation.Primary == false)
					{
						Compile(translation, entry, false);
					}
				}
			}
		}

		private void Compile(LeanTranslation translation, Entry entry, bool primary)
		{
			switch (data)
			{
				case DataType.Text:
				{
					Compile(translation, entry.Text, primary);
				}
				break;
				case DataType.Object:
				case DataType.Sprite:
				{
					Compile(translation, entry.Object, primary);
				}
				break;
			}
		}

		private void Compile(LeanTranslation translation, object data, bool primary)
		{
			translation.Data = data;

			if (primary == true)
			{
				translation.Primary = true;
			}
		}

		/// <summary>This will return the translation of this phrase for the specified language.</summary>
		public bool TryFindTranslation(string languageName, ref Entry entry)
		{
			if (entries != null)
			{
				for (var i = entries.Count - 1; i >= 0; i--)
				{
					entry = entries[i];

					if (entry.Language == languageName)
					{
						return true;
					}
				}
			}

			return false;
		}

		public void RemoveTranslation(string languageName)
		{
			if (entries != null)
			{
				for (var i = entries.Count - 1; i >= 0; i--)
				{
					if (entries[i].Language == languageName)
					{
						entries.RemoveAt(i);

						return;
					}
				}
			}
		}

		/// <summary>Add a new translation to this phrase for the specified language, or return the current one.</summary>
		public Entry AddEntry(string languageName, string text = null, Object obj = null)
		{
			var translation = default(Entry);

			if (TryFindTranslation(languageName, ref translation) == false)
			{
				translation = new Entry();

				translation.Language = languageName;

				if (entries == null)
				{
					entries = new List<Entry>();
				}

				entries.Add(translation);
			}

			translation.Text   = text;
			translation.Object = obj;

			return translation;
		}
	}
}