Skip to content

Commit 2e63af0

Browse files
committed
Version 0.1.0 - First working prototype
1 parent 558acfa commit 2e63af0

18 files changed

+390
-114
lines changed
Binary file not shown.

Assets/Editor/AutoTranslate.asmdef

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "AutoTranslate",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:9e24947de15b9834991c9d8411ea37cf",
6+
"GUID:eec0964c48f6f4e40bc3ec2257ccf8c5",
7+
"GUID:84651a3751eca9349aac36a66bba901b",
8+
"GUID:73f7137f823496d4180a1d859c2fcabe"
9+
],
10+
"includePlatforms": [
11+
"Editor"
12+
],
13+
"excludePlatforms": [],
14+
"allowUnsafeCode": false,
15+
"overrideReferences": false,
16+
"precompiledReferences": [],
17+
"autoReferenced": true,
18+
"defineConstraints": [],
19+
"versionDefines": [],
20+
"noEngineReferences": false
21+
}

Assets/Editor/AutoTranslate.asmdef.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/WindowAutoTranslate.cs

+117-76
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using GoodTime.Tools.GoogleApiTranslate;
12
using System.Collections.Generic;
23
using System.Linq;
4+
using System.Threading.Tasks;
35
using UnityEditor;
46
using UnityEngine;
57
using UnityEngine.AddressableAssets;
@@ -12,15 +14,17 @@ namespace GoodTime.HernetsMaksym.AutoTranslate.Windows
1214
{
1315
public class WindowAutoTranslate : EditorWindow
1416
{
17+
// Window parameters
1518
private const string k_WindowTitle = "Auto Translate for Localization";
16-
static readonly Vector2 k_MinSize = new Vector2(900, 600);
17-
18-
private LocalizationSettings _localizationSettings;
1919
private TypeStage _typeStage;
20-
private List<Locale> _locales;
2120

22-
private string _selectedLocale = "English";
21+
// Temp
22+
private List<Locale> _locales;
23+
private Locale _selectedLocale;
24+
private IList<StringTable> _tables;
2325

26+
// Arguments for translate
27+
private string _selectedLanguage = string.Empty;
2428
private bool _isOverrideWords = true;
2529
private bool _isTranslateEmptyWords = true;
2630
private bool _isTranslateSmartWords = true;
@@ -37,63 +41,14 @@ public void OnEnable()
3741
{
3842
_typeStage = TypeStage.Loading;
3943

40-
this.minSize = k_MinSize;
41-
4244
LoadSettings();
43-
44-
if (_locales.Count != 0)
45-
{
46-
_selectedLocale = _locales[0].name;
47-
}
4845
}
4946

5047
private void OnFocus()
5148
{
5249
LoadSettings();
5350
}
5451

55-
private async void LoadSettings()
56-
{
57-
string[] guids = AssetDatabase.FindAssets("Localization Settings t:LocalizationSettings", null);
58-
59-
if ( guids.Length != 0 )
60-
{
61-
var path = AssetDatabase.GUIDToAssetPath(guids[0]);
62-
63-
_localizationSettings = AssetDatabase.LoadAssetAtPath<LocalizationSettings>(path);
64-
65-
_locales = _localizationSettings.GetAvailableLocales().Locales;
66-
67-
Locale selectedLocale = await _localizationSettings.GetSelectedLocaleAsync().Task;
68-
69-
if (selectedLocale != null)
70-
{
71-
_selectedLocale = selectedLocale.name;
72-
}
73-
74-
_typeStage = TypeStage.Ready;
75-
}
76-
}
77-
78-
private async void LoadLaunchers()
79-
{
80-
_locales = new List<Locale>();
81-
82-
List<string> labels = new List<string>();
83-
labels.Add("Locale");
84-
IList<IResourceLocation> locations = await Addressables.LoadResourceLocationsAsync(labels, Addressables.MergeMode.None, typeof(Locale)).Task;
85-
86-
foreach (var location in locations)
87-
{
88-
_locales.Add(await Addressables.LoadAssetAsync<Locale>(location).Task);
89-
}
90-
91-
// It is no work. Why?. I dont know...
92-
//IList<Locale> lists = await Addressables.LoadAssetsAsync<Locale>(locations, null, Addressables.MergeMode.Union).Task;
93-
94-
_typeStage = TypeStage.Ready;
95-
}
96-
9752
void OnGUI()
9853
{
9954
if (_typeStage == TypeStage.Loading)
@@ -104,19 +59,19 @@ void OnGUI()
10459
{
10560
GUILayout.Space(10);
10661
EditorGUILayout.BeginHorizontal();
107-
EditorGUILayout.LabelField("Source Launcher", GUILayout.Width(300));
62+
EditorGUILayout.LabelField("Source language", GUILayout.Width(300));
10863

109-
var posit = new Rect(new Vector2(210, 10), new Vector2(200, 20));
110-
if (EditorGUILayout.DropdownButton(new GUIContent(_selectedLocale), FocusType.Passive))
64+
var posit = new Rect(new Vector2(310, 10), new Vector2(200, 20));
65+
if (EditorGUILayout.DropdownButton(new GUIContent(_selectedLanguage), FocusType.Passive))
11166
{
11267
var genericMenu = new GenericMenu();
11368

11469
foreach (var option in _locales.Select(w => w.name))
11570
{
116-
bool selected = option == _selectedLocale;
71+
bool selected = option == _selectedLanguage;
11772
genericMenu.AddItem(new GUIContent(option), selected, () =>
11873
{
119-
_selectedLocale = option;
74+
_selectedLanguage = option;
12075
});
12176
}
12277
genericMenu.DropDown(posit);
@@ -134,44 +89,128 @@ void OnGUI()
13489
_isTranslateSmartWords = EditorGUILayout.Toggle("Translate smart words", _isTranslateSmartWords);
13590

13691
GUILayout.Space(20);
137-
if (GUILayout.Button("Translate") )
92+
if (GUILayout.Button("Translate"))
13893
{
139-
Translate();
94+
ButtonTranslate_Click();
14095
}
14196
}
14297
else if (_typeStage == TypeStage.Translating)
14398
{
14499
EditorGUILayout.LabelField("Translating", EditorStyles.boldLabel);
145100
}
101+
else if (_typeStage == TypeStage.ErrorNoFoundSettings)
102+
{
103+
EditorGUILayout.LabelField("Error. Localization settings not found!", EditorStyles.boldLabel);
104+
}
105+
else if (_typeStage == TypeStage.ErrorNoFoundLocales)
106+
{
107+
EditorGUILayout.LabelField("Error. No languages found!", EditorStyles.boldLabel);
108+
}
146109
}
147110

148-
private void Translate()
111+
private void LoadSettings()
149112
{
150-
_typeStage = TypeStage.Translating;
151-
LoadTables();
113+
string[] guids = AssetDatabase.FindAssets("Localization Settings t:LocalizationSettings", null);
114+
115+
if (guids.Length != 0)
116+
{
117+
var path = AssetDatabase.GUIDToAssetPath(guids[0]);
118+
119+
LocalizationSettings localizationSettings = AssetDatabase.LoadAssetAtPath<LocalizationSettings>(path);
120+
121+
_locales = localizationSettings.GetAvailableLocales().Locales;
122+
123+
if ( _locales == null || _locales.Count == 0)
124+
{
125+
_typeStage = TypeStage.ErrorNoFoundLocales;
126+
return;
127+
}
128+
129+
Locale selectedLocale = localizationSettings.GetSelectedLocale();
130+
131+
if ( selectedLocale != null )
132+
{
133+
_selectedLanguage = selectedLocale.LocaleName;
134+
}
135+
136+
_selectedLanguage = _locales[0].LocaleName;
137+
138+
_typeStage = TypeStage.Ready;
139+
}
140+
else
141+
{
142+
_typeStage = TypeStage.ErrorNoFoundSettings;
143+
}
152144
}
153145

154-
public async void LoadTables()
146+
private void ButtonTranslate_Click()
155147
{
156-
List<string> labels = new List<string>();
148+
_typeStage = TypeStage.Translating;
149+
150+
Addressables.ClearResourceLocators();
151+
Addressables.CleanBundleCache();
157152

158-
foreach (var item in _locales)
153+
LoadTables().ContinueWith( _ =>
159154
{
160-
labels.Add("Locale-" + item.Formatter);
155+
TranslateTables();
156+
157+
_typeStage = TypeStage.Ready;
161158
}
159+
, System.Threading.Tasks.TaskContinuationOptions.ExecuteSynchronously);
160+
}
161+
162+
private async Task LoadTables()
163+
{
164+
List<string> labels = _locales.Select(w=> "Locale-" + w.Formatter).ToList();
162165

163166
IList<IResourceLocation> locations = await Addressables.LoadResourceLocationsAsync(labels, Addressables.MergeMode.Union, typeof(StringTable)).Task;
164167

165-
var tables = await Addressables.LoadAssetsAsync<StringTable>(locations, null).Task;
168+
_tables = await Addressables.LoadAssetsAsync<StringTable>(locations, null).Task;
166169

167-
foreach (var table in tables)
168-
{
169-
//table["SampleText"].Value = "sample";
170+
return;
171+
}
170172

171-
table.AddEntry("SampleText", "sample4");
172-
}
173+
private void TranslateTables()
174+
{
175+
if ( _tables == null ) return;
176+
177+
_selectedLocale = _locales.First(w => w.LocaleName == _selectedLanguage);
173178

174-
_typeStage = TypeStage.Ready;
179+
StringTable sourceLanguageTable = _tables.First(w => w.LocaleIdentifier == _selectedLocale.Identifier);
180+
181+
IList<StringTable> tablesForTranslate = _tables;
182+
tablesForTranslate.Remove(sourceLanguageTable);
183+
184+
GoogleApiTranslate translator = new GoogleApiTranslate();
185+
186+
foreach (StringTable targetLanguageTable in tablesForTranslate)
187+
{
188+
foreach (var entry in sourceLanguageTable.SharedData.Entries)
189+
{
190+
StringTableEntry sourceWord = sourceLanguageTable.GetEntry(entry.Key);
191+
if ( sourceWord.IsSmart == true && _isTranslateSmartWords == false)
192+
{
193+
continue;
194+
}
195+
StringTableEntry targetWord = targetLanguageTable.GetEntry(entry.Key);
196+
if ( targetWord == null || string.IsNullOrEmpty(targetWord.Value) )
197+
{
198+
if ( _isTranslateEmptyWords == false)
199+
{
200+
continue;
201+
}
202+
}
203+
else
204+
{
205+
if ( _isOverrideWords == false )
206+
{
207+
continue;
208+
}
209+
}
210+
string result = translator.Translate(sourceWord.Value, sourceLanguageTable.LocaleIdentifier.Code, targetLanguageTable.LocaleIdentifier.Code);
211+
targetLanguageTable.AddEntry(entry.Key, result);
212+
}
213+
}
175214
}
176215
}
177216

@@ -180,7 +219,9 @@ public enum TypeStage
180219
Loading,
181220
Ready,
182221
Translating,
183-
Done
222+
Done,
223+
ErrorNoFoundSettings,
224+
ErrorNoFoundLocales
184225
}
185226
}
186227

Assets/Samples/Scenes/Tables/SampleTable_en.asset

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ MonoBehaviour:
1919
m_Items: []
2020
m_TableData:
2121
- m_Id: 372829654384640
22-
m_Localized: sample2
22+
m_Localized: sample
2323
m_Metadata:
2424
m_Items: []
2525
references:
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "GoogleApiTranslate",
3+
"rootNamespace": "GoodTime.Tools.GoogleApiTranslate",
4+
"references": [],
5+
"includePlatforms": [],
6+
"excludePlatforms": [],
7+
"allowUnsafeCode": false,
8+
"overrideReferences": false,
9+
"precompiledReferences": [],
10+
"autoReferenced": true,
11+
"defineConstraints": [],
12+
"versionDefines": [],
13+
"noEngineReferences": false
14+
}

Assets/Scripts/GoogleApiTranslate.asmdef.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)