mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 18:57:19 +02:00
Add multiple language. [WIP]
This commit is contained in:
121
Wox/Helper/LanguageManager.cs
Normal file
121
Wox/Helper/LanguageManager.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
public class LanguageManager
|
||||
{
|
||||
private static List<string> languageDirectories = new List<string>();
|
||||
|
||||
static LanguageManager()
|
||||
{
|
||||
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 (Exception e)
|
||||
{
|
||||
Log.Error(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void ChangeLanguage(string name)
|
||||
{
|
||||
string path = GetLanguagePath(name);
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
path = GetLanguagePath("English");
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
throw new Exception("Change Language failed");
|
||||
}
|
||||
}
|
||||
|
||||
UserSettingStorage.Instance.Language = name;
|
||||
UserSettingStorage.Instance.Save();
|
||||
ResourceMerger.ApplyResources();
|
||||
}
|
||||
|
||||
internal static ResourceDictionary GetResourceDictionary()
|
||||
{
|
||||
return new ResourceDictionary
|
||||
{
|
||||
Source = new Uri(GetLanguagePath(UserSettingStorage.Instance.Language), UriKind.Absolute)
|
||||
};
|
||||
}
|
||||
|
||||
public static 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 static string GetTranslation(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
object translation = Application.Current.FindResource(key);
|
||||
if (translation == null)
|
||||
{
|
||||
return "NoTranslation";
|
||||
}
|
||||
return translation.ToString();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "NoTranslation";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Wox/Helper/ResourceMerger.cs
Normal file
21
Wox/Helper/ResourceMerger.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
|
||||
namespace Wox.Helper
|
||||
{
|
||||
public class ResourceMerger
|
||||
{
|
||||
public static void ApplyResources()
|
||||
{
|
||||
var languageResource = LanguageManager.GetResourceDictionary();
|
||||
var themeResource = ThemeManager.GetResourceDictionary();
|
||||
|
||||
Application.Current.Resources.MergedDictionaries.Clear();
|
||||
Application.Current.Resources.MergedDictionaries.Add(languageResource);
|
||||
Application.Current.Resources.MergedDictionaries.Add(themeResource);
|
||||
}
|
||||
}
|
||||
}
|
||||
130
Wox/Helper/ThemeManager.cs
Normal file
130
Wox/Helper/ThemeManager.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 System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
|
||||
namespace Wox
|
||||
{
|
||||
internal class ThemeManager
|
||||
{
|
||||
private static List<string> themeDirectories = new List<string>();
|
||||
|
||||
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 (Exception e)
|
||||
{
|
||||
Log.Error(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void ChangeTheme(string themeName)
|
||||
{
|
||||
string themePath = GetThemePath(themeName);
|
||||
if (string.IsNullOrEmpty(themePath))
|
||||
{
|
||||
themePath = GetThemePath("Dark");
|
||||
if (string.IsNullOrEmpty(themePath))
|
||||
{
|
||||
throw new Exception("Change theme failed");
|
||||
}
|
||||
}
|
||||
|
||||
UserSettingStorage.Instance.Theme = themeName;
|
||||
UserSettingStorage.Instance.Save();
|
||||
|
||||
ResourceMerger.ApplyResources();
|
||||
}
|
||||
|
||||
internal static 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 static 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("Default.xaml"))
|
||||
.ToList());
|
||||
}
|
||||
return themes;
|
||||
}
|
||||
|
||||
private static 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user