PowerToys/Wox.Core/UI/ResourceMerger.cs

54 lines
1.9 KiB
C#
Raw Normal View History

2015-01-02 23:07:49 +08:00
using System;
using System.Linq;
using System.Windows;
using Wox.Core.i18n;
using Wox.Core.Theme;
2015-01-06 18:28:23 +08:00
using Wox.Plugin;
2015-01-02 23:07:49 +08:00
namespace Wox.Core.UI
{
public class ResourceMerger
{
public static void ApplyResources()
2015-01-06 18:28:23 +08:00
{
Application.Current.Resources.MergedDictionaries.Clear();
ApplyPluginLanguages();
2015-01-07 22:23:10 +08:00
ApplyThemeAndLanguageResources();
2015-01-06 18:28:23 +08:00
}
2015-01-07 22:23:10 +08:00
private static void ApplyThemeAndLanguageResources()
2015-01-02 23:07:49 +08:00
{
var UIResourceType = typeof(IUIResource);
var UIResources = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => p.IsClass && !p.IsAbstract && UIResourceType.IsAssignableFrom(p));
foreach (var uiResource in UIResources)
{
Application.Current.Resources.MergedDictionaries.Add(
((IUIResource)Activator.CreateInstance(uiResource)).GetResourceDictionary());
}
}
2015-01-06 18:28:23 +08:00
2015-01-07 22:23:10 +08:00
public static void ApplyPluginLanguages()
2015-01-06 18:28:23 +08:00
{
var pluginI18nType = typeof(IPluginI18n);
var pluginI18ns = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => p.IsClass && !p.IsAbstract && pluginI18nType.IsAssignableFrom(p));
2015-01-06 23:24:11 +08:00
foreach (var pluginI18n in pluginI18ns)
2015-01-06 18:28:23 +08:00
{
2015-01-21 23:00:56 +08:00
string languageFile = InternationalizationManager.Instance.GetLanguageFile(
2015-01-06 23:24:11 +08:00
((IPluginI18n)Activator.CreateInstance(pluginI18n)).GetLanguagesFolder());
if (!string.IsNullOrEmpty(languageFile))
2015-01-06 18:28:23 +08:00
{
2015-01-06 23:24:11 +08:00
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri(languageFile, UriKind.Absolute)
});
}
2015-01-06 18:28:23 +08:00
}
}
2015-01-02 23:07:49 +08:00
}
2015-01-06 18:28:23 +08:00
}