forked from Caraxi/SimpleTweaksPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loc.cs
114 lines (95 loc) · 4.17 KB
/
Loc.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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace SimpleTweaksPlugin {
public class LocalizedString {
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
}
internal static class Loc {
private static SortedDictionary<string, LocalizedString> _localizationStrings = new();
private static string currentLanguage = "en";
internal static void LoadLanguage(string langCode) {
currentLanguage = "en";
_localizationStrings = new SortedDictionary<string, LocalizedString>();
if (langCode == "en") return;
if (langCode == "DEBUG") {
currentLanguage = "DEBUG";
return;
}
string json = null;
var locDir = Service.PluginInterface.GetPluginLocDirectory();
if (string.IsNullOrWhiteSpace(locDir)) return;
var langFile = Path.Combine(locDir, $"{langCode}/strings.json");
if (File.Exists(langFile)) {
json = File.ReadAllText(langFile);
} else {
using var s = Assembly.GetExecutingAssembly().GetManifestResourceStream($"SimpleTweaksPlugin.Localization.{langCode}.json");
if (s != null) {
using var sr = new StreamReader(s);
json = sr.ReadToEnd();
if (!Directory.Exists(locDir)) Directory.CreateDirectory(locDir);
File.WriteAllText(langFile, json);
}
}
if (!string.IsNullOrWhiteSpace(json)) {
_localizationStrings = JsonConvert.DeserializeObject<SortedDictionary<string, LocalizedString>>(json);
currentLanguage = langCode;
}
}
internal static string Localize(string key, string fallbackValue, string description = null) {
if (currentLanguage == "DEBUG") return $"#{key}#";
try {
return _localizationStrings[key].Message;
} catch {
_localizationStrings[key] = new LocalizedString() {
Message = fallbackValue,
Description = description ?? $"{key} - {fallbackValue}"
};
return fallbackValue;
}
}
internal static string ExportLoadedDictionary() {
return JsonConvert.SerializeObject(_localizationStrings, Formatting.Indented);
}
internal static void ImportDictionary(string json) {
try {
_localizationStrings = JsonConvert.DeserializeObject<SortedDictionary<string, LocalizedString>>(json);
} catch {
//
}
}
public static void ClearCache() {
_localizationStrings.Clear();
}
public static void UpdateTranslations() {
DownloadError = null;
var downloadPath = Service.PluginInterface.GetPluginLocDirectory();
var zipFile = Path.Join(downloadPath, "loc.zip");
Task.Run(() => {
LoadingTranslations = true;
try {
var webClient = new WebClient();
webClient.Headers.Add("Accept: text/html, application/xhtml+xml, */*");
webClient.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
webClient.DownloadFile(new Uri("https://crowdin.com/backend/download/project/simpletweaks.zip"), zipFile);
ZipFile.ExtractToDirectory(zipFile, downloadPath, true);
File.Delete(zipFile);
LoadingTranslations = false;
} catch (Exception ex) {
LoadingTranslations = false;
DownloadError = ex;
}
});
}
public static bool LoadingTranslations { get; private set; }
public static Exception DownloadError { get; private set; }
}
}