-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModLocale.cs
More file actions
113 lines (98 loc) · 3.05 KB
/
Copy pathModLocale.cs
File metadata and controls
113 lines (98 loc) · 3.05 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
using System;
using System.Collections.Generic;
using System.Reflection;
using BepInEx.Logging;
using MossLib.Base;
using MossLib.Tool;
namespace MossLib.Example;
public class ModLocale : ModLocaleBase
{
private static ModLocale _instance;
private static ManualLogSource _logger;
public static void Initialize(ManualLogSource logger)
{
if (_instance != null)
return;
_logger = logger;
_instance = new ModLocale();
_instance.Initialize(logger, Assembly.GetExecutingAssembly());
}
public static string GetFormat(string key, params object[] args)
{
if (_instance == null) return $"[{key}]";
try
{
var result = _instance.GetStringFormatted(key, args);
return string.IsNullOrEmpty(result) ? $"[{key}]" : result;
}
catch (Exception ex)
{
Warning($"Failed to get formatted string for key: {key}, error: {ex.Message}");
return $"[{key}]";
}
}
public static string Get(string key)
{
if (_instance == null) return $"[{key}]";
try
{
var result = _instance.GetString(key);
return string.IsNullOrEmpty(result) ? $"[{key}]" : result;
}
catch (Exception ex)
{
Warning($"Failed to get string for key: {key}, error: {ex.Message}");
return $"[{key}]";
}
}
public static bool ContainsKey(string key)
{
return _instance != null && _instance.HasKey(key);
}
public static string GetOnDictionary(string dictionary, string key)
{
if (_instance == null) return $"[{key}]";
try
{
var result = _instance.GetStringOnDictionary(dictionary, key);
return string.IsNullOrEmpty(result) ? $"[{key}]" : result;
}
catch (Exception ex)
{
Warning($"Failed to get string from dictionary: {dictionary}, key: {key}, error: {ex.Message}");
return $"[{key}]";
}
}
public static string[] GetArray(string key)
{
if (_instance == null) return [$"[{key}]"];
try
{
var result = _instance.GetStringArray(key);
return result == null || result.Length == 0 ? [$"[{key}]"] : result;
}
catch (Exception ex)
{
Warning($"Failed to get string array for key: {key}, error: {ex.Message}");
return [$"[{key}]"];
}
}
public static Dictionary<string, string> GetDictionary(string key)
{
if (_instance == null) return new Dictionary<string, string>();
try
{
var result = _instance.GetStringDictionary(key);
return result ?? new Dictionary<string, string>();
}
catch (Exception ex)
{
Warning($"Failed to get string dictionary for key: {key}, error: {ex.Message}");
return new Dictionary<string, string>();
}
}
private static void Warning(string text)
{
Log.Warning(text, _logger);
}
}