2015-01-03 15:20:34 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows;
|
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
|
|
|
|
{
|
2016-01-07 12:47:28 +08:00
|
|
|
|
public class Internationalization : Resource
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2016-06-19 23:18:43 +08:00
|
|
|
|
public Settings Settings { get; set; }
|
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
|
|
|
|
{
|
2016-01-07 12:47:28 +08:00
|
|
|
|
DirectoryName = "Languages";
|
2016-02-17 23:49:55 +08:00
|
|
|
|
MakesureDirectoriesExist();
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 23:49:55 +08:00
|
|
|
|
private void MakesureDirectoriesExist()
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2016-01-07 12:47:28 +08:00
|
|
|
|
if (!Directory.Exists(DirectoryPath))
|
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
|
|
|
|
{
|
2016-01-07 12:47:28 +08:00
|
|
|
|
Directory.CreateDirectory(DirectoryPath);
|
2015-01-06 18:28:23 +08:00
|
|
|
|
}
|
2016-01-07 05:34:42 +08:00
|
|
|
|
catch (Exception e)
|
2015-01-06 18:28:23 +08:00
|
|
|
|
{
|
2016-05-16 00:03:06 +08:00
|
|
|
|
Log.Exception(e);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ChangeLanguage(string languageCode)
|
|
|
|
|
{
|
|
|
|
|
Language language = GetLanguageByLanguageCode(languageCode);
|
|
|
|
|
ChangeLanguage(language);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2016-01-07 05:34:42 +08:00
|
|
|
|
throw new Exception("Change Language failed");
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 10:09:57 +08:00
|
|
|
|
Settings.Language = language.LanguageCode;
|
2016-01-07 12:47:28 +08:00
|
|
|
|
ResourceMerger.UpdateResource(this);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-07 12:47:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override ResourceDictionary GetResourceDictionary()
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2016-02-17 23:49:55 +08:00
|
|
|
|
var dictionary = new ResourceDictionary
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2016-01-07 12:47:28 +08:00
|
|
|
|
Source = new Uri(GetLanguageFile(DirectoryPath), UriKind.Absolute)
|
2015-01-03 15:20:34 +08:00
|
|
|
|
};
|
2016-02-17 23:49:55 +08:00
|
|
|
|
return dictionary;
|
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
|
|
|
|
{
|
|
|
|
|
return "NoTranslation";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetLanguagePath(string languageCode)
|
|
|
|
|
{
|
|
|
|
|
Language language = GetLanguageByLanguageCode(languageCode);
|
|
|
|
|
return GetLanguagePath(language);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-07 20:17:49 +08:00
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2016-01-07 05:34:42 +08:00
|
|
|
|
catch (Exception e)
|
2015-02-07 20:17:49 +08:00
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
var woxPluginException = new WoxPluginException(pluginPair.Metadata.Name, "Update Plugin metadata translation failed:", e);
|
2016-05-16 00:03:06 +08:00
|
|
|
|
Log.Exception(woxPluginException);
|
2015-02-07 20:17:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
private string GetLanguagePath(Language language)
|
|
|
|
|
{
|
2016-01-07 12:47:28 +08:00
|
|
|
|
string path = Path.Combine(DirectoryPath, language.LanguageCode + ".xaml");
|
2015-01-06 18:28:23 +08:00
|
|
|
|
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;
|
|
|
|
|
|
2016-03-28 10:09:57 +08:00
|
|
|
|
string path = Path.Combine(folder, Settings.Language + ".xaml");
|
2015-01-06 18:28:23 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-07 12:47:28 +08:00
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|