Skip to content

Commit 50ebd7f

Browse files
committed
Full multi-files support
1 parent 2779ce3 commit 50ebd7f

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

src/main/java/org/maxgamer/quickshop/util/language/text/TextManager.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ public class TextManager {
3030
private final Distribution distribution;
3131
// <File <Locale, Section>>
3232
private final Map<String, Map<String, JsonConfiguration>> locale2ContentMapping = new HashMap<>();
33+
private final Map<String, JsonConfiguration> bundledFile2ContentMapping = new HashMap<>();
3334
private final static String languageFileCrowdin = "/master/src/main/resources/lang/%locale%/messages.json";
34-
public List<PostProcessor> postProcessors = new ArrayList<>();
35-
private JsonConfiguration bundledLang = new JsonConfiguration();
35+
public final List<PostProcessor> postProcessors = new ArrayList<>();
36+
3637

3738
public TextManager(QuickShop plugin) {
3839
this.plugin = plugin;
@@ -53,25 +54,29 @@ private void reset(){
5354
postProcessors.clear();
5455
}
5556

56-
private void loadBundled(){
57+
private JsonConfiguration loadBundled(String file){
58+
JsonConfiguration bundledLang = new JsonConfiguration();
5759
try {
58-
bundledLang.loadFromString(new String(IOUtils.toByteArray(new InputStreamReader(plugin.getResource("lang-original/messages.json")), StandardCharsets.UTF_8)));
60+
File fileObject = new File(file);
61+
bundledLang.loadFromString(new String(IOUtils.toByteArray(new InputStreamReader(plugin.getResource("lang-original/" +fileObject.getName())), StandardCharsets.UTF_8)));
5962
} catch (IOException | InvalidConfigurationException ex) {
6063
bundledLang = new JsonConfiguration();
6164
plugin.getLogger().log(Level.SEVERE, "Cannot load bundled language file from Jar, some strings may missing!", ex);
6265
}
66+
return bundledLang;
6367
}
6468

6569
public void load() {
6670
plugin.getLogger().info("Checking for translation updates...");
6771
this.reset();
6872

69-
// Read bundled language files
70-
this.loadBundled();
7173
// Initial file mapping
7274
locale2ContentMapping.computeIfAbsent(languageFileCrowdin, e -> new HashMap<>()); // Prevent nullportinter exception
7375
distribution.getAvailableFiles().forEach(file-> locale2ContentMapping.computeIfAbsent(file, e -> new HashMap<>()));
7476

77+
// Read bundled language files
78+
distribution.getAvailableFiles().forEach(crowdinFile-> this.bundledFile2ContentMapping.computeIfAbsent(crowdinFile, e->loadBundled(crowdinFile)));
79+
7580
// Multi File and Multi-Language loader
7681
distribution.getAvailableLanguages().parallelStream().forEach(crowdinCode -> distribution.getAvailableFiles().parallelStream().forEach(crowdinFile -> {
7782
try {
@@ -118,27 +123,27 @@ public void load() {
118123
}
119124

120125
public Text of(@NotNull String path, String... args) {
121-
return new Text(this, (CommandSender) null, locale2ContentMapping.get(languageFileCrowdin), path, args);
126+
return new Text(this, (CommandSender) null, locale2ContentMapping.get(languageFileCrowdin),bundledFile2ContentMapping.get(languageFileCrowdin), path, args);
122127
}
123128

124129
public Text of(@Nullable CommandSender sender, @NotNull String path, String... args) {
125-
return new Text(this, sender, locale2ContentMapping.get(languageFileCrowdin), path, args);
130+
return new Text(this, sender, locale2ContentMapping.get(languageFileCrowdin),bundledFile2ContentMapping.get(languageFileCrowdin), path, args);
126131
}
127132

128133
public Text of(@Nullable UUID sender, @NotNull String path, String... args) {
129-
return new Text(this, sender, locale2ContentMapping.get(languageFileCrowdin), path, args);
134+
return new Text(this, sender, locale2ContentMapping.get(languageFileCrowdin),bundledFile2ContentMapping.get(languageFileCrowdin), path, args);
130135
}
131136

132137
public TextList ofList(@NotNull String path, String... args) {
133-
return new TextList(this, (CommandSender) null, locale2ContentMapping.get(languageFileCrowdin), path, args);
138+
return new TextList(this, (CommandSender) null, locale2ContentMapping.get(languageFileCrowdin),bundledFile2ContentMapping.get(languageFileCrowdin), path, args);
134139
}
135140

136141
public TextList ofList(@Nullable UUID sender, @NotNull String path, String... args) {
137-
return new TextList(this, sender, locale2ContentMapping.get(languageFileCrowdin), path, args);
142+
return new TextList(this, sender, locale2ContentMapping.get(languageFileCrowdin),bundledFile2ContentMapping.get(languageFileCrowdin), path, args);
138143
}
139144

140145
public TextList ofList(@Nullable CommandSender sender, @NotNull String path, String... args) {
141-
return new TextList(this, sender, locale2ContentMapping.get(languageFileCrowdin), path, args);
146+
return new TextList(this, sender, locale2ContentMapping.get(languageFileCrowdin),bundledFile2ContentMapping.get(languageFileCrowdin), path, args);
142147
}
143148

144149
public static class TextList {
@@ -148,30 +153,33 @@ public static class TextList {
148153
private final Map<String, JsonConfiguration> mapping;
149154
private final CommandSender sender;
150155
private final String[] args;
156+
private final JsonConfiguration bundled;
151157

152-
private TextList(TextManager manager, CommandSender sender, Map<String, JsonConfiguration> mapping, String path, String... args) {
158+
private TextList(TextManager manager, CommandSender sender, Map<String, JsonConfiguration> mapping, JsonConfiguration bundled, String path, String... args) {
153159
this.plugin = manager.plugin;
154160
this.manager = manager;
155161
this.sender = sender;
156162
this.mapping = mapping;
163+
this.bundled = bundled;
157164
this.path = path;
158165
this.args = args;
159166
}
160167

161-
private TextList(TextManager manager, UUID sender, Map<String, JsonConfiguration> mapping, String path, String... args) {
168+
private TextList(TextManager manager, UUID sender, Map<String, JsonConfiguration> mapping, JsonConfiguration bundled, String path, String... args) {
162169
this.plugin = manager.plugin;
163170
this.manager = manager;
164171
if (sender != null)
165172
this.sender = Bukkit.getPlayer(sender);
166173
else
167174
this.sender = null;
168175
this.mapping = mapping;
176+
this.bundled = bundled;
169177
this.path = path;
170178
this.args = args;
171179
}
172180

173181
private @NotNull List<String> fallbackLocal() {
174-
return manager.bundledLang.getStringList(path);
182+
return this.bundled.getStringList(path);
175183
}
176184

177185
@NotNull
@@ -232,31 +240,34 @@ public static class Text {
232240
private final Map<String, JsonConfiguration> mapping;
233241
private final CommandSender sender;
234242
private final String[] args;
243+
private final JsonConfiguration bundled;
235244

236-
private Text(TextManager manager, CommandSender sender, Map<String, JsonConfiguration> mapping, String path, String... args) {
245+
private Text(TextManager manager, CommandSender sender, Map<String, JsonConfiguration> mapping, JsonConfiguration bundled, String path, String... args) {
237246
this.plugin = manager.plugin;
238247
this.manager = manager;
239248
this.sender = sender;
240249
this.mapping = mapping;
241250
this.path = path;
251+
this.bundled = bundled;
242252
this.args = args;
243253
}
244254

245-
private Text(TextManager manager, UUID sender, Map<String, JsonConfiguration> mapping, String path, String... args) {
255+
private Text(TextManager manager, UUID sender, Map<String, JsonConfiguration> mapping, JsonConfiguration bundled, String path, String... args) {
246256
this.plugin = manager.plugin;
247257
this.manager = manager;
248258
if (sender != null)
249259
this.sender = Bukkit.getPlayer(sender);
250260
else
251261
this.sender = null;
252262
this.mapping = mapping;
263+
this.bundled = bundled;
253264
this.path = path;
254265
this.args = args;
255266
}
256267

257268
@Nullable
258269
private String fallbackLocal() {
259-
return manager.bundledLang.getString(path);
270+
return this.bundled.getString(path);
260271
}
261272

262273
@NotNull

0 commit comments

Comments
 (0)