mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Merge pull request #3850 from Flow-Launcher/fast_load_language
Initialize language before portable clean up and plugin initialization
This commit is contained in:
commit
c02ef0e76e
3 changed files with 135 additions and 88 deletions
|
|
@ -47,7 +47,7 @@ namespace Flow.Launcher.Core.Plugin
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Directories that will hold Flow Launcher plugin directory
|
/// Directories that will hold Flow Launcher plugin directory
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly string[] Directories =
|
public static readonly string[] Directories =
|
||||||
{
|
{
|
||||||
Constant.PreinstalledDirectory, DataLocation.PluginsDirectory
|
Constant.PreinstalledDirectory, DataLocation.PluginsDirectory
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
|
@ -28,22 +27,20 @@ namespace Flow.Launcher.Core.Resource
|
||||||
private const string DefaultFile = "en.xaml";
|
private const string DefaultFile = "en.xaml";
|
||||||
private const string Extension = ".xaml";
|
private const string Extension = ".xaml";
|
||||||
private readonly Settings _settings;
|
private readonly Settings _settings;
|
||||||
private readonly List<string> _languageDirectories = new();
|
private readonly List<string> _languageDirectories = [];
|
||||||
private readonly List<ResourceDictionary> _oldResources = new();
|
private readonly List<ResourceDictionary> _oldResources = [];
|
||||||
private static string SystemLanguageCode;
|
private static string SystemLanguageCode;
|
||||||
|
|
||||||
public Internationalization(Settings settings)
|
public Internationalization(Settings settings)
|
||||||
{
|
{
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
AddFlowLauncherLanguageDirectory();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddFlowLauncherLanguageDirectory()
|
#region Initialization
|
||||||
{
|
|
||||||
var directory = Path.Combine(Constant.ProgramDirectory, Folder);
|
|
||||||
_languageDirectories.Add(directory);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize the system language code based on the current culture.
|
||||||
|
/// </summary>
|
||||||
public static void InitSystemLanguageCode()
|
public static void InitSystemLanguageCode()
|
||||||
{
|
{
|
||||||
var availableLanguages = AvailableLanguages.GetAvailableLanguages();
|
var availableLanguages = AvailableLanguages.GetAvailableLanguages();
|
||||||
|
|
@ -72,35 +69,6 @@ namespace Flow.Launcher.Core.Resource
|
||||||
SystemLanguageCode = DefaultLanguageCode;
|
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>
|
/// <summary>
|
||||||
/// Initialize language. Will change app language and plugin language based on settings.
|
/// Initialize language. Will change app language and plugin language based on settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -116,13 +84,64 @@ namespace Flow.Launcher.Core.Resource
|
||||||
// Get language by language code and change language
|
// Get language by language code and change language
|
||||||
var language = GetLanguageByLanguageCode(languageCode);
|
var language = GetLanguageByLanguageCode(languageCode);
|
||||||
|
|
||||||
|
// Add Flow Launcher language directory
|
||||||
|
AddFlowLauncherLanguageDirectory();
|
||||||
|
|
||||||
// Add plugin language directories first so that we can load language files from plugins
|
// Add plugin language directories first so that we can load language files from plugins
|
||||||
AddPluginLanguageDirectories();
|
AddPluginLanguageDirectories();
|
||||||
|
|
||||||
|
// Load default language resources
|
||||||
|
LoadDefaultLanguage();
|
||||||
|
|
||||||
// Change language
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
_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 since it will be checked later
|
||||||
|
_languageDirectories.Add(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();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Change Language
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Change language during runtime. Will change app language and plugin language & save settings.
|
/// Change language during runtime. Will change app language and plugin language & save settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -151,8 +170,8 @@ namespace Flow.Launcher.Core.Resource
|
||||||
|
|
||||||
private static Language GetLanguageByLanguageCode(string languageCode)
|
private static Language GetLanguageByLanguageCode(string languageCode)
|
||||||
{
|
{
|
||||||
var lowercase = languageCode.ToLower();
|
var language = AvailableLanguages.GetAvailableLanguages().
|
||||||
var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.ToLower() == lowercase);
|
FirstOrDefault(o => o.LanguageCode.Equals(languageCode, StringComparison.OrdinalIgnoreCase));
|
||||||
if (language == null)
|
if (language == null)
|
||||||
{
|
{
|
||||||
API.LogError(ClassName, $"Language code can't be found <{languageCode}>");
|
API.LogError(ClassName, $"Language code can't be found <{languageCode}>");
|
||||||
|
|
@ -164,7 +183,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
|
// Remove old language files and load language
|
||||||
RemoveOldLanguageFiles();
|
RemoveOldLanguageFiles();
|
||||||
|
|
@ -176,8 +195,11 @@ namespace Flow.Launcher.Core.Resource
|
||||||
// Change culture info
|
// Change culture info
|
||||||
ChangeCultureInfo(language.LanguageCode);
|
ChangeCultureInfo(language.LanguageCode);
|
||||||
|
|
||||||
// Raise event for plugins after culture is set
|
if (updateMetadata)
|
||||||
await Task.Run(UpdatePluginMetadataTranslations);
|
{
|
||||||
|
// Raise event for plugins after culture is set
|
||||||
|
await Task.Run(UpdatePluginMetadataTranslations);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ChangeCultureInfo(string languageCode)
|
public static void ChangeCultureInfo(string languageCode)
|
||||||
|
|
@ -200,6 +222,10 @@ namespace Flow.Launcher.Core.Resource
|
||||||
thread.CurrentUICulture = currentCulture;
|
thread.CurrentUICulture = currentCulture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Prompt Pinyin
|
||||||
|
|
||||||
public bool PromptShouldUsePinyin(string languageCodeToSet)
|
public bool PromptShouldUsePinyin(string languageCodeToSet)
|
||||||
{
|
{
|
||||||
var languageToSet = GetLanguageByLanguageCode(languageCodeToSet);
|
var languageToSet = GetLanguageByLanguageCode(languageCodeToSet);
|
||||||
|
|
@ -212,7 +238,7 @@ namespace Flow.Launcher.Core.Resource
|
||||||
|
|
||||||
// No other languages should show the following text so just make it hard-coded
|
// No other languages should show the following text so just make it hard-coded
|
||||||
// "Do you want to search with pinyin?"
|
// "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)
|
if (API.ShowMsgBox(text, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.No)
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -220,6 +246,10 @@ namespace Flow.Launcher.Core.Resource
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Language Resources Management
|
||||||
|
|
||||||
private void RemoveOldLanguageFiles()
|
private void RemoveOldLanguageFiles()
|
||||||
{
|
{
|
||||||
var dicts = Application.Current.Resources.MergedDictionaries;
|
var dicts = Application.Current.Resources.MergedDictionaries;
|
||||||
|
|
@ -255,46 +285,6 @@ namespace Flow.Launcher.Core.Resource
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Language> LoadAvailableLanguages()
|
|
||||||
{
|
|
||||||
var list = AvailableLanguages.GetAvailableLanguages();
|
|
||||||
list.Insert(0, new Language(Constant.SystemLanguageCode, AvailableLanguages.GetSystemTranslation(SystemLanguageCode)));
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string GetTranslation(string key)
|
|
||||||
{
|
|
||||||
var translation = Application.Current.TryFindResource(key);
|
|
||||||
if (translation is string)
|
|
||||||
{
|
|
||||||
return translation.ToString();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
API.LogError(ClassName, $"No Translation for key {key}");
|
|
||||||
return $"No Translation for key {key}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdatePluginMetadataTranslations()
|
|
||||||
{
|
|
||||||
// Update plugin metadata name & description
|
|
||||||
foreach (var p in PluginManager.GetTranslationPlugins())
|
|
||||||
{
|
|
||||||
if (p.Plugin is not IPluginI18n pluginI18N) return;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
p.Metadata.Name = pluginI18N.GetTranslatedPluginTitle();
|
|
||||||
p.Metadata.Description = pluginI18N.GetTranslatedPluginDescription();
|
|
||||||
pluginI18N.OnCultureInfoChanged(CultureInfo.CurrentCulture);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
API.LogException(ClassName, $"Failed for <{p.Metadata.Name}>", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string LanguageFile(string folder, string language)
|
private static string LanguageFile(string folder, string language)
|
||||||
{
|
{
|
||||||
if (Directory.Exists(folder))
|
if (Directory.Exists(folder))
|
||||||
|
|
@ -324,5 +314,59 @@ namespace Flow.Launcher.Core.Resource
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Available Languages
|
||||||
|
|
||||||
|
public List<Language> LoadAvailableLanguages()
|
||||||
|
{
|
||||||
|
var list = AvailableLanguages.GetAvailableLanguages();
|
||||||
|
list.Insert(0, new Language(Constant.SystemLanguageCode, AvailableLanguages.GetSystemTranslation(SystemLanguageCode)));
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Get Translations
|
||||||
|
|
||||||
|
public static string GetTranslation(string key)
|
||||||
|
{
|
||||||
|
var translation = Application.Current.TryFindResource(key);
|
||||||
|
if (translation is string)
|
||||||
|
{
|
||||||
|
return translation.ToString();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
API.LogError(ClassName, $"No Translation for key {key}");
|
||||||
|
return $"No Translation for key {key}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Update Metadata
|
||||||
|
|
||||||
|
public static void UpdatePluginMetadataTranslations()
|
||||||
|
{
|
||||||
|
// Update plugin metadata name & description
|
||||||
|
foreach (var p in PluginManager.GetTranslationPlugins())
|
||||||
|
{
|
||||||
|
if (p.Plugin is not IPluginI18n pluginI18N) return;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
p.Metadata.Name = pluginI18N.GetTranslatedPluginTitle();
|
||||||
|
p.Metadata.Description = pluginI18N.GetTranslatedPluginDescription();
|
||||||
|
pluginI18N.OnCultureInfoChanged(CultureInfo.CurrentCulture);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
API.LogException(ClassName, $"Failed for <{p.Metadata.Name}>", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -192,6 +192,9 @@ namespace Flow.Launcher
|
||||||
// Enable Win32 dark mode if the system is in dark mode before creating all windows
|
// Enable Win32 dark mode if the system is in dark mode before creating all windows
|
||||||
Win32Helper.EnableWin32DarkMode(_settings.ColorScheme);
|
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();
|
Ioc.Default.GetRequiredService<Portable>().PreStartCleanUpAfterPortabilityUpdate();
|
||||||
|
|
||||||
API.LogInfo(ClassName, "Begin Flow Launcher startup ----------------------------------------------------");
|
API.LogInfo(ClassName, "Begin Flow Launcher startup ----------------------------------------------------");
|
||||||
|
|
@ -217,8 +220,8 @@ namespace Flow.Launcher
|
||||||
|
|
||||||
await PluginManager.InitializePluginsAsync();
|
await PluginManager.InitializePluginsAsync();
|
||||||
|
|
||||||
// Change language after all plugins are initialized because we need to update plugin title based on their api
|
// Update plugin titles after plugins are initialized with their api instances
|
||||||
await Ioc.Default.GetRequiredService<Internationalization>().InitializeLanguageAsync();
|
Internationalization.UpdatePluginMetadataTranslations();
|
||||||
|
|
||||||
await imageLoadertask;
|
await imageLoadertask;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue