1using System.Globalization;
3using System.Text.Json.Nodes;
11 public static string Ok;
16 public static string On;
17 public static string Off;
25 public static string Yes;
26 public static string No;
48 if (!
LanguageTypes.TryGetValue(languageType, out CultureInfo cultureInfo)) {
50 throw new Exception($
"Language {languageType} not supported.");
83 string txt =
new StreamReader(stream).ReadToEnd();
90 Log.
Error($
"Invalid json file, reason: {e}");
103 string txt =
new StreamReader(stream).ReadToEnd();
104 if (txt.Length > 0) {
105 JsonNode newJsonNode;
109 catch (Exception e) {
110 Log.
Error($
"Invalid json file, reason: {e}");
124 Ok =
Get(
"Usual",
"ok");
129 On =
Get(
"Usual",
"on");
130 Off =
Get(
"Usual",
"off");
138 Yes =
Get(
"Usual",
"yes");
139 No =
Get(
"Usual",
"no");
146 Ok ??=
Get(
"Usual",
"ok");
151 On ??=
Get(
"Usual",
"on");
152 Off ??=
Get(
"Usual",
"off");
160 Yes ??=
Get(
"Usual",
"yes");
161 No ??=
Get(
"Usual",
"no");
171 || newNode ==
null) {
174 switch (newNode.GetValueKind()) {
175 case JsonValueKind.Object: {
176 if (oldNode.GetValueKind() == JsonValueKind.Object) {
177 JsonObject oldObject = oldNode.AsObject();
178 JsonObject newObject = newNode.AsObject();
179 foreach (KeyValuePair<string, JsonNode> newChild
in newObject) {
180 if (newChild.Value ==
null) {
183 if (oldObject.TryGetPropertyValue(newChild.Key, out JsonNode oldChild)) {
187 oldObject.Add(newChild.Key, newChild.Value.DeepClone());
196 case JsonValueKind.Array: {
197 if (oldNode.GetValueKind() == JsonValueKind.Array) {
198 JsonArray oldArray = oldNode.AsArray();
199 JsonArray newArray = newNode.AsArray();
200 if (newArray.Count > oldArray.Count) {
201 for (
int i = 0; i < oldArray.Count; i++) {
204 for (
int i = oldArray.Count; i < newArray.Count; i++) {
205 oldArray.Add(newArray[i]?.DeepClone());
209 for (
int i = 0; i < newArray.Count; i++) {
219 case JsonValueKind.String:
220 case JsonValueKind.Number:
221 case JsonValueKind.True:
222 case JsonValueKind.False: {
230 switch (oldNode.Parent) {
231 case JsonObject parentObject:
232 parentObject[oldNode.GetPropertyName()] = newNode;
234 case JsonArray parentArray: parentArray[oldNode.GetElementIndex()] = newNode;
break;
247 public static string Get(
string className,
int key) =>
249 Get(className, key.ToString());
253 public static string Get(params
string[] key) =>
Get(out
bool _, key);
255 public static string Get(out
bool r, params
string[] keys) {
259 return string.Join(
':', keys);
273 public static string Get(out
bool r, JsonNode node, params
string[] keys) {
275 JsonNode nowNode = node;
277 foreach (
string key
in keys) {
278 if (
string.IsNullOrEmpty(key)
279 || nowNode ==
null) {
282 if (nowNode.GetValueKind() == JsonValueKind.Object) {
283 nowNode = nowNode[key];
284 if (nowNode ==
null) {
289 else if (nowNode.GetValueKind() == JsonValueKind.Array
290 &&
int.TryParse(key, out
int num)
292 JsonArray array = nowNode.AsArray();
293 if (num < array.Count) {
294 nowNode = array[num];
305 if (nowNode !=
null) {
306 switch (nowNode.GetValueKind()) {
307 case JsonValueKind.String:
309 return nowNode.GetValue<
string>();
310 case JsonValueKind.Number:
312 return nowNode.GetValue<decimal>().ToString(CultureInfo.InvariantCulture);
315 return flag ? keys.Last() :
string.Join(
':', keys);
318 public static bool TryGet(out
string result, params
string[] keys) {
319 result =
Get(out
bool r, keys);
323 public static bool TryGet(out
string result, JsonNode node, params
string[] keys) {
324 result =
Get(out
bool r, node, keys);
330 public static string GetBlock(
string blockName,
string prop) {
335 public static bool TryGetBlock(
string blockName,
string prop, out
string result) {
336 if (blockName.Length == 0) {
337 result =
string.Empty;
340 string[] nm = blockName.Split(
':');
341 result =
Get(out
bool r,
"Blocks", nm.Length < 2 ? $
"{blockName}:0" : blockName, prop);
343 result =
Get(out r,
"Blocks", $
"{nm[0]}:0", prop);
352 public static string GetDatabase(
string name,
string prop) =>
Get(
"Database", name, prop);
354 public static string GetFireworks(
string name,
string prop) =>
Get(
"FireworksBlock", name, prop);
359 if (languageType ==
"en-US"
371 string title = $
"{(SettingsManager.SafeMode ? $"[{
LanguageControl.
Get(
"Usual",
"safeMode")}]
" : "")}{Get("Usual
", "gameName
")} {ModsManager.ShortGameVersion} - API {ModsManager.APIVersionString}";
373 title = $
"[{Get("Usual
", "debug
")}]{title}";
377 Dictionary<string, object> objs = [];
379 Type type = c.Value.GetType();
380#pragma warning disable IL2072
381 object obj = Activator.CreateInstance(type);
382#pragma warning restore IL2072
383 objs.Add(c.Key, obj);
385 foreach (KeyValuePair<string, object> c
in objs) {
404 "en-US" =>
"English (United States)",
405 "zh-CN" =>
"中文 (中国)",
406 "ro-RO" =>
"română (România)",
407 "ru-RU" =>
"русский (Россия)",
408 "es-419" =>
"español (Latinoamérica)",
409 "vi-VN" =>
"Tiếng Việt (Việt Nam)",
410 "zh-CN-old" =>
"[旧] 中文 (中国)",
416 CultureInfo oldUICulture = Thread.CurrentThread.CurrentUICulture;
419 foreach ((
string name, CultureInfo cultureInfo) in
LanguageTypes) {
420 string nativeName = cultureInfo.NativeName;
421 string displayName = cultureInfo.DisplayName;
422 if (name ==
"zh-CN-old") {
431 Thread.CurrentThread.CurrentUICulture = oldUICulture;
435 IOrderedEnumerable<KeyValuePair<string, string>> sorted =
CachedLanguageFullNames.OrderBy(item => item.Key
switch {
448 item => item is KeyValuePair<string, string> pair
static partial void SetDocumentLang(string lang)
static void Error(object message)
static string TitlePrefix
virtual void Initialize()
static void ShowDialog(ContainerWidget parentWidget, Dialog dialog)
static readonly JsonDocumentOptions DefaultJsonOptions
static void ReplaceJsonNode(JsonNode oldNode, JsonNode newNode)
static string CurrentLanguageName
static string GetWorldPalette(int index)
static string GetBlock(string blockName, string prop)
static CultureInfo CurrentLanguageCultureInfo
static void ChangeLanguage(string languageType)
static string GetWithoutFallback(out bool r, params string[] keys)
static void Initialize(string languageType)
static string GetContentWidgets(string name, string prop)
static void loadJson(Stream stream)
static string Get(out bool r, params string[] keys)
static string Get(out bool r, JsonNode node, params string[] keys)
static Dictionary< string, string > CachedLanguageFullNames
static string GetFireworks(string name, string prop)
static void LoadEnglishJson(Stream stream)
static Dictionary< string, CultureInfo > LanguageTypes
语言标识符、与相应的CultureInfo
static void SetUsual(bool force=false)
static void CreateLanguageSelectionDialog(Widget parent)
static bool TryGetBlock(string blockName, string prop, out string result)
static string GetDatabase(string name, string prop)
static string Unavailable
static JsonNode englishJsonNode
static string GetContentWidgets(string name, int pos)
static bool TryGet(out string result, params string[] keys)
static bool TryGet(out string result, JsonNode node, params string[] keys)
static string LName()
当前设置中的语言的标识符,如果是在加载完成后获取语言,建议改用CurrentLanguageName
static string Get(params string[] key)
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
static void MergeJsonNode(JsonNode oldNode, JsonNode newNode)
virtual void LoadLauguage()
初始化语言包
static void SwitchScreen(string name, params object[] parameters)
static Dictionary< string, Screen > m_screens
static void SetConfig(string key, string value)
static List< ModEntity > ModList
所有已启用的模组
static Dictionary< string, string > Configs