2015-01-03 15:20:34 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using Wox.Core.Exception;
|
|
|
|
|
using Wox.Core.UI;
|
2015-01-05 22:41:17 +08:00
|
|
|
|
using Wox.Core.UserSettings;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2015-02-07 20:17:49 +08:00
|
|
|
|
using Wox.Plugin;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
|
|
|
|
|
namespace Wox.Core.i18n
|
|
|
|
|
{
|
|
|
|
|
public class Internationalization : IInternationalization, IUIResource
|
|
|
|
|
{
|
2015-01-06 18:28:23 +08:00
|
|
|
|
private static string DefaultLanguageDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Languages");
|
2015-01-03 15:20:34 +08:00
|
|
|
|
|
|
|
|
|
static Internationalization()
|
|
|
|
|
{
|
|
|
|
|
MakesureThemeDirectoriesExist();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void MakesureThemeDirectoriesExist()
|
|
|
|
|
{
|
2015-01-06 18:28:23 +08:00
|
|
|
|
if (!Directory.Exists(DefaultLanguageDirectory))
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2015-01-06 18:28:23 +08:00
|
|
|
|
try
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2015-01-06 18:28:23 +08:00
|
|
|
|
Directory.CreateDirectory(DefaultLanguageDirectory);
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(e.Message);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ChangeLanguage(string languageCode)
|
|
|
|
|
{
|
|
|
|
|
Language language = GetLanguageByLanguageCode(languageCode);
|
|
|
|
|
ChangeLanguage(language);
|
2015-02-07 20:17:49 +08:00
|
|
|
|
UpdateAllPluginMetadataTranslations();
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Language GetLanguageByLanguageCode(string languageCode)
|
|
|
|
|
{
|
|
|
|
|
Language language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.ToLower() == languageCode.ToLower());
|
|
|
|
|
if (language == null)
|
|
|
|
|
{
|
|
|
|
|
throw new WoxI18nException("Invalid language code:" + languageCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return language;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ChangeLanguage(Language language)
|
|
|
|
|
{
|
2015-01-06 18:28:23 +08:00
|
|
|
|
if (language == null) throw new WoxI18nException("language can't be null");
|
2015-01-03 15:20:34 +08:00
|
|
|
|
|
|
|
|
|
string path = GetLanguagePath(language);
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
path = GetLanguagePath(AvailableLanguages.English);
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
throw new System.Exception("Change Language failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UserSettingStorage.Instance.Language = language.LanguageCode;
|
|
|
|
|
UserSettingStorage.Instance.Save();
|
|
|
|
|
ResourceMerger.ApplyResources();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResourceDictionary GetResourceDictionary()
|
|
|
|
|
{
|
|
|
|
|
return new ResourceDictionary
|
|
|
|
|
{
|
2015-01-06 18:28:23 +08:00
|
|
|
|
Source = new Uri(GetLanguageFile(DefaultLanguageDirectory), UriKind.Absolute)
|
2015-01-03 15:20:34 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Language> LoadAvailableLanguages()
|
|
|
|
|
{
|
|
|
|
|
return AvailableLanguages.GetAvailableLanguages();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetTranslation(string key)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
object translation = Application.Current.FindResource(key);
|
|
|
|
|
if (translation == null)
|
|
|
|
|
{
|
|
|
|
|
return "NoTranslation";
|
|
|
|
|
}
|
|
|
|
|
return translation.ToString();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return "NoTranslation";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetLanguagePath(string languageCode)
|
|
|
|
|
{
|
|
|
|
|
Language language = GetLanguageByLanguageCode(languageCode);
|
|
|
|
|
return GetLanguagePath(language);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-07 20:17:49 +08:00
|
|
|
|
|
|
|
|
|
internal void UpdateAllPluginMetadataTranslations()
|
|
|
|
|
{
|
|
|
|
|
List<KeyValuePair<PluginPair, IPluginI18n>> plugins = AssemblyHelper.LoadPluginInterfaces<IPluginI18n>();
|
|
|
|
|
foreach (var plugin in plugins)
|
|
|
|
|
{
|
|
|
|
|
UpdatePluginMetadataTranslations(plugin.Key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void UpdatePluginMetadataTranslations(PluginPair pluginPair)
|
|
|
|
|
{
|
|
|
|
|
var pluginI18n = pluginPair.Plugin as IPluginI18n;
|
|
|
|
|
if (pluginI18n == null) return;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
pluginPair.Metadata.Name = pluginI18n.GetTranslatedPluginTitle();
|
|
|
|
|
pluginPair.Metadata.Description = pluginI18n.GetTranslatedPluginDescription();
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn("Update Plugin metadata translation failed:" + e.Message);
|
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
private string GetLanguagePath(Language language)
|
|
|
|
|
{
|
2015-01-06 18:28:23 +08:00
|
|
|
|
string path = Path.Combine(DefaultLanguageDirectory, language.LanguageCode + ".xaml");
|
|
|
|
|
if (File.Exists(path))
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2015-01-06 18:28:23 +08:00
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string GetLanguageFile(string folder)
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(folder)) return string.Empty;
|
|
|
|
|
|
|
|
|
|
string path = Path.Combine(folder, UserSettingStorage.Instance.Language + ".xaml");
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string english = Path.Combine(folder, "en.xaml");
|
|
|
|
|
if (File.Exists(english))
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2015-01-06 18:28:23 +08:00
|
|
|
|
return english;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 23:24:11 +08:00
|
|
|
|
return string.Empty;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|