Initialize language before portable clean up since it needs translations

This commit is contained in:
Jack251970 2025-07-20 15:20:18 +08:00
parent ff2d5e89f9
commit 2ee53dfbf7
3 changed files with 78 additions and 47 deletions

View file

@ -43,7 +43,7 @@ namespace Flow.Launcher.Core.Plugin
/// <summary>
/// Directories that will hold Flow Launcher plugin directory
/// </summary>
private static readonly string[] Directories =
public static readonly string[] Directories =
{
Constant.PreinstalledDirectory, DataLocation.PluginsDirectory
};

View file

@ -3,7 +3,6 @@ using System.Collections.Generic;
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 @@ namespace Flow.Launcher.Core.Resource
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 @@ namespace Flow.Launcher.Core.Resource
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();
}
/// <summary>
/// Initialize language. Will change app language and plugin language based on settings.
/// </summary>
@ -116,11 +79,73 @@ namespace Flow.Launcher.Core.Resource
// 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();
}
/// <summary>
@ -152,7 +177,7 @@ namespace Flow.Launcher.Core.Resource
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 @@ namespace Flow.Launcher.Core.Resource
}
}
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 @@ namespace Flow.Launcher.Core.Resource
// 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 @@ namespace Flow.Launcher.Core.Resource
// 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 @@ namespace Flow.Launcher.Core.Resource
}
}
private void UpdatePluginMetadataTranslations()
public static void UpdatePluginMetadataTranslations()
{
// Update plugin metadata name & description
foreach (var p in PluginManager.GetTranslationPlugins())

View file

@ -191,6 +191,9 @@ namespace Flow.Launcher
// 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<Internationalization>().InitializeLanguageAsync();
Ioc.Default.GetRequiredService<Portable>().PreStartCleanUpAfterPortabilityUpdate();
API.LogInfo(ClassName, "Begin Flow Launcher startup ----------------------------------------------------");
@ -216,8 +219,8 @@ namespace Flow.Launcher
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<Internationalization>().InitializeLanguageAsync();
// Update plugin titles after plugins are initialized with their api instances
Internationalization.UpdatePluginMetadataTranslations();
await imageLoadertask;