PowerToys/Wox.Core/Resource/ResourceMerger.cs

63 lines
2.3 KiB
C#
Raw Normal View History

2015-01-02 23:07:49 +08:00
using System;
using System.IO;
using System.Linq;
using System.Reflection;
2015-01-02 23:07:49 +08:00
using System.Windows;
using Wox.Core.Plugin;
2015-01-06 18:28:23 +08:00
using Wox.Plugin;
using Wox.Infrastructure.Exception;
2015-01-02 23:07:49 +08:00
namespace Wox.Core.Resource
2015-01-02 23:07:49 +08:00
{
public static class ResourceMerger
2015-01-02 23:07:49 +08:00
{
private static void RemoveResource(string directoryName)
2015-01-06 18:28:23 +08:00
{
directoryName = $"{Path.DirectorySeparatorChar}{directoryName}";
var dictionaries = Application.Current.Resources.MergedDictionaries;
foreach (var resource in dictionaries)
{
string currentDirectoryName = Path.GetDirectoryName(resource.Source.AbsolutePath);
if (currentDirectoryName == directoryName)
{
dictionaries.Remove(resource);
break;
}
}
}
public static void UpdateResource<T>(T t) where T : Core.Resource.Resource
{
RemoveResource(t.DirectoryName);
Application.Current.Resources.MergedDictionaries.Add(t.GetResourceDictionary());
}
internal static void UpdatePluginLanguages()
2015-01-06 18:28:23 +08:00
{
2016-05-19 02:38:43 +08:00
RemoveResource(Infrastructure.Constant.Plugins);
foreach (var plugin in PluginManager.GetPluginsForInterface<IPluginI18n>())
2015-01-06 18:28:23 +08:00
{
var location = Assembly.GetAssembly(plugin.Plugin.GetType()).Location;
var directoryName = Path.GetDirectoryName(location);
if (directoryName != null)
2015-01-06 18:28:23 +08:00
{
var internationalization = InternationalizationManager.Instance;
var folder = Path.Combine(directoryName, internationalization.DirectoryName);
var file = internationalization.GetLanguageFile(folder);
if (!string.IsNullOrEmpty(file))
{
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri(file, UriKind.Absolute)
});
}
}
else
{
throw new WoxPluginException(plugin.Metadata.Name, "Can't find plugin location.");
}
2015-01-06 18:28:23 +08:00
}
}
2015-01-02 23:07:49 +08:00
}
2015-01-06 18:28:23 +08:00
}