mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
Globalisation Changes
This commit is contained in:
parent
4a80b089b4
commit
034d7401d1
15 changed files with 410 additions and 51 deletions
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
|
||||
namespace ServerManagerTool.Plugin.Common.Events
|
||||
{
|
||||
public class ResourceDictionaryChangedEventArgs : EventArgs
|
||||
{
|
||||
public ResourceDictionaryChangedEventArgs(string languageCode)
|
||||
{
|
||||
LanguageCode = languageCode;
|
||||
}
|
||||
|
||||
public string LanguageCode;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using ServerManagerTool.Plugin.Common.Delegates;
|
||||
using ServerManagerTool.Plugin.Common.Events;
|
||||
using ServerManagerTool.Plugin.Common.Lib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
|
@ -15,6 +16,7 @@ namespace ServerManagerTool.Plugin.Common
|
|||
{
|
||||
private const string PLUGINFILE_FOLDER = "Plugins";
|
||||
private const string PLUGINFILE_EXTENSION = "dll";
|
||||
public const string LANGUAGECODE_FALLBACK = "en-US";
|
||||
|
||||
private static volatile PluginHelper _instance;
|
||||
private static readonly object _syncLock = new object();
|
||||
|
|
@ -24,9 +26,12 @@ namespace ServerManagerTool.Plugin.Common
|
|||
private FetchProfilesDelegate _fetchProfilesCallback;
|
||||
private bool _disposed;
|
||||
|
||||
public EventHandler<ResourceDictionaryChangedEventArgs> ResourceDictionaryChanged;
|
||||
|
||||
private PluginHelper()
|
||||
{
|
||||
BetaEnabled = false;
|
||||
LanguageCode = LANGUAGECODE_FALLBACK;
|
||||
Plugins = new ObservableCollection<PluginItem>();
|
||||
}
|
||||
|
||||
|
|
@ -61,6 +66,12 @@ namespace ServerManagerTool.Plugin.Common
|
|||
set;
|
||||
}
|
||||
|
||||
public string LanguageCode
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public ObservableCollection<PluginItem> Plugins
|
||||
{
|
||||
get;
|
||||
|
|
@ -236,6 +247,12 @@ namespace ServerManagerTool.Plugin.Common
|
|||
}
|
||||
}
|
||||
|
||||
public void OnResourceDictionaryChanged(string languageCode)
|
||||
{
|
||||
LanguageCode = languageCode;
|
||||
ResourceDictionaryChanged?.Invoke(this, new ResourceDictionaryChangedEventArgs(languageCode));
|
||||
}
|
||||
|
||||
internal void OpenConfigForm(string pluginCode, Window owner)
|
||||
{
|
||||
if (Plugins == null)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,10 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace ServerManagerTool.Plugin.Common
|
||||
{
|
||||
|
|
@ -21,5 +26,111 @@ namespace ServerManagerTool.Plugin.Common
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void RemoveExceptResourceDictionary(Window window, string dictionaryName)
|
||||
{
|
||||
if (window == null || string.IsNullOrWhiteSpace(dictionaryName))
|
||||
return;
|
||||
|
||||
var dictionariesToRemove = window.Resources.MergedDictionaries.Where(d => !d.Source.OriginalString.Contains(dictionaryName)).ToList();
|
||||
if (dictionariesToRemove != null)
|
||||
{
|
||||
foreach (var dictionaryToRemove in dictionariesToRemove)
|
||||
{
|
||||
window.Resources.MergedDictionaries.Remove(dictionaryToRemove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveExceptResourceDictionary(UserControl control, string dictionaryName)
|
||||
{
|
||||
if (control == null || string.IsNullOrWhiteSpace(dictionaryName))
|
||||
return;
|
||||
|
||||
var dictionariesToRemove = control.Resources.MergedDictionaries.Where(d => !d.Source.OriginalString.Contains(dictionaryName)).ToList();
|
||||
if (dictionariesToRemove != null)
|
||||
{
|
||||
foreach (var dictionaryToRemove in dictionariesToRemove)
|
||||
{
|
||||
control.Resources.MergedDictionaries.Remove(dictionaryToRemove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveResourceDictionary(Window window, string dictionaryName)
|
||||
{
|
||||
if (window == null || string.IsNullOrWhiteSpace(dictionaryName))
|
||||
return;
|
||||
|
||||
var dictionaryToRemove = window.Resources.MergedDictionaries.FirstOrDefault(d => d.Source.OriginalString.Contains(dictionaryName));
|
||||
if (dictionaryToRemove != null)
|
||||
{
|
||||
window.Resources.MergedDictionaries.Remove(dictionaryToRemove);
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveResourceDictionary(UserControl control, string dictionaryName)
|
||||
{
|
||||
if (control == null || string.IsNullOrWhiteSpace(dictionaryName))
|
||||
return;
|
||||
|
||||
var dictionaryToRemove = control.Resources.MergedDictionaries.FirstOrDefault(d => d.Source.OriginalString.Contains(dictionaryName));
|
||||
if (dictionaryToRemove != null)
|
||||
{
|
||||
control.Resources.MergedDictionaries.Remove(dictionaryToRemove);
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdateResourceDictionary(Window window, string languageCode)
|
||||
{
|
||||
if (window == null)
|
||||
return;
|
||||
|
||||
RemoveExceptResourceDictionary(window, PluginHelper.LANGUAGECODE_FALLBACK);
|
||||
|
||||
var assembly = Assembly.GetCallingAssembly();
|
||||
|
||||
var resourcePath = assembly.GetManifestResourceNames().FirstOrDefault(r => r.EndsWith($"{languageCode}.xaml"));
|
||||
if (string.IsNullOrWhiteSpace(resourcePath))
|
||||
return;
|
||||
|
||||
using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(stream))
|
||||
{
|
||||
var resourceDictionary = XamlReader.Load(reader.BaseStream) as ResourceDictionary;
|
||||
if (resourceDictionary != null)
|
||||
{
|
||||
window.Resources.MergedDictionaries.Add(resourceDictionary);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdateResourceDictionary(UserControl control, string languageCode)
|
||||
{
|
||||
if (control == null)
|
||||
return;
|
||||
|
||||
RemoveExceptResourceDictionary(control, PluginHelper.LANGUAGECODE_FALLBACK);
|
||||
|
||||
var assembly = Assembly.GetCallingAssembly();
|
||||
|
||||
var resourcePath = assembly.GetManifestResourceNames().FirstOrDefault(r => r.EndsWith($"{languageCode}.xaml"));
|
||||
if (string.IsNullOrWhiteSpace(resourcePath))
|
||||
return;
|
||||
|
||||
using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(stream))
|
||||
{
|
||||
var resourceDictionary = XamlReader.Load(reader.BaseStream) as ResourceDictionary;
|
||||
if (resourceDictionary != null)
|
||||
{
|
||||
control.Resources.MergedDictionaries.Add(resourceDictionary);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue