Skip to content

Commit a9abc90

Browse files
committed
Add full multi files support
1 parent b660cc2 commit a9abc90

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

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

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
public class TextManager {
2929
private final QuickShop plugin;
3030
private final Distribution distribution;
31-
private final File overrideFilesFolder;
3231
// <File <Locale, Section>>
3332
private final Map<String, Map<String, JsonConfiguration>> locale2ContentMapping = new HashMap<>();
3433
private final static String languageFileCrowdin = "/master/src/main/resources/lang/%locale%/messages.json";
@@ -38,11 +37,17 @@ public class TextManager {
3837
public TextManager(QuickShop plugin) {
3938
this.plugin = plugin;
4039
this.distribution = new CrowdinOTA(plugin);
41-
this.overrideFilesFolder = new File(plugin.getDataFolder(), "lang-override");
42-
this.overrideFilesFolder.mkdirs();
4340
load();
4441
}
4542

43+
@NotNull
44+
private File getOverrideFilesFolder(@NotNull String crowdinPath) {
45+
File file = new File(crowdinPath);
46+
File folder = new File(new File(plugin.getDataFolder(),"overrides"), file.getName() + ".overrides");
47+
folder.mkdirs();
48+
return folder;
49+
}
50+
4651
public void load() {
4752
plugin.getLogger().info("Checking for translation updates...");
4853
locale2ContentMapping.clear();
@@ -53,38 +58,39 @@ public void load() {
5358
bundledLang.loadFromString(new String(IOUtils.toByteArray(new InputStreamReader(plugin.getResource("lang-original/messages.json")), StandardCharsets.UTF_8)));
5459
} catch (IOException | InvalidConfigurationException ex) {
5560
bundledLang = new JsonConfiguration();
56-
plugin.getLogger().log(Level.SEVERE,"Cannot load bundled language file from Jar, some strings may missing!",ex);
61+
plugin.getLogger().log(Level.SEVERE, "Cannot load bundled language file from Jar, some strings may missing!", ex);
5762
}
5863
// init file mapping
59-
locale2ContentMapping.computeIfAbsent(languageFileCrowdin, e->new HashMap<>());
64+
locale2ContentMapping.computeIfAbsent(languageFileCrowdin, e -> new HashMap<>());
6065
// Multi File and Multi-Language loader
61-
distribution.getAvailableLanguages().parallelStream().forEach(crowdinCode -> {
66+
67+
68+
distribution.getAvailableLanguages().parallelStream().forEach(crowdinCode -> distribution.getAvailableFiles().parallelStream().forEach(crowdinFile -> {
6269
try {
6370
// load OTA text from Crowdin
6471

65-
String minecraftCode = crowdinCode.toLowerCase(Locale.ROOT).replace("-","_");
72+
String minecraftCode = crowdinCode.toLowerCase(Locale.ROOT).replace("-", "_");
6673

67-
Util.debugLog("Loading translation for locale: " + crowdinCode+" ("+minecraftCode+")");
74+
Util.debugLog("Loading translation for locale: " + crowdinCode + " (" + minecraftCode + ")");
6875
JsonConfiguration configuration = new JsonConfiguration();
6976
try {
70-
configuration.loadFromString(distribution.getFile(languageFileCrowdin,crowdinCode));
77+
configuration.loadFromString(distribution.getFile(crowdinFile, crowdinCode));
7178
} catch (InvalidConfigurationException exception) {
72-
configuration.loadFromString(distribution.getFile(languageFileCrowdin, crowdinCode, true));
79+
configuration.loadFromString(distribution.getFile(crowdinFile, crowdinCode, true));
7380
}
7481
// load override text (allow user modification the translation)
7582
JsonConfiguration override = new JsonConfiguration();
76-
File localOverrideFile = new File(overrideFilesFolder, minecraftCode + ".json");
83+
File localOverrideFile = new File(getOverrideFilesFolder(crowdinFile), minecraftCode + ".json");
7784
if (localOverrideFile.exists()) {
7885
override.loadFromString(Util.readToString(localOverrideFile));
7986
for (String key : override.getKeys(true)) {
80-
if (key.equals("language-version"))
87+
if (key.equals("language-version") || key.equals("config-version") || key.equals("version"))
8188
continue;
8289
configuration.set(key, override.get(key));
8390
}
8491
}
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"));
92+
locale2ContentMapping.get(languageFileCrowdin).computeIfAbsent(minecraftCode, e -> configuration);
93+
Util.debugLog("Locale " + crowdinFile);
8894
if (configuration.getInt("language-version") < bundledLang.getInt("language-version"))
8995
Util.debugLog("Locale " + crowdinCode + " file version is outdated, some string will fallback to English.");
9096
} catch (CrowdinOTA.OTAException e) {
@@ -94,7 +100,7 @@ public void load() {
94100
} catch (Exception e) {
95101
plugin.getLogger().log(Level.WARNING, "Couldn't update the translation for locale " + crowdinCode + ".", e);
96102
}
97-
});
103+
}));
98104

99105

100106
// for (String availableLanguage : distribution.getAvailableLanguages()) {
@@ -175,20 +181,20 @@ private List<String> postProcess(@NotNull List<String> text) {
175181
@NotNull
176182
public List<String> forLocale(@NotNull String locale) {
177183
JsonConfiguration index = mapping.get(locale);
178-
if(index == null){
179-
if(locale.equals("en_us")){
184+
if (index == null) {
185+
if (locale.equals("en_us")) {
180186
List<String> str = fallbackLocal();
181187
if (str.isEmpty())
182188
return Collections.singletonList("Fallback Missing Language Key: " + path + ", report to QuickShop!");
183189
return postProcess(str);
184-
}else{
190+
} else {
185191
return forLocale("en_us");
186192
}
187-
}else{
193+
} else {
188194
List<String> str = index.getStringList(path);
189-
if(str.isEmpty()) {
195+
if (str.isEmpty()) {
190196
return Collections.singletonList("Missing Language Key: " + path);
191-
}else {
197+
} else {
192198
return postProcess(str);
193199
}
194200
}
@@ -257,16 +263,16 @@ private String postProcess(@NotNull String text) {
257263
@NotNull
258264
public String forLocale(@NotNull String locale) {
259265
JsonConfiguration index = mapping.get(locale);
260-
if(index == null){
261-
if(locale.equals("en_us")){
266+
if (index == null) {
267+
if (locale.equals("en_us")) {
262268
String str = fallbackLocal();
263269
if (str == null)
264270
return "Fallback Missing Language Key: " + path + ", report to QuickShop!";
265271
return postProcess(str);
266-
}else{
272+
} else {
267273
return forLocale("en_us");
268274
}
269-
}else{
275+
} else {
270276
String str = index.getString(path);
271277
if (str == null)
272278
return "Missing Language Key: " + path;

0 commit comments

Comments
 (0)