mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-09 20:57:22 +02:00
Refactoring. Move system plugins to seperate DLLs.
This commit is contained in:
25
Wox.Core/i18n/AvailableLanguages.cs
Normal file
25
Wox.Core/i18n/AvailableLanguages.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Wox.Core.i18n
|
||||
{
|
||||
internal static class AvailableLanguages
|
||||
{
|
||||
public static Language English = new Language("en", "English");
|
||||
public static Language Chinese = new Language("zh-cn", "中文");
|
||||
public static Language Chinese_TW = new Language("zh-tw", "中文(繁体)");
|
||||
|
||||
public static List<Language> GetAvailableLanguages()
|
||||
{
|
||||
List<Language> languages = new List<Language>
|
||||
{
|
||||
English,
|
||||
Chinese,
|
||||
Chinese_TW
|
||||
};
|
||||
return languages;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Wox.Core/i18n/IInternationalization.cs
Normal file
18
Wox.Core/i18n/IInternationalization.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Wox.Core.i18n
|
||||
{
|
||||
interface IInternationalization
|
||||
{
|
||||
List<Language> LoadAvailableLanguages();
|
||||
|
||||
string GetTranslation(string key);
|
||||
|
||||
void ChangeLanguage(Language language);
|
||||
|
||||
void ChangeLanguage(string languageCode);
|
||||
}
|
||||
}
|
||||
130
Wox.Core/i18n/Internationalization.cs
Normal file
130
Wox.Core/i18n/Internationalization.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
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;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
|
||||
namespace Wox.Core.i18n
|
||||
{
|
||||
public class Internationalization : IInternationalization, IUIResource
|
||||
{
|
||||
private static List<string> languageDirectories = new List<string>();
|
||||
|
||||
static Internationalization()
|
||||
{
|
||||
languageDirectories.Add(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "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 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)
|
||||
{
|
||||
if(language == null) throw new WoxI18nException("language can't be null");
|
||||
|
||||
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
|
||||
{
|
||||
Source = new Uri(GetLanguagePath(UserSettingStorage.Instance.Language), UriKind.Absolute)
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private string GetLanguagePath(Language language)
|
||||
{
|
||||
foreach (string directory in languageDirectories)
|
||||
{
|
||||
string path = Path.Combine(directory, language.LanguageCode + ".xaml");
|
||||
if (File.Exists(path))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,15 +10,12 @@ using Wox.Infrastructure.Storage.UserSettings;
|
||||
|
||||
namespace Wox.Core.i18n
|
||||
{
|
||||
public class InternationalizationManager : IUIResource
|
||||
public static class InternationalizationManager
|
||||
{
|
||||
private static List<string> languageDirectories = new List<string>();
|
||||
private static InternationalizationManager instance;
|
||||
private static Internationalization instance;
|
||||
private static object syncObject = new object();
|
||||
|
||||
private InternationalizationManager() { }
|
||||
|
||||
public static InternationalizationManager Instance
|
||||
public static Internationalization Internationalization
|
||||
{
|
||||
get
|
||||
{
|
||||
@@ -28,114 +25,12 @@ namespace Wox.Core.i18n
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new InternationalizationManager();
|
||||
instance = new Internationalization();
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
23
Wox.Core/i18n/Language.cs
Normal file
23
Wox.Core/i18n/Language.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Wox.Core.i18n
|
||||
{
|
||||
public class Language
|
||||
{
|
||||
public Language(string code, string display)
|
||||
{
|
||||
LanguageCode = code;
|
||||
Display = display;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// E.g. En or Zh-CN
|
||||
/// </summary>
|
||||
public string LanguageCode { get; set; }
|
||||
|
||||
public string Display { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user