Repository navigation
Expand file tree
/
Copy pathLocalization.cs
More file actions
66 lines (58 loc) · 1.96 KB
/
Copy pathLocalization.cs
File metadata and controls
66 lines (58 loc) · 1.96 KB
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
using Playnite.SDK;
using System;
using System.IO;
using System.Windows;
using System.Windows.Markup;
namespace ThemeOptions
{
//based on code from lacro59 from
//https://github.com/Lacro59/playnite-plugincommon/blob/master/Localization.cs
//
public class Localization
{
private static readonly ILogger Logger = LogManager.GetLogger();
public static void Load(string folder, string language = "en_US")
{
var dictionaries = Application.Current.Resources.MergedDictionaries;
ResourceDictionary res = LoadDictionary(folder, language);
if (res != null)
{
dictionaries.Add(res);
}
}
public static ResourceDictionary LoadDictionary(string folder, string language = "en_US")
{
var langFile = Path.Combine(folder, "Localization", language + ".xaml");
// Load localization
if (File.Exists(langFile))
{
ResourceDictionary res;
try
{
using (var stream = new StreamReader(langFile))
{
res = (ResourceDictionary)XamlReader.Load(stream.BaseStream);
res.Source = new Uri(langFile, UriKind.Absolute);
}
foreach (var key in res.Keys)
{
if (res[key] is string locString && string.IsNullOrEmpty(locString))
{
res.Remove(key);
}
}
return res;
}
catch (Exception ex)
{
Logger.Error(ex, $"Failed to parse localization file {langFile}.");
}
}
else
{
Logger.Warn($"File {langFile} not found.");
}
return null;
}
}
}