1
+ using GoodTime . Tools . GoogleApiTranslate ;
1
2
using System . Collections . Generic ;
2
3
using System . Linq ;
4
+ using System . Threading . Tasks ;
3
5
using UnityEditor ;
4
6
using UnityEngine ;
5
7
using UnityEngine . AddressableAssets ;
@@ -12,15 +14,17 @@ namespace GoodTime.HernetsMaksym.AutoTranslate.Windows
12
14
{
13
15
public class WindowAutoTranslate : EditorWindow
14
16
{
17
+ // Window parameters
15
18
private const string k_WindowTitle = "Auto Translate for Localization" ;
16
- static readonly Vector2 k_MinSize = new Vector2 ( 900 , 600 ) ;
17
-
18
- private LocalizationSettings _localizationSettings ;
19
19
private TypeStage _typeStage ;
20
- private List < Locale > _locales ;
21
20
22
- private string _selectedLocale = "English" ;
21
+ // Temp
22
+ private List < Locale > _locales ;
23
+ private Locale _selectedLocale ;
24
+ private IList < StringTable > _tables ;
23
25
26
+ // Arguments for translate
27
+ private string _selectedLanguage = string . Empty ;
24
28
private bool _isOverrideWords = true ;
25
29
private bool _isTranslateEmptyWords = true ;
26
30
private bool _isTranslateSmartWords = true ;
@@ -37,63 +41,14 @@ public void OnEnable()
37
41
{
38
42
_typeStage = TypeStage . Loading ;
39
43
40
- this . minSize = k_MinSize ;
41
-
42
44
LoadSettings ( ) ;
43
-
44
- if ( _locales . Count != 0 )
45
- {
46
- _selectedLocale = _locales [ 0 ] . name ;
47
- }
48
45
}
49
46
50
47
private void OnFocus ( )
51
48
{
52
49
LoadSettings ( ) ;
53
50
}
54
51
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
-
97
52
void OnGUI ( )
98
53
{
99
54
if ( _typeStage == TypeStage . Loading )
@@ -104,19 +59,19 @@ void OnGUI()
104
59
{
105
60
GUILayout . Space ( 10 ) ;
106
61
EditorGUILayout . BeginHorizontal ( ) ;
107
- EditorGUILayout . LabelField ( "Source Launcher " , GUILayout . Width ( 300 ) ) ;
62
+ EditorGUILayout . LabelField ( "Source language " , GUILayout . Width ( 300 ) ) ;
108
63
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 ) )
111
66
{
112
67
var genericMenu = new GenericMenu ( ) ;
113
68
114
69
foreach ( var option in _locales . Select ( w => w . name ) )
115
70
{
116
- bool selected = option == _selectedLocale ;
71
+ bool selected = option == _selectedLanguage ;
117
72
genericMenu . AddItem ( new GUIContent ( option ) , selected , ( ) =>
118
73
{
119
- _selectedLocale = option ;
74
+ _selectedLanguage = option ;
120
75
} ) ;
121
76
}
122
77
genericMenu . DropDown ( posit ) ;
@@ -134,44 +89,128 @@ void OnGUI()
134
89
_isTranslateSmartWords = EditorGUILayout . Toggle ( "Translate smart words" , _isTranslateSmartWords ) ;
135
90
136
91
GUILayout . Space ( 20 ) ;
137
- if ( GUILayout . Button ( "Translate" ) )
92
+ if ( GUILayout . Button ( "Translate" ) )
138
93
{
139
- Translate ( ) ;
94
+ ButtonTranslate_Click ( ) ;
140
95
}
141
96
}
142
97
else if ( _typeStage == TypeStage . Translating )
143
98
{
144
99
EditorGUILayout . LabelField ( "Translating" , EditorStyles . boldLabel ) ;
145
100
}
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
+ }
146
109
}
147
110
148
- private void Translate ( )
111
+ private void LoadSettings ( )
149
112
{
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
+ }
152
144
}
153
145
154
- public async void LoadTables ( )
146
+ private void ButtonTranslate_Click ( )
155
147
{
156
- List < string > labels = new List < string > ( ) ;
148
+ _typeStage = TypeStage . Translating ;
149
+
150
+ Addressables . ClearResourceLocators ( ) ;
151
+ Addressables . CleanBundleCache ( ) ;
157
152
158
- foreach ( var item in _locales )
153
+ LoadTables ( ) . ContinueWith ( _ =>
159
154
{
160
- labels . Add ( "Locale-" + item . Formatter ) ;
155
+ TranslateTables ( ) ;
156
+
157
+ _typeStage = TypeStage . Ready ;
161
158
}
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 ( ) ;
162
165
163
166
IList < IResourceLocation > locations = await Addressables . LoadResourceLocationsAsync ( labels , Addressables . MergeMode . Union , typeof ( StringTable ) ) . Task ;
164
167
165
- var tables = await Addressables . LoadAssetsAsync < StringTable > ( locations , null ) . Task ;
168
+ _tables = await Addressables . LoadAssetsAsync < StringTable > ( locations , null ) . Task ;
166
169
167
- foreach ( var table in tables )
168
- {
169
- //table["SampleText"].Value = "sample";
170
+ return ;
171
+ }
170
172
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 ) ;
173
178
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
+ }
175
214
}
176
215
}
177
216
@@ -180,7 +219,9 @@ public enum TypeStage
180
219
Loading ,
181
220
Ready ,
182
221
Translating ,
183
- Done
222
+ Done ,
223
+ ErrorNoFoundSettings ,
224
+ ErrorNoFoundLocales
184
225
}
185
226
}
186
227
0 commit comments