Skip to content

Commit 423d1ea

Browse files
committed
语言切换选项
1 parent d263b34 commit 423d1ea

File tree

9 files changed

+202
-90
lines changed

9 files changed

+202
-90
lines changed

MaiChartManager/AppMain.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.Globalization;
23
using SingleInstanceCore;
34
using System.Text.Json;
45
using Windows.ApplicationModel;
@@ -174,4 +175,31 @@ public void OnInstanceInvoked(string[] args)
174175
{
175176
_launcher.ShowWindow();
176177
}
178+
179+
public static void SetLocale(string locale)
180+
{
181+
if (locale != "zh" && locale != "zh-TW" && locale != "en")
182+
{
183+
throw new ArgumentException("Invalid locale. Must be 'zh', 'zh-TW', or 'en'");
184+
}
185+
186+
StaticSettings.CurrentLocale = locale;
187+
StaticSettings.Config.Locale = locale;
188+
189+
// 设置 Locale 资源管理器的 Culture(这会影响所有线程)
190+
var culture = locale switch
191+
{
192+
"zh" => new CultureInfo("zh-CN"),
193+
"zh-TW" => new CultureInfo("zh-TW"),
194+
_ => new CultureInfo("en-US"),
195+
};
196+
Locale.Culture = culture;
197+
CultureInfo.CurrentCulture = culture;
198+
CultureInfo.CurrentUICulture = culture;
199+
200+
// 保存配置文件
201+
var cfgFilePath = Path.Combine(StaticSettings.appData, "config.json");
202+
var json = JsonSerializer.Serialize(StaticSettings.Config, new JsonSerializerOptions { WriteIndented = true });
203+
System.IO.File.WriteAllText(cfgFilePath, json);
204+
}
177205
}

MaiChartManager/Controllers/App/LocaleController.cs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,6 @@ public string GetCurrentLocale()
1717
[HttpPost]
1818
public void SetLocale([FromBody] string locale)
1919
{
20-
if (locale != "zh" && locale != "zh-TW" && locale != "en")
21-
{
22-
throw new ArgumentException("Invalid locale. Must be 'zh', 'zh-TW', or 'en'");
23-
}
24-
25-
StaticSettings.CurrentLocale = locale;
26-
StaticSettings.Config.Locale = locale;
27-
28-
// 设置 Locale 资源管理器的 Culture(这会影响所有线程)
29-
var culture = locale switch
30-
{
31-
"zh" => new CultureInfo("zh-CN"),
32-
"zh-TW" => new CultureInfo("zh-TW"),
33-
_ => new CultureInfo("en-US")
34-
};
35-
Locale.Culture = culture;
36-
CultureInfo.CurrentCulture = culture;
37-
CultureInfo.CurrentUICulture = culture;
38-
39-
// 保存配置文件
40-
try
41-
{
42-
var cfgFilePath = Path.Combine(StaticSettings.appData, "config.json");
43-
var json = JsonSerializer.Serialize(StaticSettings.Config, new JsonSerializerOptions { WriteIndented = true });
44-
System.IO.File.WriteAllText(cfgFilePath, json);
45-
}
46-
catch (Exception ex)
47-
{
48-
logger.LogError(ex, "保存配置文件失败");
49-
throw;
50-
}
20+
AppMain.SetLocale(locale);
5121
}
5222
}

0 commit comments

Comments
 (0)