1
- using System ;
1
+ using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Globalization ;
4
4
using System . IO ;
14
14
15
15
namespace Flow . Launcher . Core . Resource
16
16
{
17
- public class Internationalization
17
+ public class Internationalization : IDisposable
18
18
{
19
19
private static readonly string ClassName = nameof ( Internationalization ) ;
20
20
@@ -30,6 +30,7 @@ public class Internationalization
30
30
private readonly List < string > _languageDirectories = [ ] ;
31
31
private readonly List < ResourceDictionary > _oldResources = [ ] ;
32
32
private static string SystemLanguageCode ;
33
+ private readonly SemaphoreSlim _langChangeLock = new ( 1 , 1 ) ;
33
34
34
35
public Internationalization ( Settings settings )
35
36
{
@@ -185,20 +186,33 @@ private static Language GetLanguageByLanguageCode(string languageCode)
185
186
186
187
private async Task ChangeLanguageAsync ( Language language , bool updateMetadata = true )
187
188
{
188
- // Remove old language files and load language
189
- RemoveOldLanguageFiles ( ) ;
190
- if ( language != AvailableLanguages . English )
189
+ await _langChangeLock . WaitAsync ( ) ;
190
+
191
+ try
191
192
{
192
- LoadLanguage ( language ) ;
193
- }
193
+ // Remove old language files and load language
194
+ RemoveOldLanguageFiles ( ) ;
195
+ if ( language != AvailableLanguages . English )
196
+ {
197
+ LoadLanguage ( language ) ;
198
+ }
194
199
195
- // Change culture info
196
- ChangeCultureInfo ( language . LanguageCode ) ;
200
+ // Change culture info
201
+ ChangeCultureInfo ( language . LanguageCode ) ;
197
202
198
- if ( updateMetadata )
203
+ if ( updateMetadata )
204
+ {
205
+ // Raise event for plugins after culture is set
206
+ await Task . Run ( UpdatePluginMetadataTranslations ) ;
207
+ }
208
+ }
209
+ catch ( Exception e )
199
210
{
200
- // Raise event for plugins after culture is set
201
- await Task . Run ( UpdatePluginMetadataTranslations ) ;
211
+ API . LogException ( ClassName , $ "Failed to change language to <{ language . LanguageCode } >", e ) ;
212
+ }
213
+ finally
214
+ {
215
+ _langChangeLock . Release ( ) ;
202
216
}
203
217
}
204
218
@@ -257,6 +271,7 @@ private void RemoveOldLanguageFiles()
257
271
{
258
272
dicts . Remove ( r ) ;
259
273
}
274
+ _oldResources . Clear ( ) ;
260
275
}
261
276
262
277
private void LoadLanguage ( Language language )
@@ -368,5 +383,15 @@ public static void UpdatePluginMetadataTranslations()
368
383
}
369
384
370
385
#endregion
386
+
387
+ #region IDisposable
388
+
389
+ public void Dispose ( )
390
+ {
391
+ RemoveOldLanguageFiles ( ) ;
392
+ _langChangeLock . Dispose ( ) ;
393
+ }
394
+
395
+ #endregion
371
396
}
372
397
}
0 commit comments