Skip to content

Commit 9a0527c

Browse files
committed
add: added base window tools localization
1 parent a4dc5de commit 9a0527c

9 files changed

Lines changed: 200 additions & 56 deletions

File tree

Assets/Code/Editor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#if UNITY_EDITOR
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using Sirenix.OdinInspector;
6+
using Sirenix.OdinInspector.Editor;
7+
using UnityEditor;
8+
using UnityEngine;
9+
10+
namespace Localization.Editor
11+
{
12+
public class LocalizationEditorWindow : OdinEditorWindow
13+
{
14+
private const string LocalizationFolder = "Assets/Code/Localization/Intro/Resources/Localization";
15+
16+
[MenuItem("Tools/Localization Editor 🈺")]
17+
private static void OpenWindow()
18+
{
19+
var window = GetWindow<LocalizationEditorWindow>();
20+
window.titleContent = new GUIContent("Localization Editor");
21+
window.minSize = new Vector2(600, 500);
22+
window.Show();
23+
}
24+
25+
[ValueDropdown("GetAvailableLanguages")]
26+
[OnValueChanged("LoadLocalizationFile")]
27+
public string SelectedLanguage;
28+
29+
[TableList(AlwaysExpanded = true)]
30+
public List<LocalizationEntry> Entries = new();
31+
32+
[Button("💾 Save"), GUIColor(0.6f, 1f, 0.6f)]
33+
private void Save()
34+
{
35+
if (string.IsNullOrEmpty(SelectedLanguage)) return;
36+
37+
string path = Path.Combine(LocalizationFolder, SelectedLanguage + ".txt");
38+
List<string> lines = new();
39+
foreach (var entry in Entries)
40+
{
41+
lines.Add($"{entry.Key}={entry.Value.Replace("\n", "\\n")}");
42+
}
43+
44+
File.WriteAllLines(path, lines);
45+
AssetDatabase.Refresh();
46+
47+
Debug.Log($"✅ Saved language file: {path}");
48+
}
49+
50+
private void LoadLocalizationFile()
51+
{
52+
Entries.Clear();
53+
string path = Path.Combine(LocalizationFolder, SelectedLanguage + ".txt");
54+
if (!File.Exists(path))
55+
{
56+
Debug.LogWarning($"File not found: {path}");
57+
return;
58+
}
59+
60+
string[] lines = File.ReadAllLines(path);
61+
foreach (var line in lines)
62+
{
63+
if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#")) continue;
64+
65+
string[] pair = line.Split(new[] { '=' }, 2);
66+
if (pair.Length == 2)
67+
{
68+
Entries.Add(new LocalizationEntry { Key = pair[0].Trim(), Value = pair[1].Trim().Replace("\\n", "\n") });
69+
}
70+
}
71+
}
72+
73+
private IEnumerable<string> GetAvailableLanguages()
74+
{
75+
if (!Directory.Exists(LocalizationFolder))
76+
Directory.CreateDirectory(LocalizationFolder);
77+
78+
string[] files = Directory.GetFiles(LocalizationFolder, "*.txt");
79+
foreach (string file in files)
80+
yield return Path.GetFileNameWithoutExtension(file);
81+
}
82+
83+
[Serializable]
84+
public class LocalizationEntry
85+
{
86+
[HorizontalGroup("Row", Width = 250)]
87+
public string Key;
88+
89+
[HorizontalGroup("Row")]
90+
[MultiLineProperty(2)]
91+
public string Value;
92+
}
93+
}
94+
}
95+
#endif

Assets/Code/Editor/LocalizationEditorWindow.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
eng English
2-
ru Russian
3-
uk Ukrainian
4-
pla Play
5-
go GameOver
6-
ex Exit
7-
so Sounds
1+
eng=English
2+
ru=Russian
3+
uk=Ukrainian
4+
pla=Play
5+
go=GameOver
6+
ex=Exit
7+
so=Sounds
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
eng Английский
2-
ru Русский
3-
uk Украинский
4-
pla Играть
5-
go Поражение
6-
ex Выход
7-
so Звуки
1+
eng=Английский
2+
ru=Русский
3+
uk=Украинский
4+
pla=Играть
5+
go=Поражение
6+
ex=Выход
7+
so=Звуки
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
eng Англійська
2-
ru Російська
3-
uk Українська
4-
pla Грати
5-
go Поразка
6-
ex Вихід
7-
so Звуки
1+
eng=Англійська
2+
ru=Російська
3+
uk=Українська
4+
pla=Грати
5+
go=Поразка
6+
ex=Вихід
7+
so=Звуки

Assets/Code/Localization/Scripts/Locale.cs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,70 @@
11
using System.Collections.Generic;
22
using UnityEngine;
33

