3using System.Collections.Generic;
13public class Localization_SOURCE_Window : EditorWindow
16 private static string locSelectedPath;
18 private static readonly
string locRegistryKey =
"LOCATION_MANAGER_LocManagPath";
21 private static readonly
string locHeadingFormat =
"Localization_Manager_Source";
24 public static string locCurrentLanguage;
27 private static bool locManagerSelected =
true;
30 private static int locCategorySelected;
31 public static List<string> locAvailableCategories =
new List<string>();
32 private static string locSelectedCategoryName;
34 public class LocalizationElemenets
37 [Multiline]
public string Text;
41 public static List<LocalizationElemenets> localizationElements =
new List<LocalizationElemenets>();
43 public static bool loc_WindowInitialized;
44 private static bool loc_ReadySteady;
49 public static void Init()
51 if (loc_WindowInitialized)
54 loc_WindowInitialized =
true;
56 localizationElements.Clear();
57 Loc_GetLocalizationManagerPath();
60 [MenuItem(
"Window/Localization Manager")]
61 public static void InitWindow()
63 var win =
new Localization_SOURCE_Window();
64 win.minSize =
new Vector2(400, 250);
65 win.titleContent =
new GUIContent(
"Localization Editor");
68 localizationElements.Clear();
69 Loc_GetLocalizationManagerPath();
78 public static void Loc_GetLocalizationManagerPath()
80 locCurrentLanguage =
"";
82 locSelectedPath = PlayerPrefs.GetString(locRegistryKey);
83 loc_ReadySteady = !
string.IsNullOrEmpty(locSelectedPath);
85 if (!loc_ReadySteady)
return;
87 loc_ReadySteady =
true;
88 locManagerSelected =
true;
93 private static void Loc_SelectLanguageFile()
95 Loc_SaveDatabase(locSelectedPath,
false);
97 var f = EditorUtility.OpenFilePanel(
"Select Language File Path", Application.dataPath,
"xml");
99 if (
string.IsNullOrEmpty(f))
return;
102 locManagerSelected =
false;
104 locCurrentLanguage = Path.GetFileNameWithoutExtension(locSelectedPath);
106 Loc_LoadDatabase(locSelectedPath,
false);
109 private static void Loc_CreateLanguageFile()
111 Loc_SaveDatabase(locSelectedPath,
false);
113 var f = EditorUtility.SaveFilePanel(
"Create Language File", Application.dataPath,
"English",
"xml");
115 if (
string.IsNullOrEmpty(f))
return;
117 File.Create(f).Dispose();
120 locManagerSelected =
false;
122 locCurrentLanguage = Path.GetFileNameWithoutExtension(locSelectedPath);
124 Loc_LoadDatabase(locSelectedPath,
false);
126 AssetDatabase.Refresh();
132 public static void Loc_RefreshContent()
134 locAvailableCategories.Clear();
135 locAvailableCategories.Add(
"Default");
136 localizationElements.Clear();
138 if (!
File.Exists(locSelectedPath))
140 Loc_ErrorDebug(
"The selected file path '" + locSelectedPath +
"' doesn't exist!");
141 PlayerPrefs.DeleteKey(locRegistryKey);
142 loc_ReadySteady =
false;
146 var allLines =
File.ReadAllLines(locSelectedPath);
147 if (allLines.Length <= 1)
151 for (var i = 1; i < allLines.Length; i++)
153 var currentLine = allLines[i];
154 if (currentLine.Length <= 1)
continue;
156 locAvailableCategories.Add(currentLine.Trim().Remove(0, 1));
160 var currentCategory = 0;
161 for (var i = 1; i < allLines.Length; i++)
163 var currentLine = allLines[i];
164 if (currentLine.Length <= 1)
continue;
174 if (!locManagerSelected &&
177 var locElement =
new LocalizationElemenets();
178 var keySrc = locManagerSelected
181 locElement.Key = keySrc;
182 if (!locManagerSelected)
184 var keyText = currentLine.Substring(keySrc.Length + 1, currentLine.Length - keySrc.Length - 1);
188 locElement.Category = currentCategory;
191 localizationElements.Add(locElement);
198 public static void Loc_SaveDatabase(
string ToPath,
bool RefreshData =
true)
200 if (!
File.Exists(ToPath))
202 Loc_ErrorDebug(
"The file path " + ToPath +
" doesn't exist!");
206 File.WriteAllText(ToPath, locHeadingFormat);
208 var fstream =
new FileStream(ToPath,
FileMode.Append);
209 var fwriter =
new StreamWriter(fstream);
211 fwriter.WriteLine(
"");
213 foreach (var category
in locAvailableCategories)
216 if (category !=
"Default")
220 foreach (var locElement
in localizationElements)
223 if (category != locAvailableCategories[locElement.Category])
continue;
224 if (
string.IsNullOrEmpty(locElement.Key))
continue;
231 Loc_ErrorDebug(
"key '" + locElement.Key +
232 "' contains Category Delimiter or Key Delimiter. Please remove these characters from the key... Saving process was terminated");
237 if (locManagerSelected)
239 fwriter.WriteLine(locElement.Key);
243 var sb =
new StringBuilder(locElement.Text);
255 AssetDatabase.Refresh();
257 if (RefreshData) Loc_RefreshContent();
263 public static void Loc_LoadDatabase(
string FromPath,
bool RefreshData =
true)
265 if (!
File.Exists(FromPath))
267 Loc_ErrorDebug(
"The file path " + FromPath +
" doesn't exist!");
271 if (
File.ReadAllLines(FromPath).Length > 1)
273 var storedFilelines =
new List<string>();
274 for (var i = 1; i <
File.ReadAllLines(FromPath).Length; i++)
275 storedFilelines.Add(
File.ReadAllLines(FromPath)[i]);
277 foreach (var categories
in locAvailableCategories)
278 foreach (var locArray
in localizationElements)
279 if (Loc_GetLocalizationCategory(categories) == locArray.Category)
280 foreach (var s
in storedFilelines)
282 if (
string.IsNullOrEmpty(s))
continue;
288 if (
string.IsNullOrEmpty(Key))
continue;
290 if (Key == locArray.Key)
292 if (s.Length < Key.Length + 1)
continue;
293 locArray.Text = s.Substring(Key.Length + 1, s.Length - Key.Length - 1)
300 if (RefreshData) Loc_RefreshContent();
306 private static int Loc_GetLocalizationCategory(
string entry)
309 foreach (var categ
in locAvailableCategories)
311 if (categ == entry)
return c;
318 private static Vector2 guiScrollHelper;
322 EditorGUI.indentLevel++;
325 pDrawLabel(
"Localization Manager by Matej Vanco",
true);
330 if (!loc_ReadySteady)
332 GUILayout.BeginVertical(
"Box");
334 EditorGUILayout.HelpBox(
335 "There is no Localization Manager file. To set up keys structure and language system, select or create a Localization Manager file.",
337 GUILayout.BeginHorizontal(
"Box");
338 if (GUILayout.Button(
"Select Localization Manager file"))
340 var f = EditorUtility.OpenFilePanel(
"Select Localization Manager file", Application.dataPath,
"txt");
341 if (
string.IsNullOrEmpty(f))
return;
343 PlayerPrefs.SetString(locRegistryKey, locSelectedPath);
344 Loc_MessageDebug(
"All set! The Localization Manager is now ready.");
345 Loc_GetLocalizationManagerPath();
349 if (GUILayout.Button(
"Create Localization Manager file"))
351 var f = EditorUtility.SaveFilePanel(
"Create Localization Manager file", Application.dataPath,
352 "LocalizationManager",
"txt");
353 if (
string.IsNullOrEmpty(f))
355 File.Create(f).Dispose();
357 PlayerPrefs.SetString(locRegistryKey, locSelectedPath);
358 Loc_MessageDebug(
"Great! The Localization Manager is now ready.");
359 Loc_SaveDatabase(locSelectedPath,
false);
360 Loc_GetLocalizationManagerPath();
364 GUILayout.EndHorizontal();
365 GUILayout.EndVertical();
372 #region SECTION__UPPER
374 GUILayout.BeginHorizontal(
"Box");
375 if (GUILayout.Button(
"Save System"))
376 Loc_SaveDatabase(locSelectedPath);
378 if (locManagerSelected)
379 if (GUILayout.Button(
"Reset Manager Path"))
380 if (EditorUtility.DisplayDialog(
"Question",
381 "You are about to reset the Localization Manager path... No file o file be removed, just the current Language Localization path will be removed from the registry. Are you sure to continue?",
384 PlayerPrefs.DeleteKey(locRegistryKey);
389 GUILayout.EndHorizontal();
394 if (!
string.IsNullOrEmpty(locCurrentLanguage))
396 locManagerSelected =
false;
397 lang = locCurrentLanguage;
401 locManagerSelected =
true;
402 lang =
"Language Manager";
405 GUILayout.BeginHorizontal(
"Box");
406 pDrawLabel(
"Selected: " + lang);
407 if (GUILayout.Button(
"Deselect Language"))
409 Loc_SaveDatabase(locSelectedPath,
false);
410 Loc_GetLocalizationManagerPath();
413 if (GUILayout.Button(
"Select Language"))
414 Loc_SelectLanguageFile();
416 if (GUILayout.Button(
"Create Language"))
417 Loc_CreateLanguageFile();
418 GUILayout.EndHorizontal();
424 GUILayout.BeginVertical(
"Box");
426 #region SECTION__CATEGORIES
428 GUILayout.BeginHorizontal(
"Box");
429 EditorGUIUtility.labelWidth -= 70;
430 locCategorySelected = EditorGUILayout.Popup(
"Category:", locCategorySelected, locAvailableCategories.ToArray(),
431 GUILayout.MaxWidth(300), GUILayout.MinWidth(150));
432 EditorGUIUtility.labelWidth += 70;
433 if (locManagerSelected)
436 locSelectedCategoryName = EditorGUILayout.TextField(locSelectedCategoryName);
437 if (GUILayout.Button(
"Add Category"))
439 if (
string.IsNullOrEmpty(locSelectedCategoryName))
441 Loc_ErrorDebug(
"Please fill the required field! [Category Name]");
445 locAvailableCategories.Add(locSelectedCategoryName);
446 locSelectedCategoryName =
"";
447 GUI.FocusControl(
"Set");
451 if (GUILayout.Button(
"Remove Category") && locAvailableCategories.Count > 1)
452 if (EditorUtility.DisplayDialog(
"Question",
"You are about to remove a category... Are you sure?",
455 if (
string.IsNullOrEmpty(locSelectedCategoryName))
457 locAvailableCategories.RemoveAt(locAvailableCategories.Count - 1);
458 locCategorySelected = 0;
464 foreach (var cat
in locAvailableCategories)
466 if (locSelectedCategoryName == cat)
468 locAvailableCategories.RemoveAt(cc);
469 locCategorySelected = 0;
477 if (notfound) Loc_ErrorDebug(
"The category couldn't be found.");
478 locSelectedCategoryName =
"";
485 GUILayout.EndHorizontal();
491 #region SECTION__LOCALIZATION_ARRAY
493 GUILayout.BeginHorizontal();
494 pDrawLabel(
"Localization Keys & Texts");
495 if (locManagerSelected && GUILayout.Button(
"+"))
496 localizationElements.Add(
new LocalizationElemenets {Category = locCategorySelected});
497 GUILayout.EndHorizontal();
499 if (localizationElements.Count == 0)
501 pDrawLabel(
" - - Empty! - -");
505 guiScrollHelper = EditorGUILayout.BeginScrollView(guiScrollHelper);
508 foreach (var locA
in localizationElements)
510 if (locA.Category >= locAvailableCategories.Count)
516 if (locAvailableCategories[locA.Category] != locAvailableCategories[locCategorySelected])
519 EditorGUIUtility.labelWidth -= 100;
520 EditorGUILayout.BeginHorizontal(
"Box");
521 if (!locManagerSelected)
523 EditorGUILayout.LabelField(locA.Key, GUILayout.Width(100));
525 EditorGUILayout.LabelField(
"Text:", GUILayout.Width(100));
526 locA.Text = EditorGUILayout.TextArea(locA.Text, GUILayout.MinWidth(100));
530 EditorGUILayout.LabelField(
"Key:", GUILayout.Width(45));
532 locA.Key = EditorGUILayout.TextField(locA.Key, GUILayout.MaxWidth(100), GUILayout.MinWidth(30));
533 EditorGUILayout.LabelField(
"Category:", GUILayout.Width(75));
534 locA.Category = EditorGUILayout.Popup(locA.Category, locAvailableCategories.ToArray());
535 if (GUILayout.Button(
"-", GUILayout.Width(30)))
537 localizationElements.Remove(locA);
542 EditorGUILayout.EndHorizontal();
543 EditorGUIUtility.labelWidth += 100;
547 EditorGUILayout.EndScrollView();
550 GUILayout.EndVertical();
554 EditorGUI.indentLevel--;
558 #region LayoutShortcuts
560 private void pDrawLabel(
string text,
bool bold =
false)
565 add += text +
"</b>";
569 var style =
new GUIStyle();
570 style.richText =
true;
571 style.normal.textColor =
Color.white;
572 EditorGUILayout.LabelField(text, style);
575 private void pDrawSpace(
float space = 10)
577 GUILayout.Space(space);
582 private static void Loc_ErrorDebug(
string msg)
584 EditorUtility.DisplayDialog(
"Error", msg,
"OK");
587 private static void Loc_MessageDebug(
string msg)
589 EditorUtility.DisplayDialog(
"Info", msg,
"OK");
System.IO.FileMode FileMode
static string GENERAL_NewLineSymbol
static string GENERAL_DelimiterSymbol_Key
static string GENERAL_DelimiterSymbol_Category