forked from ashpynov/PlayniteSound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Localization.cs
58 lines (51 loc) · 1.8 KB
/
Localization.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
using Playnite.SDK;
using System;
using System.IO;
using System.Windows;
using System.Windows.Markup;
using PlayniteSounds.Common.Constants;
namespace PlayniteSounds
{
//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 SetPluginLanguage(string pluginFolder, string language = SoundFile.LocalizationSource)
{
var dictionaries = Application.Current.Resources.MergedDictionaries;
var langFile = Path.Combine(pluginFolder, SoundDirectory.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);
}
}
}
catch (Exception ex)
{
Logger.Error(ex, $"Failed to parse localization file {langFile}.");
return;
}
dictionaries.Add(res);
}
else
{
Logger.Warn($"File {langFile} not found.");
}
}
}
}