forked from nomi-san/parsec-vdd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
125 lines (108 loc) · 3.85 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media;
namespace ParsecVDisplay
{
public partial class App : Application
{
public static string[] Languages => LanguageDicts.Keys.ToArray();
static Dictionary<string, ResourceDictionary> LanguageDicts;
static ResourceDictionary CurrentLanguage;
protected override void OnStartup(StartupEventArgs e)
{
// Disable GPU to prevent flickering when adding display
RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
base.OnStartup(e);
var silent = e.Args.Contains("-silent");
var window = new MainWindow();
if (!silent)
{
window.Show();
}
SetLanguage(Config.Language, false);
}
public static void LoadTranslations()
{
LanguageDicts = new Dictionary<string, ResourceDictionary>();
var assembly = ResourceAssembly;
var rm = new ResourceManager(assembly.GetName().Name + ".g", assembly);
try
{
var list = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true);
foreach (DictionaryEntry item in list)
{
if (item.Key is string key
&& key.StartsWith("languages/"))
{
try
{
var source = key
.Replace("languages/", "/Languages/")
.Replace(".baml", ".xaml");
var sri = GetResourceStream(new Uri(source, UriKind.Relative));
var resources = LoadBaml<ResourceDictionary>(sri.Stream);
var name = resources["lang_name"].ToString();
LanguageDicts.Add(name, resources);
}
catch
{
}
}
}
}
finally
{
rm.ReleaseAllResources();
}
SetLanguage(Config.Language, saveConfig: false);
}
public static void SetLanguage(string name, bool saveConfig = true)
{
if (LanguageDicts.TryGetValue(name, out var dict))
{
CurrentLanguage = dict;
if (Current != null)
{
Current.Dispatcher.Invoke(() =>
{
Current.Resources.MergedDictionaries.Add(dict);
});
}
if (saveConfig)
{
Config.Language = name;
}
}
}
public static string GetTranslation(string key, params object[] args)
{
if (CurrentLanguage != null)
{
if (CurrentLanguage.Contains(key))
{
var t = CurrentLanguage[key]
.ToString()
.Replace("\\n", "\n");
return string.Format(t, args);
}
}
return string.Format("{{{{{0}}}}}", key);
}
static T LoadBaml<T>(Stream stream)
{
ParserContext pc = new ParserContext();
MethodInfo loadBamlMethod = typeof(XamlReader).GetMethod("LoadBaml",
BindingFlags.NonPublic | BindingFlags.Static);
return (T)loadBamlMethod.Invoke(null, new object[] { stream, pc, null, false });
}
}
}