From 2ee53dfbf771ba00dc603dff25851a655e302633 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 20 Jul 2025 15:20:18 +0800 Subject: [PATCH 1/6] Initialize language before portable clean up since it needs translations --- Flow.Launcher.Core/Plugin/PluginManager.cs | 2 +- .../Resource/Internationalization.cs | 116 +++++++++++------- Flow.Launcher/App.xaml.cs | 7 +- 3 files changed, 78 insertions(+), 47 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/PluginManager.cs b/Flow.Launcher.Core/Plugin/PluginManager.cs index 9c54ad7b118..8f134c1946e 100644 --- a/Flow.Launcher.Core/Plugin/PluginManager.cs +++ b/Flow.Launcher.Core/Plugin/PluginManager.cs @@ -43,7 +43,7 @@ public static class PluginManager /// /// Directories that will hold Flow Launcher plugin directory /// - private static readonly string[] Directories = + public static readonly string[] Directories = { Constant.PreinstalledDirectory, DataLocation.PluginsDirectory }; diff --git a/Flow.Launcher.Core/Resource/Internationalization.cs b/Flow.Launcher.Core/Resource/Internationalization.cs index 256975654d5..99bc9a84431 100644 --- a/Flow.Launcher.Core/Resource/Internationalization.cs +++ b/Flow.Launcher.Core/Resource/Internationalization.cs @@ -3,7 +3,6 @@ using System.Globalization; using System.IO; using System.Linq; -using System.Reflection; using System.Threading; using System.Threading.Tasks; using System.Windows; @@ -35,13 +34,6 @@ public class Internationalization public Internationalization(Settings settings) { _settings = settings; - AddFlowLauncherLanguageDirectory(); - } - - private void AddFlowLauncherLanguageDirectory() - { - var directory = Path.Combine(Constant.ProgramDirectory, Folder); - _languageDirectories.Add(directory); } public static void InitSystemLanguageCode() @@ -72,35 +64,6 @@ public static void InitSystemLanguageCode() SystemLanguageCode = DefaultLanguageCode; } - private void AddPluginLanguageDirectories() - { - foreach (var plugin in PluginManager.GetTranslationPlugins()) - { - var location = Assembly.GetAssembly(plugin.Plugin.GetType()).Location; - var dir = Path.GetDirectoryName(location); - if (dir != null) - { - var pluginThemeDirectory = Path.Combine(dir, Folder); - _languageDirectories.Add(pluginThemeDirectory); - } - else - { - API.LogError(ClassName, $"Can't find plugin path <{location}> for <{plugin.Metadata.Name}>"); - } - } - - LoadDefaultLanguage(); - } - - private void LoadDefaultLanguage() - { - // Removes language files loaded before any plugins were loaded. - // Prevents the language Flow started in from overwriting English if the user switches back to English - RemoveOldLanguageFiles(); - LoadLanguage(AvailableLanguages.English); - _oldResources.Clear(); - } - /// /// Initialize language. Will change app language and plugin language based on settings. /// @@ -116,11 +79,73 @@ public async Task InitializeLanguageAsync() // Get language by language code and change language var language = GetLanguageByLanguageCode(languageCode); + // Add Flow Launcher language directory + AddFlowLauncherLanguageDirectory(); + // Add plugin language directories first so that we can load language files from plugins AddPluginLanguageDirectories(); + // Load default language resources + LoadDefaultLanguage(); + // Change language - await ChangeLanguageAsync(language); + await ChangeLanguageAsync(language, false); + } + + private void AddFlowLauncherLanguageDirectory() + { + // Check if Flow Launcher language directory exists + var directory = Path.Combine(Constant.ProgramDirectory, Folder); + if (!Directory.Exists(directory)) + { + API.LogError(ClassName, $"Flow Launcher language directory can't be found <{directory}>"); + return; + } + + // Check if the language directory contains default language file + if (!File.Exists(Path.Combine(directory, DefaultFile))) + { + API.LogError(ClassName, $"Default language file can't be found in path <{directory}>"); + return; + } + + _languageDirectories.Add(directory); + } + + private void AddPluginLanguageDirectories() + { + foreach (var pluginsDir in PluginManager.Directories) + { + if (!Directory.Exists(pluginsDir)) continue; + + // Enumerate all top directories in the plugin directory + foreach (var dir in Directory.GetDirectories(pluginsDir)) + { + // Check if the directory contains a language folder + var pluginLanguageDir = Path.Combine(dir, Folder); + if (!Directory.Exists(pluginLanguageDir)) continue; + + // Check if the language directory contains default language file + if (File.Exists(Path.Combine(pluginLanguageDir, DefaultFile))) + { + // Add the plugin language directory to the list + _languageDirectories.Add(pluginLanguageDir); + } + else + { + API.LogError(ClassName, $"Can't find default language file in path <{pluginLanguageDir}>"); + } + } + } + } + + private void LoadDefaultLanguage() + { + // Removes language files loaded before any plugins were loaded. + // Prevents the language Flow started in from overwriting English if the user switches back to English + RemoveOldLanguageFiles(); + LoadLanguage(AvailableLanguages.English); + _oldResources.Clear(); } /// @@ -152,7 +177,7 @@ public void ChangeLanguage(string languageCode) private static Language GetLanguageByLanguageCode(string languageCode) { var lowercase = languageCode.ToLower(); - var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.ToLower() == lowercase); + var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.Equals(lowercase, StringComparison.CurrentCultureIgnoreCase)); if (language == null) { API.LogError(ClassName, $"Language code can't be found <{languageCode}>"); @@ -164,7 +189,7 @@ private static Language GetLanguageByLanguageCode(string languageCode) } } - private async Task ChangeLanguageAsync(Language language) + private async Task ChangeLanguageAsync(Language language, bool updateMetadata = true) { // Remove old language files and load language RemoveOldLanguageFiles(); @@ -176,8 +201,11 @@ private async Task ChangeLanguageAsync(Language language) // Change culture info ChangeCultureInfo(language.LanguageCode); - // Raise event for plugins after culture is set - await Task.Run(UpdatePluginMetadataTranslations); + if (updateMetadata) + { + // Raise event for plugins after culture is set + await Task.Run(UpdatePluginMetadataTranslations); + } } public static void ChangeCultureInfo(string languageCode) @@ -212,7 +240,7 @@ public bool PromptShouldUsePinyin(string languageCodeToSet) // No other languages should show the following text so just make it hard-coded // "Do you want to search with pinyin?" - string text = languageToSet == AvailableLanguages.Chinese ? "是否启用拼音搜索?" : "是否啓用拼音搜索?" ; + string text = languageToSet == AvailableLanguages.Chinese ? "是否启用拼音搜索?" : "是否啓用拼音搜索?"; if (API.ShowMsgBox(text, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.No) return false; @@ -276,7 +304,7 @@ public static string GetTranslation(string key) } } - private void UpdatePluginMetadataTranslations() + public static void UpdatePluginMetadataTranslations() { // Update plugin metadata name & description foreach (var p in PluginManager.GetTranslationPlugins()) diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 93fd88e4f0b..f48829bb33a 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -191,6 +191,9 @@ await API.StopwatchLogInfoAsync(ClassName, "Startup cost", async () => // Enable Win32 dark mode if the system is in dark mode before creating all windows Win32Helper.EnableWin32DarkMode(_settings.ColorScheme); + // Initialize language before portable clean up since it needs translations + await Ioc.Default.GetRequiredService().InitializeLanguageAsync(); + Ioc.Default.GetRequiredService().PreStartCleanUpAfterPortabilityUpdate(); API.LogInfo(ClassName, "Begin Flow Launcher startup ----------------------------------------------------"); @@ -216,8 +219,8 @@ await API.StopwatchLogInfoAsync(ClassName, "Startup cost", async () => await PluginManager.InitializePluginsAsync(); - // Change language after all plugins are initialized because we need to update plugin title based on their api - await Ioc.Default.GetRequiredService().InitializeLanguageAsync(); + // Update plugin titles after plugins are initialized with their api instances + Internationalization.UpdatePluginMetadataTranslations(); await imageLoadertask; From 634bdc5bd685c717b6e44b393b9d14a69eacfe99 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 20 Jul 2025 15:26:07 +0800 Subject: [PATCH 2/6] Do not check Flow Launcher default language file since it is binary embedded --- Flow.Launcher.Core/Resource/Internationalization.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Internationalization.cs b/Flow.Launcher.Core/Resource/Internationalization.cs index 99bc9a84431..5500f5421b8 100644 --- a/Flow.Launcher.Core/Resource/Internationalization.cs +++ b/Flow.Launcher.Core/Resource/Internationalization.cs @@ -102,13 +102,6 @@ private void AddFlowLauncherLanguageDirectory() return; } - // Check if the language directory contains default language file - if (!File.Exists(Path.Combine(directory, DefaultFile))) - { - API.LogError(ClassName, $"Default language file can't be found in path <{directory}>"); - return; - } - _languageDirectories.Add(directory); } From 5e8acf7d747990a76067b636aa773ddecbfbdab3 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 20 Jul 2025 15:29:27 +0800 Subject: [PATCH 3/6] Use OrdinalIgnoreCase --- Flow.Launcher.Core/Resource/Internationalization.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Core/Resource/Internationalization.cs b/Flow.Launcher.Core/Resource/Internationalization.cs index 5500f5421b8..bd138b17996 100644 --- a/Flow.Launcher.Core/Resource/Internationalization.cs +++ b/Flow.Launcher.Core/Resource/Internationalization.cs @@ -170,7 +170,7 @@ public void ChangeLanguage(string languageCode) private static Language GetLanguageByLanguageCode(string languageCode) { var lowercase = languageCode.ToLower(); - var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.Equals(lowercase, StringComparison.CurrentCultureIgnoreCase)); + var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.Equals(lowercase, StringComparison.OrdinalIgnoreCase)); if (language == null) { API.LogError(ClassName, $"Language code can't be found <{languageCode}>"); From ac7da2d2d61092eacc7e1cdee618cef619c3f0ef Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 20 Jul 2025 17:26:14 +0800 Subject: [PATCH 4/6] Do not check if the language directory contains default language file --- Flow.Launcher.Core/Resource/Internationalization.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Internationalization.cs b/Flow.Launcher.Core/Resource/Internationalization.cs index bd138b17996..10258f080a4 100644 --- a/Flow.Launcher.Core/Resource/Internationalization.cs +++ b/Flow.Launcher.Core/Resource/Internationalization.cs @@ -118,16 +118,8 @@ private void AddPluginLanguageDirectories() var pluginLanguageDir = Path.Combine(dir, Folder); if (!Directory.Exists(pluginLanguageDir)) continue; - // Check if the language directory contains default language file - if (File.Exists(Path.Combine(pluginLanguageDir, DefaultFile))) - { - // Add the plugin language directory to the list - _languageDirectories.Add(pluginLanguageDir); - } - else - { - API.LogError(ClassName, $"Can't find default language file in path <{pluginLanguageDir}>"); - } + // Check if the language directory contains default language file since it will be checked later + _languageDirectories.Add(pluginLanguageDir); } } } From f77f14b0ee19bf0b83f21167cf459e00b80e1a42 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 20 Jul 2025 17:35:07 +0800 Subject: [PATCH 5/6] Improve code quality --- .../Resource/Internationalization.cs | 93 ++++++++++++------- 1 file changed, 62 insertions(+), 31 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Internationalization.cs b/Flow.Launcher.Core/Resource/Internationalization.cs index 10258f080a4..c1e1dbe797d 100644 --- a/Flow.Launcher.Core/Resource/Internationalization.cs +++ b/Flow.Launcher.Core/Resource/Internationalization.cs @@ -27,8 +27,8 @@ public class Internationalization private const string DefaultFile = "en.xaml"; private const string Extension = ".xaml"; private readonly Settings _settings; - private readonly List _languageDirectories = new(); - private readonly List _oldResources = new(); + private readonly List _languageDirectories = []; + private readonly List _oldResources = []; private static string SystemLanguageCode; public Internationalization(Settings settings) @@ -36,6 +36,11 @@ public Internationalization(Settings settings) _settings = settings; } + #region Initialization + + /// + /// Initialize the system language code based on the current culture. + /// public static void InitSystemLanguageCode() { var availableLanguages = AvailableLanguages.GetAvailableLanguages(); @@ -133,6 +138,10 @@ private void LoadDefaultLanguage() _oldResources.Clear(); } + #endregion + + #region Change Language + /// /// Change language during runtime. Will change app language and plugin language & save settings. /// @@ -213,6 +222,10 @@ public static void ChangeCultureInfo(string languageCode) thread.CurrentUICulture = currentCulture; } + #endregion + + #region Prompt Pinyin + public bool PromptShouldUsePinyin(string languageCodeToSet) { var languageToSet = GetLanguageByLanguageCode(languageCodeToSet); @@ -233,6 +246,10 @@ public bool PromptShouldUsePinyin(string languageCodeToSet) return true; } + #endregion + + #region Language Resources Management + private void RemoveOldLanguageFiles() { var dicts = Application.Current.Resources.MergedDictionaries; @@ -268,6 +285,40 @@ private void LoadLanguage(Language language) } } + private static string LanguageFile(string folder, string language) + { + if (Directory.Exists(folder)) + { + var path = Path.Combine(folder, language); + if (File.Exists(path)) + { + return path; + } + else + { + API.LogError(ClassName, $"Language path can't be found <{path}>"); + var english = Path.Combine(folder, DefaultFile); + if (File.Exists(english)) + { + return english; + } + else + { + API.LogError(ClassName, $"Default English Language path can't be found <{path}>"); + return string.Empty; + } + } + } + else + { + return string.Empty; + } + } + + #endregion + + #region Available Languages + public List LoadAvailableLanguages() { var list = AvailableLanguages.GetAvailableLanguages(); @@ -275,6 +326,10 @@ public List LoadAvailableLanguages() return list; } + #endregion + + #region Get Translations + public static string GetTranslation(string key) { var translation = Application.Current.TryFindResource(key); @@ -289,6 +344,10 @@ public static string GetTranslation(string key) } } + #endregion + + #region Update Metadata + public static void UpdatePluginMetadataTranslations() { // Update plugin metadata name & description @@ -308,34 +367,6 @@ public static void UpdatePluginMetadataTranslations() } } - private static string LanguageFile(string folder, string language) - { - if (Directory.Exists(folder)) - { - var path = Path.Combine(folder, language); - if (File.Exists(path)) - { - return path; - } - else - { - API.LogError(ClassName, $"Language path can't be found <{path}>"); - var english = Path.Combine(folder, DefaultFile); - if (File.Exists(english)) - { - return english; - } - else - { - API.LogError(ClassName, $"Default English Language path can't be found <{path}>"); - return string.Empty; - } - } - } - else - { - return string.Empty; - } - } + #endregion } } From fea899d09aa362b2a5285db9ce8936c44a36baa6 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 20 Jul 2025 17:37:12 +0800 Subject: [PATCH 6/6] No need to get lower for language code --- Flow.Launcher.Core/Resource/Internationalization.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Internationalization.cs b/Flow.Launcher.Core/Resource/Internationalization.cs index c1e1dbe797d..d2ab2d028df 100644 --- a/Flow.Launcher.Core/Resource/Internationalization.cs +++ b/Flow.Launcher.Core/Resource/Internationalization.cs @@ -170,8 +170,8 @@ public void ChangeLanguage(string languageCode) private static Language GetLanguageByLanguageCode(string languageCode) { - var lowercase = languageCode.ToLower(); - var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.Equals(lowercase, StringComparison.OrdinalIgnoreCase)); + var language = AvailableLanguages.GetAvailableLanguages(). + FirstOrDefault(o => o.LanguageCode.Equals(languageCode, StringComparison.OrdinalIgnoreCase)); if (language == null) { API.LogError(ClassName, $"Language code can't be found <{languageCode}>");