-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathLoc.cs
More file actions
148 lines (118 loc) · 5.89 KB
/
Loc.cs
File metadata and controls
148 lines (118 loc) · 5.89 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Dalamud.Interface.ImGuiNotification;
using Newtonsoft.Json;
using SimpleTweaksPlugin.Utility;
namespace SimpleTweaksPlugin;
public class LocalizedString {
[JsonProperty("message")] public string Message { get; set; } = string.Empty;
[JsonProperty("description")] public string Description { get; set; } = string.Empty;
}
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();
}
private class CrowdinManifest {
[JsonProperty("files")] public string[] Files;
[JsonProperty("languages")] public string[] Languages;
[JsonProperty("timestamp")] public ulong Timestamp;
[JsonProperty("content")] public Dictionary<string, string[]> Content;
}
public static void UpdateTranslations(bool force = false, Action? callback = null) {
DownloadError = null;
var downloadPath = Service.PluginInterface.GetPluginLocDirectory();
var config = SimpleTweaksPlugin.Plugin.PluginConfig;
Task.Run(async () => {
LoadingTranslations = true;
try {
var httpClient = Common.HttpClient;
if (DateTime.Now - config.LanguageListUpdate > TimeSpan.FromMinutes(60) || force) {
Service.NotificationManager.AddNotification(new Notification() { Content = "Updating Language List", Minimized = true, InitialDuration = TimeSpan.FromSeconds(4)});
var manifestJson = await httpClient.GetStringAsync("https://distributions.crowdin.net/a20076cbde84bba34152668i8hw/manifest.json");
var manifest = JsonConvert.DeserializeObject<CrowdinManifest>(manifestJson);
if (manifest == null) return;
foreach (var l in manifest.Languages) {
config.LanguageUpdates.TryAdd(l, DateTime.MinValue);
}
SimpleLog.Warning(JsonConvert.SerializeObject(manifest, Formatting.Indented));
config.LanguageListUpdate = DateTime.Now;
}
if (config.LanguageUpdates.TryGetValue(config.Language, out var updateTime)) {
if (DateTime.Now - updateTime > TimeSpan.FromMinutes(60) || force) {
Service.NotificationManager.AddNotification(new Notification() { Content = $"Updating Language: {config.Language}", Minimized = true, InitialDuration = TimeSpan.FromSeconds(4)});
var languageJson = await httpClient.GetStringAsync($"https://distributions.crowdin.net/a20076cbde84bba34152668i8hw/content/{config.Language}/strings.json");
var savePath = Path.Join(downloadPath, config.Language, "strings.json");
var dir = Path.GetDirectoryName(savePath);
if (!string.IsNullOrWhiteSpace(dir)) {
new DirectoryInfo(dir).Create();
await File.WriteAllTextAsync(savePath, languageJson);
}
}
}
if (callback != null) await Service.Framework.RunOnTick(callback);
LoadingTranslations = false;
} catch (Exception ex) {
SimpleLog.Error(ex);
LoadingTranslations = false;
DownloadError = ex;
}
});
}
public static bool LoadingTranslations { get; private set; }
public static Exception? DownloadError { get; private set; }
}