2015-01-03 15:20:34 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2017-02-21 10:19:50 +08:00
|
|
|
|
using System.Reflection;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using System.Windows;
|
2017-02-21 10:19:50 +08:00
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using Wox.Core.Plugin;
|
|
|
|
|
using Wox.Infrastructure;
|
2015-11-09 09:32:33 +08:00
|
|
|
|
using Wox.Infrastructure.Exception;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2016-06-19 23:18:43 +08:00
|
|
|
|
using Wox.Infrastructure.UserSettings;
|
2015-02-07 20:17:49 +08:00
|
|
|
|
using Wox.Plugin;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
|
2016-01-08 04:04:37 +08:00
|
|
|
|
namespace Wox.Core.Resource
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
public class Internationalization
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2016-06-19 23:18:43 +08:00
|
|
|
|
public Settings Settings { get; set; }
|
2017-02-21 10:19:50 +08:00
|
|
|
|
private const string DirectoryName = "Languages";
|
|
|
|
|
private readonly List<string> _languageDirectories = new List<string>();
|
|
|
|
|
private readonly List<ResourceDictionary> _oldResources = new List<ResourceDictionary>();
|
2016-03-28 10:09:57 +08:00
|
|
|
|
|
2016-01-07 12:47:28 +08:00
|
|
|
|
public Internationalization()
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
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}>");
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ChangeLanguage(string languageCode)
|
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
languageCode = languageCode.NonNull();
|
2015-01-03 15:20:34 +08:00
|
|
|
|
Language language = GetLanguageByLanguageCode(languageCode);
|
|
|
|
|
ChangeLanguage(language);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Language GetLanguageByLanguageCode(string languageCode)
|
|
|
|
|
{
|
2017-01-30 08:26:11 +08:00
|
|
|
|
var lowercase = languageCode.ToLower();
|
|
|
|
|
var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.ToLower() == lowercase);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
if (language == null)
|
|
|
|
|
{
|
2017-01-30 08:26:11 +08:00
|
|
|
|
Log.Error($"|Internationalization.GetLanguageByLanguageCode|Language code can't be found <{languageCode}>");
|
|
|
|
|
return AvailableLanguages.English;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return language;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ChangeLanguage(Language language)
|
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
language = language.NonNull();
|
|
|
|
|
|
|
|
|
|
var files = _languageDirectories.Select(LanguageFile).Where(f => !string.IsNullOrEmpty(f)).ToArray();
|
|
|
|
|
|
|
|
|
|
if (files.Length > 0)
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
Settings.Language = language.LanguageCode;
|
|
|
|
|
|
|
|
|
|
var dicts = Application.Current.Resources.MergedDictionaries;
|
|
|
|
|
foreach (var r in _oldResources)
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
dicts.Remove(r);
|
2017-01-30 08:26:11 +08:00
|
|
|
|
}
|
2017-02-21 10:19:50 +08:00
|
|
|
|
foreach (var f in files)
|
2017-01-30 08:26:11 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
var r = new ResourceDictionary
|
2017-01-30 08:26:11 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
Source = new Uri(f, UriKind.Absolute)
|
|
|
|
|
};
|
|
|
|
|
dicts.Add(r);
|
|
|
|
|
_oldResources.Add(r);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-21 10:19:50 +08:00
|
|
|
|
|
|
|
|
|
foreach (var plugin in PluginManager.GetPluginsForInterface<IPluginI18n>())
|
2017-01-30 08:26:11 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
UpdatePluginMetadataTranslations(plugin);
|
2017-01-30 08:26:11 +08:00
|
|
|
|
}
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-07 12:47:28 +08:00
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
public List<Language> LoadAvailableLanguages()
|
|
|
|
|
{
|
|
|
|
|
return AvailableLanguages.GetAvailableLanguages();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetTranslation(string key)
|
|
|
|
|
{
|
2015-11-29 16:20:13 +08:00
|
|
|
|
var translation = Application.Current.TryFindResource(key);
|
|
|
|
|
if (translation is string)
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
|
|
|
|
return translation.ToString();
|
|
|
|
|
}
|
2015-11-29 16:20:13 +08:00
|
|
|
|
else
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-27 04:19:45 +08:00
|
|
|
|
var m = $"No Translation for key {key}";
|
|
|
|
|
Log.Error($"|Internationalization.GetTranslation|{m}");
|
|
|
|
|
return m;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 10:19:50 +08:00
|
|
|
|
private void UpdatePluginMetadataTranslations(PluginPair pluginPair)
|
2015-02-07 20:17:49 +08:00
|
|
|
|
{
|
|
|
|
|
var pluginI18n = pluginPair.Plugin as IPluginI18n;
|
|
|
|
|
if (pluginI18n == null) return;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
pluginPair.Metadata.Name = pluginI18n.GetTranslatedPluginTitle();
|
|
|
|
|
pluginPair.Metadata.Description = pluginI18n.GetTranslatedPluginDescription();
|
|
|
|
|
}
|
2016-01-07 05:34:42 +08:00
|
|
|
|
catch (Exception e)
|
2015-02-07 20:17:49 +08:00
|
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
|
Log.Exception($"|Internationalization.UpdatePluginMetadataTranslations|Update Plugin metadata translation failed for <{pluginPair.Metadata.Name}>", e);
|
2015-02-07 20:17:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 10:19:50 +08:00
|
|
|
|
public string LanguageFile(string folder)
|
2015-01-06 18:28:23 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
if (Directory.Exists(folder))
|
2015-01-06 18:28:23 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-06 18:28:23 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-01-06 23:24:11 +08:00
|
|
|
|
return string.Empty;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-07 12:47:28 +08:00
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|