4-
namespace Localization{
5-
public static class Locale{
4+
namespace Localization
5+
{
6+
public static class Locale
7+
{
68
private const string STR_LOCALIZATION_KEY = "locale";
79
private const string STR_LOCALIZATION_PREFIX = "localization/";
810

911
private static string _currentLanguage;
1012

1113
public static bool currentLanguageHasBeenSet = false;
12-
public static Dictionary<string, string> currentLanguageStrings = new Dictionary<string, string>();
14+
public static Dictionary<string, string> currentLanguageStrings = new();
1315

1416
private static TextAsset _currentLocalizationText;
1517

16-
public static string CurrentLanguage{
18+
public static string CurrentLanguage
19+
{
1720
get { return _currentLanguage; }
18-
set{
19-
if (value != null && value.Trim() != string.Empty){
21+
set
22+
{
23+
if (value != null && value.Trim() != string.Empty)
24+
{
2025
_currentLanguage = value;
21-
_currentLocalizationText = Resources.Load(STR_LOCALIZATION_PREFIX + _currentLanguage, typeof(TextAsset)) as TextAsset;
22-
if (_currentLocalizationText == null){
26+
_currentLocalizationText =
27+
Resources.Load(STR_LOCALIZATION_PREFIX + _currentLanguage, typeof(TextAsset)) as TextAsset;
28+
if (_currentLocalizationText == null)
29+
{
2330
Debug.LogWarningFormat("Missing locale '{0}', loading English.", _currentLanguage);
2431
_currentLanguage = SystemLanguage.English.ToString();
25-
_currentLocalizationText = Resources.Load(STR_LOCALIZATION_PREFIX + _currentLanguage, typeof(TextAsset)) as TextAsset;
32+
_currentLocalizationText =
33+
Resources.Load(STR_LOCALIZATION_PREFIX + _currentLanguage, typeof(TextAsset)) as TextAsset;
2634
}
27-
if (_currentLocalizationText != null){
28-
string[] lines = _currentLocalizationText.text.Split(new string[] { "\r\n", "\n\r", "\n" }, System.StringSplitOptions.RemoveEmptyEntries);
35+
36+
if (_currentLocalizationText != null)
37+
{
38+
string[] lines = _currentLocalizationText.text.Split(new string[] { "\r\n", "\n\r", "\n" },
39+
System.StringSplitOptions.RemoveEmptyEntries);
2940
currentLanguageStrings.Clear();
30-
for (int i = 0; i < lines.Length; i++){
41+
for (int i = 0; i < lines.Length; i++)
42+
{
3143
string[] pairs = lines[i].Split(new char[] { '\t', '=' }, 2);
32-
if (pairs.Length == 2){
44+
if (pairs.Length == 2)
45+
{
3346
currentLanguageStrings.Add(pairs[0].Trim(), pairs[1].Trim());
3447
}
3548
}
36-
}else{
49+
}
50+
else
51+
{
3752
Debug.LogErrorFormat("Local language '{0}', not found!", _currentLanguage);
3853
}
3954
}
4055
}
4156
}
42-
public static bool CurrentLanguageHasBeenSet{
43-
get{
44-
return currentLanguageHasBeenSet;
45-
}
57+
58+
public static bool CurrentLanguageHasBeenSet
59+
{
60+
get { return currentLanguageHasBeenSet; }
4661
}
4762

48-
public static SystemLanguage PlayerLanguage{
49-
get{
50-
return (SystemLanguage)PlayerPrefs.GetInt(STR_LOCALIZATION_KEY, (int)Application.systemLanguage);
51-
}
52-
set{
63+
public static SystemLanguage PlayerLanguage
64+
{
65+
get { return (SystemLanguage)PlayerPrefs.GetInt(STR_LOCALIZATION_KEY, (int)Application.systemLanguage); }
66+
set
67+
{
5368
PlayerPrefs.SetInt(STR_LOCALIZATION_KEY, (int)value);
5469
PlayerPrefs.Save();
5570
}
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
using UnityEngine;
22
using TMPro;
33

4-
namespace Localization{
4+
namespace Localization
5+
{
56
[RequireComponent(typeof(TMP_Text))]
6-
public class Localize : LocalizBase{
7+
public class Localize : LocalizBase
8+
{
79
private TMP_Text _text;
810

9-
protected override void Start(){
11+
protected override void Start()
12+
{
1013
_text = GetComponent<TMP_Text>();
1114
base.Start();
1215
}
13-
public override void UpdateLocale(){
16+
17+
public override void UpdateLocale()
18+
{
1419
if (!_text) return;
1520

16-
if (!System.String.IsNullOrEmpty(localizationKey) && Locale.currentLanguageStrings.ContainsKey(localizationKey))
17-
_text.text = Locale.currentLanguageStrings[localizationKey].Replace(@"\n", "" + '\n'); ;
21+
if (!System.String.IsNullOrEmpty(localizationKey) &&
22+
Locale.currentLanguageStrings.ContainsKey(localizationKey))
23+
_text.text = Locale.currentLanguageStrings[localizationKey].Replace(@"\n", "" + '\n');
24+
;
1825
}
1926
}
2027
}

Assets/Code/Localization/Scripts/LocalizeBase.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
using UnityEngine;
22

3-
namespace Localization{
4-
public abstract class LocalizBase : MonoBehaviour{
3+
namespace Localization
4+
{
5+
public abstract class LocalizBase : MonoBehaviour
6+
{
57
public string localizationKey;
68

79
public abstract void UpdateLocale();
810

9-
protected virtual void Start(){
10-
if (!Locale.currentLanguageHasBeenSet){
11+
protected virtual void Start()
12+
{
13+
if (!Locale.currentLanguageHasBeenSet)
14+
{
1115
Locale.currentLanguageHasBeenSet = true;
1216
SetCurrentLanguage(Locale.PlayerLanguage);
1317
}
18+
1419
UpdateLocale();
1520
}
1621

17-
public static string GetLocalizedString(string key){
22+
public static string GetLocalizedString(string key)
23+
{
1824
if (Locale.currentLanguageStrings.ContainsKey(key))
1925
return Locale.currentLanguageStrings[key];
2026
else
2127
return string.Empty;
2228
}
23-
public static void SetCurrentLanguage(SystemLanguage language){
29+
30+
public static void SetCurrentLanguage(SystemLanguage language)
31+
{
2432
Locale.CurrentLanguage = language.ToString();
2533
Locale.PlayerLanguage = language;
2634
Localize[] allTexts = GameObject.FindObjectsOfType<Localize>();

0 commit comments

Comments
 (0)