mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
Add plugin title translations
This commit is contained in:
57
Wox.Core/AssemblyHelper.cs
Normal file
57
Wox.Core/AssemblyHelper.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core
|
||||
{
|
||||
internal class AssemblyHelper
|
||||
{
|
||||
public static List<KeyValuePair<PluginPair, T>> LoadPluginInterfaces<T>() where T : class
|
||||
{
|
||||
List<PluginMetadata> CSharpPluginMetadatas = PluginManager.AllPlugins.Select(o => o.Metadata).Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
|
||||
List<KeyValuePair<PluginPair, T>> plugins = new List<KeyValuePair<PluginPair, T>>();
|
||||
foreach (PluginMetadata metadata in CSharpPluginMetadatas)
|
||||
{
|
||||
try
|
||||
{
|
||||
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
|
||||
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(T))).ToList();
|
||||
if (types.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (Type type in types)
|
||||
{
|
||||
plugins.Add(new KeyValuePair<PluginPair, T>(PluginManager.AllPlugins.First(o => o.Metadata.ID == metadata.ID),
|
||||
Activator.CreateInstance(type) as T));
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(string.Format("Couldn't load plugin {0}: {1}", metadata.Name, e.Message));
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
public static List<T> LoadInterfacesFromAppDomain<T>() where T : class
|
||||
{
|
||||
var interfaceObjects = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(s => s.GetTypes())
|
||||
.Where(p => p.IsClass && !p.IsAbstract && p.GetInterfaces().Contains(typeof(T)));
|
||||
|
||||
return interfaceObjects.Select(interfaceObject => (T) Activator.CreateInstance(interfaceObject)).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using Wox.Core.Exception;
|
||||
using Wox.Core.i18n;
|
||||
using Wox.Core.UI;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Infrastructure;
|
||||
@@ -23,7 +24,7 @@ namespace Wox.Core.Plugin
|
||||
{
|
||||
public const string ActionKeywordWildcardSign = "*";
|
||||
private static List<PluginMetadata> pluginMetadatas;
|
||||
private static List<KeyValuePair<PluginMetadata, IInstantQuery>> instantSearches;
|
||||
private static List<KeyValuePair<PluginPair, IInstantQuery>> instantSearches;
|
||||
private static List<KeyValuePair<PluginPair, IExclusiveQuery>> exclusiveSearchPlugins;
|
||||
|
||||
public static String DebuggerMode { get; private set; }
|
||||
@@ -99,6 +100,7 @@ namespace Wox.Core.Plugin
|
||||
sw.Stop();
|
||||
DebugHelper.WriteLine(string.Format("Plugin init:{0} - {1}", pair.Metadata.Name, sw.ElapsedMilliseconds));
|
||||
pair.InitTime = sw.ElapsedMilliseconds;
|
||||
InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -178,7 +180,7 @@ namespace Wox.Core.Plugin
|
||||
{
|
||||
//todo:to improve performance, any instant search plugin that takes long than 200ms will not consider a instant plugin anymore
|
||||
return pluginMetadata.Language.ToUpper() == AllowedLanguage.CSharp &&
|
||||
LoadInstantSearches().Any(o => o.Key.ID == pluginMetadata.ID);
|
||||
LoadInstantSearches().Any(o => o.Key.Metadata.ID == pluginMetadata.ID);
|
||||
}
|
||||
|
||||
internal static void ExecutePluginQuery(PluginPair pair, Query query)
|
||||
@@ -211,39 +213,11 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
}
|
||||
|
||||
private static List<KeyValuePair<PluginMetadata, IInstantQuery>> LoadInstantSearches()
|
||||
private static List<KeyValuePair<PluginPair, IInstantQuery>> LoadInstantSearches()
|
||||
{
|
||||
if (instantSearches != null) return instantSearches;
|
||||
|
||||
instantSearches = new List<KeyValuePair<PluginMetadata, IInstantQuery>>();
|
||||
List<PluginMetadata> CSharpPluginMetadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
|
||||
|
||||
foreach (PluginMetadata metadata in CSharpPluginMetadatas)
|
||||
{
|
||||
try
|
||||
{
|
||||
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
|
||||
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IInstantQuery))).ToList();
|
||||
if (types.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (Type type in types)
|
||||
{
|
||||
instantSearches.Add(new KeyValuePair<PluginMetadata, IInstantQuery>(metadata, Activator.CreateInstance(type) as IInstantQuery));
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(string.Format("Couldn't load plugin {0}: {1}", metadata.Name, e.Message));
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
instantSearches = AssemblyHelper.LoadPluginInterfaces<IInstantQuery>();
|
||||
|
||||
return instantSearches;
|
||||
}
|
||||
@@ -261,38 +235,7 @@ namespace Wox.Core.Plugin
|
||||
internal static List<KeyValuePair<PluginPair, IExclusiveQuery>> LoadExclusiveSearchPlugins()
|
||||
{
|
||||
if (exclusiveSearchPlugins != null) return exclusiveSearchPlugins;
|
||||
|
||||
exclusiveSearchPlugins = new List<KeyValuePair<PluginPair, IExclusiveQuery>>();
|
||||
List<PluginMetadata> CSharpPluginMetadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
|
||||
|
||||
foreach (PluginMetadata metadata in CSharpPluginMetadatas)
|
||||
{
|
||||
try
|
||||
{
|
||||
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
|
||||
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IExclusiveQuery))).ToList();
|
||||
if (types.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (Type type in types)
|
||||
{
|
||||
exclusiveSearchPlugins.Add(new KeyValuePair<PluginPair, IExclusiveQuery>(AllPlugins.First(o => o.Metadata.ID == metadata.ID),
|
||||
Activator.CreateInstance(type) as IExclusiveQuery));
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Error(string.Format("Couldn't load plugin {0}: {1}", metadata.Name, e.Message));
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
exclusiveSearchPlugins = AssemblyHelper.LoadPluginInterfaces<IExclusiveQuery>();
|
||||
return exclusiveSearchPlugins;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using Wox.Core.i18n;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Core.Theme;
|
||||
using Wox.Plugin;
|
||||
|
||||
@@ -9,38 +11,28 @@ namespace Wox.Core.UI
|
||||
{
|
||||
public class ResourceMerger
|
||||
{
|
||||
public static void ApplyResources()
|
||||
internal static void ApplyResources()
|
||||
{
|
||||
Application.Current.Resources.MergedDictionaries.Clear();
|
||||
ApplyPluginLanguages();
|
||||
ApplyThemeAndLanguageResources();
|
||||
}
|
||||
|
||||
private static void ApplyThemeAndLanguageResources()
|
||||
internal static void ApplyThemeAndLanguageResources()
|
||||
{
|
||||
var UIResourceType = typeof(IUIResource);
|
||||
var UIResources = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(s => s.GetTypes())
|
||||
.Where(p => p.IsClass && !p.IsAbstract && UIResourceType.IsAssignableFrom(p));
|
||||
|
||||
var UIResources = AssemblyHelper.LoadInterfacesFromAppDomain<IUIResource>();
|
||||
foreach (var uiResource in UIResources)
|
||||
{
|
||||
Application.Current.Resources.MergedDictionaries.Add(
|
||||
((IUIResource)Activator.CreateInstance(uiResource)).GetResourceDictionary());
|
||||
Application.Current.Resources.MergedDictionaries.Add(uiResource.GetResourceDictionary());
|
||||
}
|
||||
}
|
||||
|
||||
public static void ApplyPluginLanguages()
|
||||
internal static void ApplyPluginLanguages()
|
||||
{
|
||||
var pluginI18nType = typeof(IPluginI18n);
|
||||
var pluginI18ns = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(s => s.GetTypes())
|
||||
.Where(p => p.IsClass && !p.IsAbstract && pluginI18nType.IsAssignableFrom(p));
|
||||
|
||||
var pluginI18ns = AssemblyHelper.LoadInterfacesFromAppDomain<IPluginI18n>();
|
||||
foreach (var pluginI18n in pluginI18ns)
|
||||
{
|
||||
string languageFile = InternationalizationManager.Instance.GetLanguageFile(
|
||||
((IPluginI18n)Activator.CreateInstance(pluginI18n)).GetLanguagesFolder());
|
||||
string languageFile = InternationalizationManager.Instance.GetLanguageFile(pluginI18n.GetLanguagesFolder());
|
||||
if (!string.IsNullOrEmpty(languageFile))
|
||||
{
|
||||
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
|
||||
@@ -50,5 +42,7 @@ namespace Wox.Core.UI
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -69,6 +69,7 @@
|
||||
<Compile Include="Exception\WoxI18nException.cs" />
|
||||
<Compile Include="Exception\WoxJsonRPCException.cs" />
|
||||
<Compile Include="Exception\WoxPluginException.cs" />
|
||||
<Compile Include="AssemblyHelper.cs" />
|
||||
<Compile Include="Plugin\QueryDispatcher\BaseQueryDispatcher.cs" />
|
||||
<Compile Include="Updater\Release.cs" />
|
||||
<Compile Include="Updater\UpdaterManager.cs" />
|
||||
|
||||
@@ -9,6 +9,7 @@ using Wox.Core.Exception;
|
||||
using Wox.Core.UI;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.i18n
|
||||
{
|
||||
@@ -40,6 +41,7 @@ namespace Wox.Core.i18n
|
||||
{
|
||||
Language language = GetLanguageByLanguageCode(languageCode);
|
||||
ChangeLanguage(language);
|
||||
UpdateAllPluginMetadataTranslations();
|
||||
}
|
||||
|
||||
private Language GetLanguageByLanguageCode(string languageCode)
|
||||
@@ -109,6 +111,36 @@ namespace Wox.Core.i18n
|
||||
return GetLanguagePath(language);
|
||||
}
|
||||
|
||||
|
||||
internal void UpdateAllPluginMetadataTranslations()
|
||||
{
|
||||
List<KeyValuePair<PluginPair, IPluginI18n>> plugins = AssemblyHelper.LoadPluginInterfaces<IPluginI18n>();
|
||||
foreach (var plugin in plugins)
|
||||
{
|
||||
UpdatePluginMetadataTranslations(plugin.Key);
|
||||
}
|
||||
}
|
||||
|
||||
internal void UpdatePluginMetadataTranslations(PluginPair pluginPair)
|
||||
{
|
||||
var pluginI18n = pluginPair.Plugin as IPluginI18n;
|
||||
if (pluginI18n == null) return;
|
||||
try
|
||||
{
|
||||
pluginPair.Metadata.Name = pluginI18n.GetTranslatedPluginTitle();
|
||||
pluginPair.Metadata.Description = pluginI18n.GetTranslatedPluginDescription();
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Warn("Update Plugin metadata translation failed:" + e.Message);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
private string GetLanguagePath(Language language)
|
||||
{
|
||||
string path = Path.Combine(DefaultLanguageDirectory, language.LanguageCode + ".xaml");
|
||||
|
||||
Reference in New Issue
Block a user