-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLocaleData.cs
More file actions
180 lines (173 loc) · 8.06 KB
/
LocaleData.cs
File metadata and controls
180 lines (173 loc) · 8.06 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using Discord;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DonderHelper
{
public static class LocaleData
{
private static Dictionary<string, Dictionary<string, string>> dicts = [];
public static void Initialize()
{
Dictionary<string, string> AddKeys(string path)
{
if (File.Exists(path))
{
Dictionary<string, string> dict = [];
string[] strings = File.ReadAllLines(path).Where(item => !string.IsNullOrWhiteSpace(item) && !item.StartsWith("//")).ToArray();
foreach (string s in strings)
{
if (!s.Contains('=')) continue;
string[] entry = s.Split('=', 2);
dict.TryAdd(entry[0], entry[1].Replace("\\n", "\n"));
}
return dict;
}
return [];
}
foreach (string textfile in Directory.GetFiles("Lang", "*.txt", SearchOption.TopDirectoryOnly))
{
string locale = Path.GetFileNameWithoutExtension(textfile).Replace(".txt", "");
dicts.TryAdd(locale, AddKeys(textfile));
}
}
public static bool HasLanguage(string locale) => dicts.ContainsKey(locale);
public static bool HasKey(string key, string locale)
{
return TryGetString(key, locale, out _);
}
public static bool TryGetString(string input, string locale, out string? result)
{
result = null;
return dicts.TryGetValue(locale, out var dict) && dict.TryGetValue(input, out result);
}
public static string GetString(string input, string locale, params object?[] items)
{
object?[] items_safe = items != null ? items : [""];
var dict = dicts.TryGetValue(locale, out var dictionary) ? dictionary : dicts["en-US"];
for (int i = 0; i < items_safe.Length; i++)
{
if (dict.TryGetValue(items_safe[i]?.ToString() ?? "", out string? output_key))
items_safe[i] = output_key;
}
return dict.TryGetValue(input, out string? output) ? String.Format(output, items_safe) : $"INVALID KEY: {input}";
}
public static Dictionary<string, string> GetStrings(string input, params object?[] items)
{
Dictionary<string, string> strings = [];
foreach (var dict in dicts) {
strings.Add(dict.Key, GetString(input, dict.Key, items));
if (dict.Key == "en-US") strings.Add("en-GB", GetString(input, dict.Key, items));
}
return strings;
}
public static string GetAIBattleItemInfo(string name, string type, int wins, string locale)
{
return GetString("CAMPAIGN_AIBATTLE_DESC", locale, wins, GetItemInfo(name, type, locale));
}
public static string GetItemInfo(string name, string type, string locale)
{
return GetString("ITEM_INFO", locale, name, GetString(type, locale));
}
#region Song
public static string GetDifficulty(Song.SongDifficulty difficulty, string locale)
{
switch (difficulty)
{
case Song.SongDifficulty.Easy: return GetString("DIFFICULTY_EASY", locale);
case Song.SongDifficulty.Normal: return GetString("DIFFICULTY_NORMAL", locale);
case Song.SongDifficulty.Hard: return GetString("DIFFICULTY_HARD", locale);
case Song.SongDifficulty.Extreme: return GetString("DIFFICULTY_EX", locale);
case Song.SongDifficulty.Hidden: return GetString("DIFFICULTY_HIDDEN", locale);
default: return "???";
}
}
public static string GetAvailability(Song.Availability availability, string locale)
{
switch (availability)
{
case Song.Availability.No: return GetString("AVAILABLE_NO", locale);
case Song.Availability.Yes: return GetString("AVAILABLE_YES", locale);
case Song.Availability.Campaign: return GetString("AVAILABLE_CAMPAIGN", locale);
case Song.Availability.CampaignNo: return GetString("AVAILABLE_CAMPAIGNNO", locale);
case Song.Availability.Shop: return GetString("AVAILABLE_SHOP", locale);
case Song.Availability.AIBattle: return GetString("AVAILABLE_AIBATTLE", locale);
case Song.Availability.QRCode: return GetString("AVAILABLE_QRCODE", locale);
case Song.Availability.Transfer: return GetString("AVAILABLE_TRANSFER", locale);
default: return GetString("AVAILABLE_UNKNOWN", locale);
}
}
public static string GetGenreAsString(Song.SongGenre genre, string locale)
{
switch (genre)
{
case Song.SongGenre.Pop: return GetString("GENRE_POP", locale);
case Song.SongGenre.Kids: return GetString("GENRE_KIDS", locale);
case Song.SongGenre.Anime: return GetString("GENRE_ANIME", locale);
case Song.SongGenre.Game: return GetString("GENRE_GAME", locale);
case Song.SongGenre.Vocaloid: return GetString("GENRE_VOCALOID", locale);
case Song.SongGenre.Variety: return GetString("GENRE_VARIETY", locale);
case Song.SongGenre.Classical: return GetString("GENRE_CLASSICAL", locale);
case Song.SongGenre.Namco: return GetString("GENRE_NAMCO", locale);
default: return GetString("GENRE_UNKNOWN", locale);
}
}
public static Dictionary<string, string> GetGenreAsStrings(Song.SongGenre genre)
{
Dictionary<string, string> strings = [];
foreach (var dict in dicts)
strings.Add(dict.Key, GetGenreAsString(genre, dict.Key));
return strings;
}
public static string GetJapanRegionStatusAsString(Song song, string locale)
{
return GetString("AVAILABLE_REGION", locale,
GetString("REGION_JAPAN", locale),
EmoteData.GetAvailability(song.Region.Japan),
GetAvailability(song.Region.Japan, locale));
}
public static string GetAsiaRegionStatusAsString(Song song, string locale)
{
return GetString("AVAILABLE_REGION", locale,
GetString("REGION_ASIA", locale),
EmoteData.GetAvailability(song.Region.Asia),
GetAvailability(song.Region.Asia, locale));
}
public static string GetOceaniaRegionStatusAsString(Song song, string locale)
{
return GetString("AVAILABLE_REGION", locale,
GetString("REGION_OCEANIA", locale),
EmoteData.GetAvailability(song.Region.Oceania),
GetAvailability(song.Region.Oceania, locale));
}
public static string GetUSARegionStatusAsString(Song song, string locale)
{
return GetString("AVAILABLE_REGION", locale,
GetString("REGION_USA", locale),
EmoteData.GetAvailability(song.Region.UnitedStates),
GetAvailability(song.Region.UnitedStates, locale));
}
public static string GetChinaRegionStatusAsString(Song song, string locale)
{
return GetString("AVAILABLE_REGION", locale,
GetString("REGION_CHINA", locale),
EmoteData.GetAvailability(song.Region.China),
GetAvailability(song.Region.China, locale));
}
#endregion
public static string GetLocaleAsEmoji(string locale)
{
return locale switch
{
"ja" => ":flag_jp:",
"en-US" => ":flag_au:",
"ko" => ":flag_kr:",
"zh-TW" => ":flag_tw:",
"zh-CN" => ":flag_cn:",
_ => ""
};
}
}
}