This commit is contained in:
bao-qian 2017-02-28 00:12:06 +00:00
parent cb1305c87f
commit 28c57f75e8
2 changed files with 75 additions and 41 deletions

View file

@ -4,10 +4,8 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Windows; using System.Windows;
using JetBrains.Annotations;
using Wox.Core.Plugin; using Wox.Core.Plugin;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Exception;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
using Wox.Infrastructure.UserSettings; using Wox.Infrastructure.UserSettings;
using Wox.Plugin; using Wox.Plugin;
@ -17,31 +15,53 @@ namespace Wox.Core.Resource
public class Internationalization public class Internationalization
{ {
public Settings Settings { get; set; } public Settings Settings { get; set; }
private const string DirectoryName = "Languages"; private const string Folder = "Languages";
private const string DefaultFile = "en.xaml";
private const string Extension = ".xaml";
private readonly List<string> _languageDirectories = new List<string>(); private readonly List<string> _languageDirectories = new List<string>();
private readonly List<ResourceDictionary> _oldResources = new List<ResourceDictionary>(); private readonly List<ResourceDictionary> _oldResources = new List<ResourceDictionary>();
public Internationalization() public Internationalization()
{ {
var woxThemeDirectory = Path.Combine(Constant.ProgramDirectory, DirectoryName); AddPluginLanguageDirectories();
_languageDirectories.Add(woxThemeDirectory); LoadDefaultLanguage();
// we don't want to load /Languages/en.xaml twice
// so add wox language directory after load plugin language files
AddWoxLanguageDirectory();
}
private void AddWoxLanguageDirectory()
{
var directory = Path.Combine(Constant.ProgramDirectory, Folder);
_languageDirectories.Add(directory);
}
private void AddPluginLanguageDirectories()
{
foreach (var plugin in PluginManager.GetPluginsForInterface<IPluginI18n>()) foreach (var plugin in PluginManager.GetPluginsForInterface<IPluginI18n>())
{ {
var location = Assembly.GetAssembly(plugin.Plugin.GetType()).Location; var location = Assembly.GetAssembly(plugin.Plugin.GetType()).Location;
var dir = Path.GetDirectoryName(location); var dir = Path.GetDirectoryName(location);
if (dir != null) if (dir != null)
{ {
var pluginThemeDirectory = Path.Combine(dir, DirectoryName); var pluginThemeDirectory = Path.Combine(dir, Folder);
_languageDirectories.Add(pluginThemeDirectory); _languageDirectories.Add(pluginThemeDirectory);
} }
else else
{ {
Log.Error($"|ResourceMerger.UpdatePluginLanguages|Can't find plugin path <{location}> for <{plugin.Metadata.Name}>"); Log.Error($"|Internationalization.AddPluginLanguageDirectories|Can't find plugin path <{location}> for <{plugin.Metadata.Name}>");
} }
} }
} }
private void LoadDefaultLanguage()
{
LoadLanguage(AvailableLanguages.English);
_oldResources.Clear();
}
public void ChangeLanguage(string languageCode) public void ChangeLanguage(string languageCode)
{ {
languageCode = languageCode.NonNull(); languageCode = languageCode.NonNull();
@ -68,17 +88,38 @@ namespace Wox.Core.Resource
{ {
language = language.NonNull(); language = language.NonNull();
var files = _languageDirectories.Select(LanguageFile).Where(f => !string.IsNullOrEmpty(f)).ToArray(); Settings.Language = language.LanguageCode;
RemoveOldLanguageFiles();
if (language != AvailableLanguages.English)
{
LoadLanguage(language);
}
Log.Debug($"|TEST|{Application.Current.Resources.MergedDictionaries.Select(d => d.Source.AbsolutePath).Formatted()}");
UpdatePluginMetadataTranslations();
}
private void RemoveOldLanguageFiles()
{
var dicts = Application.Current.Resources.MergedDictionaries;
foreach (var r in _oldResources)
{
dicts.Remove(r);
}
}
private void LoadLanguage(Language language)
{
var dicts = Application.Current.Resources.MergedDictionaries;
var filename = $"{language.LanguageCode}{Extension}";
var files = _languageDirectories
.Select(d => LanguageFile(d, filename))
.Where(f => !string.IsNullOrEmpty(f))
.ToArray();
if (files.Length > 0) if (files.Length > 0)
{ {
Settings.Language = language.LanguageCode;
var dicts = Application.Current.Resources.MergedDictionaries;
foreach (var r in _oldResources)
{
dicts.Remove(r);
}
foreach (var f in files) foreach (var f in files)
{ {
var r = new ResourceDictionary var r = new ResourceDictionary
@ -89,14 +130,8 @@ namespace Wox.Core.Resource
_oldResources.Add(r); _oldResources.Add(r);
} }
} }
foreach (var plugin in PluginManager.GetPluginsForInterface<IPluginI18n>())
{
UpdatePluginMetadataTranslations(plugin);
}
} }
public List<Language> LoadAvailableLanguages() public List<Language> LoadAvailableLanguages()
{ {
return AvailableLanguages.GetAvailableLanguages(); return AvailableLanguages.GetAvailableLanguages();
@ -111,32 +146,34 @@ namespace Wox.Core.Resource
} }
else else
{ {
var m = $"No Translation for key {key}"; Log.Error($"|Internationalization.GetTranslation|No Translation for key {key}");
Log.Error($"|Internationalization.GetTranslation|{m}"); return $"No Translation for key {key}";
return m;
} }
} }
private void UpdatePluginMetadataTranslations(PluginPair pluginPair) private void UpdatePluginMetadataTranslations()
{ {
var pluginI18n = pluginPair.Plugin as IPluginI18n; foreach (var p in PluginManager.GetPluginsForInterface<IPluginI18n>())
if (pluginI18n == null) return;
try
{ {
pluginPair.Metadata.Name = pluginI18n.GetTranslatedPluginTitle(); var pluginI18N = p.Plugin as IPluginI18n;
pluginPair.Metadata.Description = pluginI18n.GetTranslatedPluginDescription(); if (pluginI18N == null) return;
} try
catch (Exception e) {
{ p.Metadata.Name = pluginI18N.GetTranslatedPluginTitle();
Log.Exception($"|Internationalization.UpdatePluginMetadataTranslations|Update Plugin metadata translation failed for <{pluginPair.Metadata.Name}>", e); p.Metadata.Description = pluginI18N.GetTranslatedPluginDescription();
}
catch (Exception e)
{
Log.Exception($"|Internationalization.UpdatePluginMetadataTranslations|Failed for <{p.Metadata.Name}>", e);
}
} }
} }
public string LanguageFile(string folder) public string LanguageFile(string folder, string language)
{ {
if (Directory.Exists(folder)) if (Directory.Exists(folder))
{ {
string path = Path.Combine(folder, Settings.Language + ".xaml"); string path = Path.Combine(folder, language);
if (File.Exists(path)) if (File.Exists(path))
{ {
return path; return path;
@ -144,7 +181,7 @@ namespace Wox.Core.Resource
else else
{ {
Log.Error($"|Internationalization.LanguageFile|Language path can't be found <{path}>"); Log.Error($"|Internationalization.LanguageFile|Language path can't be found <{path}>");
string english = Path.Combine(folder, "en.xaml"); string english = Path.Combine(folder, DefaultFile);
if (File.Exists(english)) if (File.Exists(english))
{ {
return english; return english;
@ -162,5 +199,4 @@ namespace Wox.Core.Resource
} }
} }
} }
}
}

View file

@ -61,8 +61,6 @@ namespace Wox
Current.MainWindow.Title = Constant.Wox; Current.MainWindow.Title = Constant.Wox;
// happlebao todo temp fix for instance code logic // happlebao todo temp fix for instance code logic
// remove all dictionaries defined in xaml e.g.g App.xaml
Current.Resources.MergedDictionaries.Clear();
// load plugin before change language, because plugin language also needs be changed // load plugin before change language, because plugin language also needs be changed
InternationalizationManager.Instance.Settings = _settings; InternationalizationManager.Instance.Settings = _settings;
InternationalizationManager.Instance.ChangeLanguage(_settings.Language); InternationalizationManager.Instance.ChangeLanguage(_settings.Language);