-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLocaleManager.cs
More file actions
80 lines (77 loc) · 3.18 KB
/
Copy pathLocaleManager.cs
File metadata and controls
80 lines (77 loc) · 3.18 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
using BepInEx.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace WeaponBuildMaster
{
//本地化管理器
public static class LocaleManager
{
//配置入口
//当前选择的语言, 默认为英语, 默认自带中英双语
public static ConfigEntry<string> CurrentLanguage;
//存储载入的本地化字典
private static readonly Dictionary<string, Dictionary<string, string>> _loadedTranslations = new Dictionary<string, Dictionary<string, string>>();
//fallback的语言
private const string FallbackLangName = "English";
//配置初始化
public static void Init(ConfigFile config)
{
string dirPath = Path.Combine(PluginsCore.pluginDir, "locales");
_loadedTranslations.Clear();
List<string> availableLanguages = new List<string>();
//遍历文件
string[] jsonFiles = Directory.GetFiles(dirPath, "*.json");
foreach (string file in jsonFiles)
{
try
{
string json = File.ReadAllText(file);
LocaleData data = JsonConvert.DeserializeObject<LocaleData>(json);
if (data != null && !string.IsNullOrEmpty(data.Language) && data.Translate != null)
{
//加载
_loadedTranslations[data.Language] = data.Translate;
availableLanguages.Add(data.Language);
}
}
catch (Exception e)
{
Console.WriteLine($"[EFTBallisticCalculator] UI Locale Load Error ({file}): {e.Message}");
}
}
//空字典防御
if (availableLanguages.Count == 0)
{
availableLanguages.Add(FallbackLangName);
_loadedTranslations[FallbackLangName] = new Dictionary<string, string>();
}
//生成配置项
CurrentLanguage = config.Bind(
"Language / 语言",
"Language / 语言设置",
availableLanguages.Contains(FallbackLangName) ? FallbackLangName : availableLanguages[0],
new ConfigDescription(
"Change display language (Applies immediately). / 更改游戏内Mod内容的显示语言(即时生效)。",
new AcceptableValueList<string>(availableLanguages.ToArray()) // <--- 动态下拉框
));
}
public static string Get(string key)
{
//读取当前语言
if (_loadedTranslations.TryGetValue(CurrentLanguage.Value, out var currentDict))
{
if (currentDict.TryGetValue(key, out var text)) return text;
}
//fallback
if (_loadedTranslations.TryGetValue(FallbackLangName, out var fallbackDict))
{
if (fallbackDict.TryGetValue(key, out var fallbackText)) return fallbackText;
}
//fallback都读不到, 返回key
return key;
}
}
}