Skip to content

Commit b660cc2

Browse files
committed
Fix ton of bugs in TextManager
1 parent dfd286e commit b660cc2

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

src/main/java/org/maxgamer/quickshop/listener/PlayerListener.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,20 @@ public void onInventoryClose(InventoryCloseEvent e) {
321321

322322
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
323323
public void onJoin(PlayerJoinEvent e) {
324+
Util.debugLog("Player "+e.getPlayer().getName()+" using locale "+e.getPlayer().getLocale()+": "+plugin.text().of(e.getPlayer(),"file-test").forLocale());
325+
// Notify the player any messages they were sent
326+
if (plugin.getConfig().getBoolean("shop.auto-fetch-shop-messages")) {
327+
MsgUtil.flush(e.getPlayer());
328+
}
329+
}
330+
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
331+
public void onJoin(PlayerLocaleChangeEvent e) {
332+
Util.debugLog("Player "+e.getPlayer().getName()+" using new locale "+e.getLocale()+": "+plugin.text().of(e.getPlayer(),"file-test").forLocale(e.getLocale()));
324333
// Notify the player any messages they were sent
325334
if (plugin.getConfig().getBoolean("shop.auto-fetch-shop-messages")) {
326335
MsgUtil.flush(e.getPlayer());
327336
}
328337
}
329-
330338
@EventHandler(ignoreCancelled = true)
331339
public void onPlayerQuit(PlayerQuitEvent e) {
332340
// Remove them from the menu

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,25 @@ public void load() {
5555
bundledLang = new JsonConfiguration();
5656
plugin.getLogger().log(Level.SEVERE,"Cannot load bundled language file from Jar, some strings may missing!",ex);
5757
}
58-
distribution.getAvailableLanguages().parallelStream().forEach(availableLanguage -> {
58+
// init file mapping
59+
locale2ContentMapping.computeIfAbsent(languageFileCrowdin, e->new HashMap<>());
60+
// Multi File and Multi-Language loader
61+
distribution.getAvailableLanguages().parallelStream().forEach(crowdinCode -> {
5962
try {
6063
// load OTA text from Crowdin
61-
Util.debugLog("Loading translation for locale: " + availableLanguage);
62-
Map<String, JsonConfiguration> fileLocaleMapping = locale2ContentMapping.computeIfAbsent(languageFileCrowdin, k -> new HashMap<>());
64+
65+
String minecraftCode = crowdinCode.toLowerCase(Locale.ROOT).replace("-","_");
66+
67+
Util.debugLog("Loading translation for locale: " + crowdinCode+" ("+minecraftCode+")");
6368
JsonConfiguration configuration = new JsonConfiguration();
6469
try {
65-
configuration.loadFromString(distribution.getFile(languageFileCrowdin, availableLanguage));
70+
configuration.loadFromString(distribution.getFile(languageFileCrowdin,crowdinCode));
6671
} catch (InvalidConfigurationException exception) {
67-
configuration.loadFromString(distribution.getFile(languageFileCrowdin, availableLanguage, true));
72+
configuration.loadFromString(distribution.getFile(languageFileCrowdin, crowdinCode, true));
6873
}
69-
fileLocaleMapping.put(availableLanguage, configuration);
7074
// load override text (allow user modification the translation)
7175
JsonConfiguration override = new JsonConfiguration();
72-
File localOverrideFile = new File(overrideFilesFolder, availableLanguage + ".json");
76+
File localOverrideFile = new File(overrideFilesFolder, minecraftCode + ".json");
7377
if (localOverrideFile.exists()) {
7478
override.loadFromString(Util.readToString(localOverrideFile));
7579
for (String key : override.getKeys(true)) {
@@ -78,18 +82,21 @@ public void load() {
7882
configuration.set(key, override.get(key));
7983
}
8084
}
81-
Util.debugLog("Locale: " + availableLanguage + " has been successfully loaded.");
85+
locale2ContentMapping.get(languageFileCrowdin).computeIfAbsent(minecraftCode,e->configuration);
86+
Util.debugLog("Locale: " + crowdinCode +"("+minecraftCode+")"+ " has been successfully loaded.");
87+
Util.debugLog("Locale: " + crowdinCode+"("+minecraftCode+")"+ " test: "+configuration.getString("file-test"));
8288
if (configuration.getInt("language-version") < bundledLang.getInt("language-version"))
83-
Util.debugLog("Locale " + availableLanguage + " file version is outdated, some string will fallback to English.");
89+
Util.debugLog("Locale " + crowdinCode + " file version is outdated, some string will fallback to English.");
8490
} catch (CrowdinOTA.OTAException e) {
85-
plugin.getLogger().warning("Couldn't update the translation for locale " + availableLanguage + " because it not configured, please report to QuickShop");
91+
plugin.getLogger().warning("Couldn't update the translation for locale " + crowdinCode + " because it not configured, please report to QuickShop");
8692
} catch (IOException e) {
87-
plugin.getLogger().log(Level.WARNING, "Couldn't update the translation for locale " + availableLanguage + " please check your network connection.", e);
93+
plugin.getLogger().log(Level.WARNING, "Couldn't update the translation for locale " + crowdinCode + " please check your network connection.", e);
8894
} catch (Exception e) {
89-
plugin.getLogger().log(Level.WARNING, "Couldn't update the translation for locale " + availableLanguage + ".", e);
95+
plugin.getLogger().log(Level.WARNING, "Couldn't update the translation for locale " + crowdinCode + ".", e);
9096
}
9197
});
9298

99+
93100
// for (String availableLanguage : distribution.getAvailableLanguages()) {
94101
//
95102
// }
@@ -169,16 +176,16 @@ private List<String> postProcess(@NotNull List<String> text) {
169176
public List<String> forLocale(@NotNull String locale) {
170177
JsonConfiguration index = mapping.get(locale);
171178
if(index == null){
172-
if(locale.equals("en-US")){
179+
if(locale.equals("en_us")){
173180
List<String> str = fallbackLocal();
174181
if (str.isEmpty())
175182
return Collections.singletonList("Fallback Missing Language Key: " + path + ", report to QuickShop!");
176183
return postProcess(str);
177184
}else{
178-
return forLocale("en-US");
185+
return forLocale("en_us");
179186
}
180187
}else{
181-
List<String> str = index.getStringList(locale);
188+
List<String> str = index.getStringList(path);
182189
if(str.isEmpty()) {
183190
return Collections.singletonList("Missing Language Key: " + path);
184191
}else {
@@ -193,7 +200,7 @@ public List<String> forLocale() {
193200
if (sender instanceof Player) {
194201
return forLocale(((Player) sender).getLocale());
195202
} else {
196-
return forLocale("en");
203+
return forLocale("en_us");
197204
}
198205
}
199206

@@ -251,16 +258,16 @@ private String postProcess(@NotNull String text) {
251258
public String forLocale(@NotNull String locale) {
252259
JsonConfiguration index = mapping.get(locale);
253260
if(index == null){
254-
if(locale.equals("en-US")){
261+
if(locale.equals("en_us")){
255262
String str = fallbackLocal();
256263
if (str == null)
257264
return "Fallback Missing Language Key: " + path + ", report to QuickShop!";
258265
return postProcess(str);
259266
}else{
260-
return forLocale("en-US");
267+
return forLocale("en_us");
261268
}
262269
}else{
263-
String str = index.getString(locale);
270+
String str = index.getString(path);
264271
if (str == null)
265272
return "Missing Language Key: " + path;
266273
return postProcess(str);
@@ -272,7 +279,7 @@ public String forLocale() {
272279
if (sender instanceof Player) {
273280
return forLocale(((Player) sender).getLocale());
274281
} else {
275-
return forLocale("en-US");
282+
return forLocale("en_us");
276283
}
277284
}
278285

0 commit comments

Comments
 (0)