aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Lean/Localization/Scripts/LeanTranslation.cs
blob: 5fc5e185a773f03c364fdba92d53b4ec4fa22f05 (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
using UnityEngine;
using System.Collections.Generic;

namespace Lean.Localization
{
	/// <summary>This contains the translated value for the current language, and other associated data.</summary>
	public class LeanTranslation
	{
		public struct Entry
		{
			public string Language;

			public Object Owner;
		}

		/// <summary>The name of this translation.</summary>
		public string Name { get { return name; } } [SerializeField] private string name;

		/// <summary>The data of this translation (e.g. string or Object).
		/// NOTE: This is a System.Object, so you must correctly cast it back before use.</summary>
		public object Data;

		/// <summary>If Data has been filled with data for the primary language, then this will be set to true.</summary>
		public bool Primary;

		/// <summary>This stores a list of all LeanSource instances that are currently managing the current value of this translation in the current language.
		/// NOTE: If this is empty then no LeanSource of this name is localized for the current language.</summary>
		public List<Entry> Entries { get { return entries; } } private List<Entry> entries = new List<Entry>();

		private static bool buffering;

		private static System.Text.StringBuilder current = new System.Text.StringBuilder();

		private static System.Text.StringBuilder buffer = new System.Text.StringBuilder();

		private static List<LeanToken> tokens = new List<LeanToken>();

		public LeanTranslation(string newName)
		{
			name = newName;
		}

		public void Register(string language, Object owner)
		{
			var entry = new Entry();

			entry.Language = language;
			entry.Owner    = owner;

			entries.Add(entry);
		}

		public void Clear()
		{
			Data    = null;
			Primary = false;

			entries.Clear();
		}

		public int LanguageCount(string language)
		{
			var total = 0;

			for (var i = entries.Count - 1; i >= 0; i--)
			{
				if (entries[i].Language == language)
				{
					total += 1;
				}
			}

			return total;
		}

		/// <summary>This returns Text with all tokens substituted using the LeanLocalization.Tokens list.</summary>
		public static string FormatText(string rawText, string currentText, LeanLocalizedBehaviour behaviour)
		{
			if (string.IsNullOrEmpty(currentText) == true)
			{
				currentText = rawText;
			}

			if (rawText != null)
			{
				current.Length = 0;
				buffer.Length = 0;
				tokens.Clear();

				for (var i = 0; i < rawText.Length; i++)
				{
					var rawChar = rawText[i];

					if (rawChar == '{')
					{
						if (buffering == true)
						{
							buffering = false;

							buffer.Length = 0;
						}
						else
						{
							buffering = true;
						}
					}
					else if (rawChar == '}')
					{
						if (buffering == true)
						{
							if (buffer.Length > 0)
							{
								var token = default(LeanToken);

								if (buffer.Length > 0 && LeanLocalization.CurrentTokens.TryGetValue(buffer.ToString(), out token) == true) // TODO: Avoid ToString here?
								{
									current.Append(token.Value);

									tokens.Add(token);
								}
								else
								{
									current.Append('{').Append(buffer).Append('}');
								}

								buffer.Length = 0;
							}

							buffering = false;
						}
					}
					else
					{
						if (buffering == true)
						{
							buffer.Append(rawChar);
						}
						else
						{
							current.Append(rawChar);
						}
					}
				}

				if (Match(currentText, current) == false)
				{
					if (behaviour != null)
					{
						behaviour.UnregisterAll();
					}

					for (var i = tokens.Count - 1; i >= 0; i--)
					{
						var token = tokens[i];

						token.Register(behaviour);

						behaviour.Register(token);
					}

					return current.ToString();
				}
			}

			return currentText;
		}

		private static bool Match(string a, System.Text.StringBuilder b)
		{
			if (a == null && b.Length > 0)
			{
				return false;
			}

			if (a.Length != b.Length)
			{
				return false;
			}

			for (var i = 0; i < a.Length; i++)
			{
				if (a[i] != b[i])
				{
					return false;
				}
			}

			return true;
		}
	}
}