Add i18n support [WIP]

This commit is contained in:
qianlifeng
2015-01-02 23:07:49 +08:00
parent bf87500e35
commit 203965043e
21 changed files with 257 additions and 124 deletions

View File

@@ -6,6 +6,7 @@ using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json;
using Wox.Plugin;
using MessageBox = System.Windows.Forms.MessageBox;
namespace Wox.Core.Plugin
{
@@ -61,7 +62,7 @@ namespace Wox.Core.Plugin
plugin.Name, existingPlugin.Metadata.Version, plugin.Version, plugin.Author);
}
DialogResult result = MessageBox.Show(content, "Install plugin", MessageBoxButtons.YesNo,
DialogResult result = System.Windows.Forms.MessageBox.Show(content, "Install plugin", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{

View File

@@ -2,4 +2,6 @@
=====
* Handle Query
* Loading Plugins (including system plugin and user plugin)
* Manage Plugins (including system plugin and user plugin)
* Manage Themes
* Manage i18n

View File

@@ -0,0 +1,73 @@
using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace Wox.Core.Theme
{
public static class FontHelper
{
static FontWeightConverter fontWeightConverter = new FontWeightConverter();
public static FontWeight GetFontWeightFromInvariantStringOrNormal(string value)
{
if (value == null) return FontWeights.Normal;
try
{
return (FontWeight) fontWeightConverter.ConvertFromInvariantString(value);
}
catch {
return FontWeights.Normal;
}
}
static FontStyleConverter fontStyleConverter = new FontStyleConverter();
public static FontStyle GetFontStyleFromInvariantStringOrNormal(string value)
{
if (value == null) return FontStyles.Normal;
try
{
return (FontStyle)fontStyleConverter.ConvertFromInvariantString(value);
}
catch
{
return FontStyles.Normal;
}
}
static FontStretchConverter fontStretchConverter = new FontStretchConverter();
public static FontStretch GetFontStretchFromInvariantStringOrNormal(string value)
{
if (value == null) return FontStretches.Normal;
try
{
return (FontStretch)fontStretchConverter.ConvertFromInvariantString(value);
}
catch
{
return FontStretches.Normal;
}
}
public static FamilyTypeface ChooseRegularFamilyTypeface(this FontFamily family)
{
return family.FamilyTypefaces.OrderBy(o =>
{
return Math.Abs(o.Stretch.ToOpenTypeStretch() - FontStretches.Normal.ToOpenTypeStretch()) * 100 +
Math.Abs(o.Weight.ToOpenTypeWeight() - FontWeights.Normal.ToOpenTypeWeight()) +
(o.Style == FontStyles.Normal ? 0 : o.Style == FontStyles.Oblique ? 1 : 2) * 1000;
}).FirstOrDefault() ?? family.FamilyTypefaces.FirstOrDefault();
}
public static FamilyTypeface ConvertFromInvariantStringsOrNormal(this FontFamily family, string style, string weight, string stretch)
{
var styleObj = GetFontStyleFromInvariantStringOrNormal(style);
var weightObj = GetFontWeightFromInvariantStringOrNormal(weight);
var stretchObj = GetFontStretchFromInvariantStringOrNormal(stretch);
return family.FamilyTypefaces.FirstOrDefault(o => o.Style == styleObj && o.Weight == weightObj && o.Stretch == stretchObj)
?? family.ChooseRegularFamilyTypeface();
}
}
}

View File

@@ -0,0 +1,151 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Wox.Core.UI;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage.UserSettings;
namespace Wox.Core.Theme
{
public class ThemeManager : IUIResource
{
private static List<string> themeDirectories = new List<string>();
private static ThemeManager instance;
private static object syncObject = new object();
private ThemeManager() { }
public static ThemeManager Instance
{
get
{
if (instance == null)
{
lock (syncObject)
{
if (instance == null)
{
instance = new ThemeManager();
}
}
}
return instance;
}
}
static ThemeManager()
{
themeDirectories.Add(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Themes"));
string userProfilePath = Environment.GetEnvironmentVariable("USERPROFILE");
if (userProfilePath != null)
{
themeDirectories.Add(Path.Combine(Path.Combine(userProfilePath, ".Wox"), "Themes"));
}
MakesureThemeDirectoriesExist();
}
private static void MakesureThemeDirectoriesExist()
{
foreach (string pluginDirectory in themeDirectories)
{
if (!Directory.Exists(pluginDirectory))
{
try
{
Directory.CreateDirectory(pluginDirectory);
}
catch (System.Exception e)
{
Log.Error(e.Message);
}
}
}
}
public void ChangeTheme(string themeName)
{
string themePath = GetThemePath(themeName);
if (string.IsNullOrEmpty(themePath))
{
themePath = GetThemePath("Dark");
if (string.IsNullOrEmpty(themePath))
{
throw new System.Exception("Change theme failed");
}
}
UserSettingStorage.Instance.Theme = themeName;
UserSettingStorage.Instance.Save();
ResourceMerger.ApplyResources();
}
public ResourceDictionary GetResourceDictionary()
{
var dict = new ResourceDictionary
{
Source = new Uri(GetThemePath(UserSettingStorage.Instance.Theme), UriKind.Absolute)
};
Style queryBoxStyle = dict["QueryBoxStyle"] as Style;
if (queryBoxStyle != null)
{
queryBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.QueryBoxFont)));
queryBoxStyle.Setters.Add(new Setter(TextBox.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontStyle)));
queryBoxStyle.Setters.Add(new Setter(TextBox.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontWeight)));
queryBoxStyle.Setters.Add(new Setter(TextBox.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontStretch)));
}
Style resultItemStyle = dict["ItemTitleStyle"] as Style;
Style resultSubItemStyle = dict["ItemSubTitleStyle"] as Style;
Style resultItemSelectedStyle = dict["ItemTitleSelectedStyle"] as Style;
Style resultSubItemSelectedStyle = dict["ItemSubTitleSelectedStyle"] as Style;
if (resultItemStyle != null && resultSubItemStyle != null && resultSubItemSelectedStyle != null && resultItemSelectedStyle != null)
{
Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.ResultItemFont));
Setter fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontStyle));
Setter fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontWeight));
Setter fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontStretch));
Setter[] setters = new Setter[] { fontFamily, fontStyle, fontWeight, fontStretch };
Array.ForEach(new Style[] { resultItemStyle, resultSubItemStyle, resultItemSelectedStyle, resultSubItemSelectedStyle }, o => Array.ForEach(setters, p => o.Setters.Add(p)));
}
return dict;
}
public List<string> LoadAvailableThemes()
{
List<string> themes = new List<string>();
foreach (var themeDirectory in themeDirectories)
{
themes.AddRange(
Directory.GetFiles(themeDirectory)
.Where(filePath => filePath.EndsWith(".xaml") && !filePath.EndsWith("Base.xaml"))
.ToList());
}
return themes;
}
private string GetThemePath(string themeName)
{
foreach (string themeDirectory in themeDirectories)
{
string path = Path.Combine(themeDirectory, themeName + ".xaml");
if (File.Exists(path))
{
return path;
}
}
return string.Empty;
}
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Linq;
using System.Windows;
using Wox.Core.i18n;
using Wox.Core.Theme;
namespace Wox.Core.UI
{
public class ResourceMerger
{
public static void ApplyResources()
{
var UIResourceType = typeof(IUIResource);
var UIResources = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => p.IsClass && !p.IsAbstract && UIResourceType.IsAssignableFrom(p));
Application.Current.Resources.MergedDictionaries.Clear();
foreach (var uiResource in UIResources)
{
Application.Current.Resources.MergedDictionaries.Add(
((IUIResource)Activator.CreateInstance(uiResource)).GetResourceDictionary());
}
}
}
}

