2using System.Collections.Generic;
13[AddComponentMenu(
"Matej Vanco/Language Localization")]
38 LocalizationComponent,
89 "If assignation type is set to GameObjectChild & the bool is set to True, the target text will be searched in the global Childs root object")]
92 [Space] [Tooltip(
"Allow UIText component?")]
97 [Space] [Tooltip(
"New specific UI Texts")]
103 "If enabled, the object fields above will be cleared if the QuickActions are applied to key in specific category")]
112 if (!Application.isPlaying)
return;
121 #region INTERNAL METHODS
125 internal void Internal_RefreshInternalLocalization()
128 Categories.AddRange(Localization_SOURCE_Window.locAvailableCategories);
131 internal void Internal_AddKey(
string KeyName)
133 foreach (var a
in Localization_SOURCE_Window.localizationElements)
134 if (a.Key == KeyName)
140 Category = a.Category,
141 AssignationType = _LocalizationSelector._AssignationType.AutoFindText
155 private string Internal_ConvertAndReturnText(_LocalizationSelector lSelector,
string[] lines)
157 if (lines.Length > 1)
159 var storedFilelines =
new List<string>();
160 for (var i = 1; i < lines.Length; i++)
161 storedFilelines.Add(lines[i]);
164 if (Internal_GetLocalizationCategory(categories) == lSelector.Category)
165 foreach (var s
in storedFilelines)
167 if (
string.IsNullOrEmpty(s))
172 if (del == 0 || del > s.Length)
continue;
174 var Key = s.Substring(0, del);
176 if (
string.IsNullOrEmpty(Key))
continue;
177 if (Key == lSelector.Key)
179 if (s.Length < Key.Length + 1)
181 lSelector.Text = s.Substring(Key.Length + 1, s.Length - Key.Length - 1);
182 return lSelector.Text;
190 internal int Internal_GetLocalizationCategory(
string entry)
195 if (categ == entry)
return c;
211 switch (sel.AssignationType)
214 var childName = sel.AT_ChildName;
215 if (sel.AT_FindChildByKeyName)
218 sel.AT_FoundObjects.Clear();
219 var root = sel.AT_UseGeneralChildsRootObject
221 : sel.AT_CustomChildsRootObject;
223 if (useThis) root = useThis.transform;
227 Debug.LogError(
"Localization: The key '" + sel.Key +
228 "' should have been assigned to specific childs by it's key name, but the root object is empty");
232 foreach (var t
in root.GetComponentsInChildren<Transform>(
true))
234 if (t.name != childName)
continue;
236 if (sel.AT_UITextComponentAllowed && t.GetComponent<Text>())
237 sel.AT_FoundObjects.Add(t.gameObject);
238 if (sel.AT_TextMeshComponentAllowed && t.GetComponent<TextMesh>())
239 sel.AT_FoundObjects.Add(t.gameObject);
245 sel.AT_FoundObjects.Clear();
246 foreach (var k
in FindObjectsOfType<Localization_KEY>())
247 if (k.KeyID == sel.Key)
248 sel.AT_FoundObjects.Add(k.gameObject);
251 sel.AT_FoundObjects.Clear();
252 foreach (var text
in sel.AT_UITextObject)
253 if (text.text == sel.Key)
254 sel.AT_FoundObjects.Add(text.gameObject);
257 sel.AT_FoundObjects.Clear();
260 var aa = FindTextRecursive(useThis, sel.Key);
261 sel.AT_FoundObjects.AddRange(aa);
266 sel.AT_FoundObjects.AddRange(texts);
268 sel.AT_FoundObjects.AddRange(FindTextRecursive(
ExtraCanvas1, sel.Key));
270 sel.AT_FoundObjects.AddRange(FindTextRecursive(
ExtraCanvas2, sel.Key));
272 sel.AT_FoundObjects.AddRange(FindTextRecursive(
ExtraCanvas3, sel.Key));
283 private GameObject[] FindTextRecursive(GameObject go,
string text)
285 var returnList =
new List<GameObject>();
286 foreach (Transform child
in go.transform)
288 var textcomp = child.GetComponent<Text>();
289 if (textcomp && textcomp.text == text) returnList.Add(child.gameObject);
291 if (tooltextcomp && tooltextcomp.tooltip == text) returnList.Add(child.gameObject);
293 if (tooltipcomp && tooltipcomp.newTooltip == text) returnList.Add(child.gameObject);
294 var ddown = child.GetComponent<Dropdown>();
296 foreach (var data
in ddown.options)
298 if (data.text != text)
continue;
299 returnList.Add(child.gameObject);
302 if (child.childCount > 0)
304 var retval = FindTextRecursive(child.gameObject, text);
305 if (retval !=
null && retval.Length > 0) returnList.AddRange(retval);
309 return returnList.ToArray();
319 Debug.LogError(
"Localization: The index for language selection is incorrect! Languages count: " +
326 Debug.LogError(
"Localization: The language that you've selected is empty!");
332 sel.Text = Internal_ConvertAndReturnText(sel,
LanguageFiles[languageIndex].text.Split(
'\n'))
335 switch (sel.AssignationType)
340 foreach (var gm
in sel.AT_FoundObjects)
341 if (gm.GetComponent<Text>() && sel.AT_UITextComponentAllowed)
343 gm.GetComponent<Text>().text = sel.Text;
345 else if (gm.GetComponent<TextMesh>() && sel.AT_TextMeshComponentAllowed)
347 gm.GetComponent<TextMesh>().text = sel.Text;
357 else if (gm.GetComponent<Dropdown>())
359 var dd = gm.GetComponent<Dropdown>();
360 foreach (var optionData
in dd.options)
362 if (optionData.text != sel.Key)
continue;
363 optionData.text = sel.Text;
370 foreach (var t
in sel.AT_TextMeshbject)
372 if (t ==
null)
continue;
379 foreach (var t
in sel.AT_UITextObject)
381 if (t ==
null)
continue;
388 foreach (var dyn
in sel.AT_UIObjects)
390 if (dyn is GameObject go)
392 var txt = go.GetComponent<Text>();
393 var ddown = go.GetComponent<Dropdown>();
396 if (txt.text != sel.Key)
continue;
401 foreach (var data
in ddown.options)
403 if (data.text != sel.Key)
continue;
404 data.text = sel.Text;
410 if (t.text != sel.Key)
continue;
414 if (dyn is Dropdown d)
415 foreach (var data
in d.options)
417 if (data.text != sel.Key)
continue;
418 data.text = sel.Text;
432 Debug.Log($
"Translation for '{key}' not found!");
442 if (l.Key == KeyInput)
444 Debug.Log(
"Localization: Key '" + KeyInput +
"' couldn't be found");
453 [ContextMenu(
"Load Language")]
bool AT_UITextComponentAllowed
bool AT_FindChildByKeyName
bool AT_UseGeneralChildsRootObject
TextMesh[] AT_TextMeshbject
Transform AT_CustomChildsRootObject
bool AT_TextMeshComponentAllowed
_AssignationType AssignationType
List< GameObject > AT_FoundObjects
TextMesh[] SpecificTextMeshes
bool ClearAllPreviousTargets
bool useGeneralChildsRoot
_LocalizationSelector._AssignationType assignationType
Transform AT_GameObjectChildsRoot
static string GENERAL_NewLineSymbol
static string GENERAL_DelimiterSymbol_Key
void Lang_RefreshKeyAssignations(GameObject useThis=null)
Refresh all resource objects by selected options
string TranslateText(string key)
void Lang_LoadLanguage(int languageIndex)
Load language database by the selected language index
TextAsset[] LanguageFiles
string Lang_ReturnText(string KeyInput)
Return exists text by the specific key input
static string GENERAL_DelimiterSymbol_Category
List< string > Categories
List< _LocalizationSelector > LocalizationSelector
QuickActions quickActions
static Localization_SOURCE Instance
void ReloadLanguage(GameObject onlyThis=null)