-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslationProvider.cs
More file actions
51 lines (43 loc) · 1.5 KB
/
Copy pathTranslationProvider.cs
File metadata and controls
51 lines (43 loc) · 1.5 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace RDPChecker
{
public static class TranslationProvider
{
public static Dictionary<string, string> Translations { get; private set; } = new();
public static string LangFolder { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "lang");
public static string CurrentLang { get; private set; } = "fr";
public static event EventHandler LanguageChanged;
public static void Load(string langCode)
{
CurrentLang = langCode;
string path = Path.Combine(LangFolder, $"{langCode}.json");
if (!Directory.Exists(LangFolder))
Directory.CreateDirectory(LangFolder);
if (!File.Exists(path))
{
Translations = new();
LanguageChanged?.Invoke(null, EventArgs.Empty);
return;
}
try
{
var json = File.ReadAllText(path);
Translations = JsonSerializer.Deserialize<Dictionary<string, string>>(json) ?? new();
}
catch
{
Translations = new();
}
LanguageChanged?.Invoke(null, EventArgs.Empty);
}
public static string T(string key)
{
if (Translations != null && Translations.TryGetValue(key, out var v))
return v;
return key; // fallback
}
}
}