PowerToys/Wox.Core/Resource/Internationalization.cs

201 lines
6.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using Wox.Core.Plugin;
using Wox.Infrastructure;
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;
namespace Wox.Core.Resource
{
public class Internationalization
{
2016-06-19 23:18:43 +08:00
public Settings Settings { get; set; }
2017-02-28 08:12:06 +08:00
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<ResourceDictionary> _oldResources = new List<ResourceDictionary>();
public Internationalization()
{
2017-02-28 08:12:06 +08:00
AddPluginLanguageDirectories();
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);
}
2017-02-28 08:12:06 +08:00
private void AddPluginLanguageDirectories()
{
foreach (var plugin in PluginManager.GetPluginsForInterface<IPluginI18n>())
{
var location = Assembly.GetAssembly(plugin.Plugin.GetType()).Location;
var dir = Path.GetDirectoryName(location);
if (dir != null)
{
2017-02-28 08:12:06 +08:00
var pluginThemeDirectory = Path.Combine(dir, Folder);
_languageDirectories.Add(pluginThemeDirectory);
}
else
{
2017-02-28 08:12:06 +08:00
Log.Error($"|Internationalization.AddPluginLanguageDirectories|Can't find plugin path <{location}> for <{plugin.Metadata.Name}>");
}
}
}
2017-02-28 08:12:06 +08:00
private void LoadDefaultLanguage()
{
LoadLanguage(AvailableLanguages.English);
_oldResources.Clear();
}
public void ChangeLanguage(string languageCode)
{
languageCode = languageCode.NonNull();
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);
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;
}
}
public void ChangeLanguage(Language language)
{
language = language.NonNull();
2017-02-28 08:12:06 +08:00
Settings.Language = language.LanguageCode;
2017-02-28 08:12:06 +08:00
RemoveOldLanguageFiles();
if (language != AvailableLanguages.English)
{
2017-02-28 08:12:06 +08:00
LoadLanguage(language);
}
UpdatePluginMetadataTranslations();
2017-02-28 08:12:06 +08:00
}
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)
{
foreach (var f in files)
2017-01-30 08:26:11 +08:00
{
var r = new ResourceDictionary
2017-01-30 08:26:11 +08:00
{
Source = new Uri(f, UriKind.Absolute)
};
dicts.Add(r);
_oldResources.Add(r);
}
}
}
public List<Language> LoadAvailableLanguages()
{
return AvailableLanguages.GetAvailableLanguages();
}
public string GetTranslation(string key)
{
var translation = Application.Current.TryFindResource(key);
if (translation is string)
{
return translation.ToString();
}
else
{
2017-02-28 08:12:06 +08:00
Log.Error($"|Internationalization.GetTranslation|No Translation for key {key}");
return $"No Translation for key {key}";
}
}
2017-02-28 08:12:06 +08:00
private void UpdatePluginMetadataTranslations()
2015-02-07 20:17:49 +08:00
{
2017-02-28 08:12:06 +08:00
foreach (var p in PluginManager.GetPluginsForInterface<IPluginI18n>())
2015-02-07 20:17:49 +08:00
{
2017-02-28 08:12:06 +08:00
var pluginI18N = p.Plugin as IPluginI18n;
if (pluginI18N == null) return;
try
{
p.Metadata.Name = pluginI18N.GetTranslatedPluginTitle();
p.Metadata.Description = pluginI18N.GetTranslatedPluginDescription();
}
catch (Exception e)
{
Log.Exception($"|Internationalization.UpdatePluginMetadataTranslations|Failed for <{p.Metadata.Name}>", e);
}
2015-02-07 20:17:49 +08:00
}
}
2017-02-28 08:12:06 +08:00
public string LanguageFile(string folder, string language)
2015-01-06 18:28:23 +08:00
{
if (Directory.Exists(folder))
2015-01-06 18:28:23 +08:00
{
2017-02-28 08:12:06 +08:00
string path = Path.Combine(folder, language);
if (File.Exists(path))
{
return path;
}
else
{
Log.Error($"|Internationalization.LanguageFile|Language path can't be found <{path}>");
2017-02-28 08:12:06 +08:00
string english = Path.Combine(folder, DefaultFile);
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;
}
}
}
2017-02-28 08:12:06 +08:00
}