Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions Flow.Launcher.Core/Resource/Internationalization.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand All @@ -14,7 +14,7 @@

namespace Flow.Launcher.Core.Resource
{
public class Internationalization
public class Internationalization : IDisposable
{
private static readonly string ClassName = nameof(Internationalization);

Expand All @@ -30,6 +30,7 @@ public class Internationalization
private readonly List<string> _languageDirectories = [];
private readonly List<ResourceDictionary> _oldResources = [];
private static string SystemLanguageCode;
private readonly SemaphoreSlim _langChangeLock = new(1, 1);

public Internationalization(Settings settings)
{
Expand Down Expand Up @@ -185,20 +186,33 @@ private static Language GetLanguageByLanguageCode(string languageCode)

private async Task ChangeLanguageAsync(Language language, bool updateMetadata = true)
{
// Remove old language files and load language
RemoveOldLanguageFiles();
if (language != AvailableLanguages.English)
await _langChangeLock.WaitAsync();

try
{
LoadLanguage(language);
}
// Remove old language files and load language
RemoveOldLanguageFiles();
if (language != AvailableLanguages.English)
{
LoadLanguage(language);
}

// Change culture info
ChangeCultureInfo(language.LanguageCode);
// Change culture info
ChangeCultureInfo(language.LanguageCode);

if (updateMetadata)
if (updateMetadata)
{
// Raise event for plugins after culture is set
await Task.Run(UpdatePluginMetadataTranslations);
}
}
catch (Exception e)
{
// Raise event for plugins after culture is set
await Task.Run(UpdatePluginMetadataTranslations);
API.LogException(ClassName, $"Failed to change language to <{language.LanguageCode}>", e);
}
finally
{
_langChangeLock.Release();
}
}

Expand Down Expand Up @@ -257,6 +271,7 @@ private void RemoveOldLanguageFiles()
{
dicts.Remove(r);
}
_oldResources.Clear();
}

private void LoadLanguage(Language language)
Expand Down Expand Up @@ -368,5 +383,15 @@ public static void UpdatePluginMetadataTranslations()
}

#endregion

#region IDisposable

public void Dispose()
{
RemoveOldLanguageFiles();
_langChangeLock.Dispose();
}

#endregion
}
}
5 changes: 4 additions & 1 deletion Flow.Launcher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
private static Settings _settings;
private static MainWindow _mainWindow;
private readonly MainViewModel _mainVM;
private readonly Internationalization _internationalization;

// To prevent two disposals running at the same time.
private static readonly object _disposingLock = new();
Expand Down Expand Up @@ -107,6 +108,7 @@
API = Ioc.Default.GetRequiredService<IPublicAPI>();
_settings.Initialize();
_mainVM = Ioc.Default.GetRequiredService<MainViewModel>();
_internationalization = Ioc.Default.GetRequiredService<Internationalization>();
}
catch (Exception e)
{
Expand Down Expand Up @@ -193,7 +195,7 @@
Win32Helper.EnableWin32DarkMode(_settings.ColorScheme);

// Initialize language before portable clean up since it needs translations
await Ioc.Default.GetRequiredService<Internationalization>().InitializeLanguageAsync();
await _internationalization.InitializeLanguageAsync();

Ioc.Default.GetRequiredService<Portable>().PreStartCleanUpAfterPortabilityUpdate();

Expand All @@ -204,7 +206,7 @@
RegisterDispatcherUnhandledException();
RegisterTaskSchedulerUnhandledException();

var imageLoadertask = ImageLoader.InitializeAsync();

Check warning on line 209 in Flow.Launcher/App.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`Loadertask` is not a recognized word. (unrecognized-spelling)

AbstractPluginEnvironment.PreStartPluginExecutablePathUpdate(_settings);

Expand All @@ -223,7 +225,7 @@
// Update plugin titles after plugins are initialized with their api instances
Internationalization.UpdatePluginMetadataTranslations();

await imageLoadertask;

Check warning on line 228 in Flow.Launcher/App.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`Loadertask` is not a recognized word. (unrecognized-spelling)

Check warning on line 228 in Flow.Launcher/App.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`Loadertask` is not a recognized word. (unrecognized-spelling)

_mainWindow = new MainWindow();

Expand Down Expand Up @@ -401,7 +403,7 @@

// If we call Environment.Exit(0), the application dispose will be called before _mainWindow.Close()
// Accessing _mainWindow?.Dispatcher will cause the application stuck
// So here we need to check it and just return so that we will not acees _mainWindow?.Dispatcher

Check warning on line 406 in Flow.Launcher/App.xaml.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`acees` is not a recognized word. (unrecognized-spelling)
if (!_mainWindow.CanClose)
{
return;
Expand All @@ -421,6 +423,7 @@
_mainWindow?.Dispatcher.Invoke(_mainWindow.Dispose);
_mainVM?.Dispose();
DialogJump.Dispose();
_internationalization.Dispose();
}

API.LogInfo(ClassName, "End Flow Launcher dispose ----------------------------------------------------");
Expand Down
Loading