View File

@@ -40,6 +40,8 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.7\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
@@ -47,6 +49,7 @@
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exception\ExceptionFormatter.cs" />
@@ -54,6 +57,9 @@
<Compile Include="Exception\WoxException.cs" />
<Compile Include="Exception\WoxHttpException.cs" />
<Compile Include="Exception\WoxJsonRPCException.cs" />
<Compile Include="i18n\InternationalizationManager.cs" />
<Compile Include="UI\IUIResource.cs" />
<Compile Include="UI\ResourceMerger.cs" />
<Compile Include="Plugin\PluginInstaller.cs" />
<Compile Include="Plugin\QueryDispatcher\IQueryDispatcher.cs" />
<Compile Include="Plugin\QueryDispatcher\QueryDispatcher.cs" />
@@ -68,6 +74,8 @@
<Compile Include="Plugin\PluginManager.cs" />
<Compile Include="Plugin\PythonPlugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Theme\FontHelper.cs" />
<Compile Include="Theme\ThemeManager.cs" />
</ItemGroup>
<ItemGroup>
<None Include="README.md" />

View File

@@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using Wox.Core.UI;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage.UserSettings;
namespace Wox.Core.i18n
{
public class InternationalizationManager : IUIResource
{
private static List<string> languageDirectories = new List<string>();
private static InternationalizationManager instance;
private static object syncObject = new object();
private InternationalizationManager() { }
public static InternationalizationManager Instance
{
get
{
if (instance == null)
{
lock (syncObject)
{
if (instance == null)
{
instance = new InternationalizationManager();
}
}
}
return instance;
}
}
static InternationalizationManager()
{
languageDirectories.Add(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Languages"));
string userProfilePath = Environment.GetEnvironmentVariable("USERPROFILE");
if (userProfilePath != null)
{
languageDirectories.Add(Path.Combine(Path.Combine(userProfilePath, ".Wox"), "Languages"));
}
MakesureThemeDirectoriesExist();
}
private static void MakesureThemeDirectoriesExist()
{
foreach (string pluginDirectory in languageDirectories)
{
if (!Directory.Exists(pluginDirectory))
{
try
{
Directory.CreateDirectory(pluginDirectory);
}
catch (System.Exception e)
{
Log.Error(e.Message);
}
}
}
}
public void ChangeLanguage(string name)
{
string path = GetLanguagePath(name);
if (string.IsNullOrEmpty(path))
{
path = GetLanguagePath("English");
if (string.IsNullOrEmpty(path))
{
throw new System.Exception("Change Language failed");
}
}
UserSettingStorage.Instance.Language = name;
UserSettingStorage.Instance.Save();
ResourceMerger.ApplyResources();
}
public ResourceDictionary GetResourceDictionary()
{
return new ResourceDictionary
{
Source = new Uri(GetLanguagePath(UserSettingStorage.Instance.Language), UriKind.Absolute)
};
}
public List<string> LoadAvailableLanguages()
{
List<string> themes = new List<string>();
foreach (var directory in languageDirectories)
{
themes.AddRange(
Directory.GetFiles(directory)
.Where(filePath => filePath.EndsWith(".xaml"))
.Select(Path.GetFileNameWithoutExtension)
.ToList());
}
return themes;
}
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 name)
{
foreach (string directory in languageDirectories)
{
string path = Path.Combine(directory, name + ".xaml");
if (File.Exists(path))
{
return path;
}
}
return string.Empty;
}
}
}