2015-01-02 16:16:09 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Windows;
|
2015-01-02 23:07:49 +08:00
|
|
|
|
using Wox.Core.UI;
|
2015-01-02 16:16:09 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
|
|
|
|
using Wox.Infrastructure.Storage.UserSettings;
|
|
|
|
|
|
2015-01-02 23:07:49 +08:00
|
|
|
|
namespace Wox.Core.i18n
|
2015-01-02 16:16:09 +08:00
|
|
|
|
{
|
2015-01-02 23:07:49 +08:00
|
|
|
|
public class InternationalizationManager : IUIResource
|
2015-01-02 16:16:09 +08:00
|
|
|
|
{
|
|
|
|
|
private static List<string> languageDirectories = new List<string>();
|
2015-01-02 23:07:49 +08:00
|
|
|
|
private static InternationalizationManager instance;
|
|
|
|
|
private static object syncObject = new object();
|
2015-01-02 16:16:09 +08:00
|
|
|
|
|
2015-01-02 23:07:49 +08:00
|
|
|
|
private InternationalizationManager() { }
|
|
|
|
|
|
|
|
|
|
public static InternationalizationManager Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (instance == null)
|
|
|
|
|
{
|
|
|
|
|
lock (syncObject)
|
|
|
|
|
{
|
|
|
|
|
if (instance == null)
|
|
|
|
|
{
|
|
|
|
|
instance = new InternationalizationManager();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static InternationalizationManager()
|
2015-01-02 16:16:09 +08:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
2015-01-02 23:07:49 +08:00
|
|
|
|
catch (System.Exception e)
|
2015-01-02 16:16:09 +08:00
|
|
|
|
{
|
|
|
|
|
Log.Error(e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-02 23:07:49 +08:00
|
|
|
|
public void ChangeLanguage(string name)
|
2015-01-02 16:16:09 +08:00
|
|
|
|
{
|
|
|
|
|
string path = GetLanguagePath(name);
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
path = GetLanguagePath("English");
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
2015-01-02 23:07:49 +08:00
|
|
|
|
throw new System.Exception("Change Language failed");
|
2015-01-02 16:16:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UserSettingStorage.Instance.Language = name;
|
|
|
|
|
UserSettingStorage.Instance.Save();
|
|
|
|
|
ResourceMerger.ApplyResources();
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-02 23:07:49 +08:00
|
|
|
|
public ResourceDictionary GetResourceDictionary()
|
2015-01-02 16:16:09 +08:00
|
|
|
|
{
|
|
|
|
|
return new ResourceDictionary
|
|
|
|
|
{
|
|
|
|
|
Source = new Uri(GetLanguagePath(UserSettingStorage.Instance.Language), UriKind.Absolute)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-02 23:07:49 +08:00
|
|
|
|
public List<string> LoadAvailableLanguages()
|
2015-01-02 16:16:09 +08:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-02 23:07:49 +08:00
|
|
|
|
public string GetTranslation(string key)
|
2015-01-02 16:16:09 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
object translation = Application.Current.FindResource(key);
|
|
|
|
|
if (translation == null)
|
|
|
|
|
{
|
|
|
|
|
return "NoTranslation";
|
|
|
|
|
}
|
|
|
|
|
return translation.ToString();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return "NoTranslation";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-02 23:07:49 +08:00
|
|
|
|
private string GetLanguagePath(string name)
|
2015-01-02 16:16:09 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (string directory in languageDirectories)
|
|
|
|
|
{
|
|
|
|
|
string path = Path.Combine(directory, name + ".xaml");
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|