Completely rewrite resource load / update / remove

1. part of #468
2. fix #1245, bug introduced in 7e1c77beddf2c978fa1440e799fec4eb9554739d
This commit is contained in:
bao-qian
2017-02-21 02:19:50 +00:00
parent 17e5bb8d53
commit d0c426a7f9
10 changed files with 157 additions and 203 deletions

View File

@@ -2,7 +2,11 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using JetBrains.Annotations;
using Wox.Core.Plugin;
using Wox.Infrastructure;
using Wox.Infrastructure.Exception;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.UserSettings;
@@ -10,13 +14,34 @@ using Wox.Plugin;
namespace Wox.Core.Resource
{
public class Internationalization : Resource
public class Internationalization
{
public Settings Settings { get; set; }
private const string DirectoryName = "Languages";
private string DirectoryPath => Path.Combine(Constant.ProgramDirectory, DirectoryName);
private readonly List<string> _languageDirectories = new List<string>();
private readonly List<ResourceDictionary> _oldResources = new List<ResourceDictionary>();
public Internationalization()
{
DirectoryName = "Languages";
var woxThemeDirectory = Path.Combine(Constant.ProgramDirectory, DirectoryName);
_languageDirectories.Add(woxThemeDirectory);
foreach (var plugin in PluginManager.GetPluginsForInterface<IPluginI18n>())
{
var location = Assembly.GetAssembly(plugin.Plugin.GetType()).Location;
var dir = Path.GetDirectoryName(location);
if (dir != null)
{
var pluginThemeDirectory = Path.Combine(dir, DirectoryName);
_languageDirectories.Add(pluginThemeDirectory);
}
else
{
Log.Error($"|ResourceMerger.UpdatePluginLanguages|Can't find plugin path <{location}> for <{plugin.Metadata.Name}>");
}
}
MakesureDirectoriesExist();
}
@@ -37,6 +62,7 @@ namespace Wox.Core.Resource
public void ChangeLanguage(string languageCode)
{
languageCode = languageCode.NonNull();
Language language = GetLanguageByLanguageCode(languageCode);
ChangeLanguage(language);
}
@@ -58,42 +84,37 @@ namespace Wox.Core.Resource
public void ChangeLanguage(Language language)
{
if (language != null)
language = language.NonNull();
var files = _languageDirectories.Select(LanguageFile).Where(f => !string.IsNullOrEmpty(f)).ToArray();
if (files.Length > 0)
{
string path = GetLanguagePath(language);
if (!string.IsNullOrEmpty(path))
Settings.Language = language.LanguageCode;
var dicts = Application.Current.Resources.MergedDictionaries;
foreach (var r in _oldResources)
{
Settings.Language = language.LanguageCode;
ResourceMerger.UpdateResource(this);
dicts.Remove(r);
}
else
foreach (var f in files)
{
Log.Error($"|Internationalization.ChangeLanguage|Language path can't be found <{path}>");
path = GetLanguagePath(AvailableLanguages.English);
if (string.IsNullOrEmpty(path))
var r = new ResourceDictionary
{
Log.Error($"|Internationalization.ChangeLanguage|Default english language path can't be found <{path}>");
}
Source = new Uri(f, UriKind.Absolute)
};
dicts.Add(r);
_oldResources.Add(r);
}
}
else
foreach (var plugin in PluginManager.GetPluginsForInterface<IPluginI18n>())
{
Log.Error("|Internationalization.ChangeLanguage|Language can't be null");
UpdatePluginMetadataTranslations(plugin);
}
}
public override ResourceDictionary GetResourceDictionary()
{
var uri = GetLanguageFile(DirectoryPath);
var dictionary = new ResourceDictionary
{
Source = new Uri(uri, UriKind.Absolute)
};
return dictionary;
}
public List<Language> LoadAvailableLanguages()
{
return AvailableLanguages.GetAvailableLanguages();
@@ -112,14 +133,7 @@ namespace Wox.Core.Resource
}
}
private string GetLanguagePath(string languageCode)
{
Language language = GetLanguageByLanguageCode(languageCode);
return GetLanguagePath(language);
}
internal void UpdatePluginMetadataTranslations(PluginPair pluginPair)
private void UpdatePluginMetadataTranslations(PluginPair pluginPair)
{
var pluginI18n = pluginPair.Plugin as IPluginI18n;
if (pluginI18n == null) return;
@@ -134,38 +148,34 @@ namespace Wox.Core.Resource
}
}
private string GetLanguagePath(Language language)
public string LanguageFile(string folder)
{
string path = Path.Combine(DirectoryPath, language.LanguageCode + ".xaml");
if (File.Exists(path))
if (Directory.Exists(folder))
{
return path;
}
return string.Empty;
}
public string GetLanguageFile(string folder)
{
if (!Directory.Exists(folder)) return string.Empty;
string path = Path.Combine(folder, Settings.Language + ".xaml");
if (File.Exists(path))
{
return path;
string path = Path.Combine(folder, Settings.Language + ".xaml");
if (File.Exists(path))
{
return path;
}
else
{
Log.Error($"|Internationalization.LanguageFile|Language path can't be found <{path}>");
string english = Path.Combine(folder, "en.xaml");
if (File.Exists(english))
{
return english;
}
else
{
Log.Error($"|Internationalization.LanguageFile|Default English Language path can't be found <{path}>");
return string.Empty;
}
}
}
else
{
string english = Path.Combine(folder, "en.xaml");
if (File.Exists(english))
{
return english;
}
return string.Empty;
}
}
}