Skip to content

Commit

Permalink
Combine multiple translation files instead of picking one
Browse files Browse the repository at this point in the history
  • Loading branch information
cam72cam committed Sep 9, 2023
1 parent 8a2a0a2 commit baa0310
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/main/java/cam72cam/mod/ModCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,24 +323,28 @@ public InputStream getResource(String resourcePath) throws IOException {
// Magical Translations!
ResourceLocation lang = toLang(resourcePath);
if (Minecraft.getInstance().getResourceManager().hasResource(lang)) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(Minecraft.getInstance().getResourceManager().getResource(lang).getInputStream()))) {
List<String> translations = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
String[] splits = line.split("=", 2);
if (splits.length == 2) {
String key = splits[0];
String value = splits[1];

translations.add(String.format("\"%s\": \"%s\"", key, value));
translations.add(String.format("\"%s\": \"%s\"", key.replace(":", "."), value));
translations.add(String.format("\"%s\": \"%s\"", key.replace(".name", ""), value));
translations.add(String.format("\"%s\": \"%s\"", key.replace(".name", "").replace(":", "."), value));
Map<String, String> rawTranslations = new HashMap<>();
for (IResource resource : Minecraft.getInstance().getResourceManager().getResources(lang)) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
String[] splits = line.split("=", 2);
if (splits.length == 2) {
rawTranslations.put(splits[0], splits[1]);
}
}
}
String output = "{" + String.join(",", translations) + "}";
return new ByteArrayInputStream(output.getBytes(StandardCharsets.UTF_8));
}
List<String> translations = new ArrayList<>();
rawTranslations.forEach((key, value) -> {
translations.add(String.format("\"%s\": \"%s\"", key, value));
translations.add(String.format("\"%s\": \"%s\"", key.replace(":", "."), value));
translations.add(String.format("\"%s\": \"%s\"", key.replace(".name", ""), value));
translations.add(String.format("\"%s\": \"%s\"", key.replace(".name", "").replace(":", "."), value));
});
String output = "{" + String.join(",", translations) + "}";
ModCore.info("Created translation for %s : %s", resourcePath, output);
return new ByteArrayInputStream(output.getBytes(StandardCharsets.UTF_8));
}
}
return null;
Expand Down

0 comments on commit baa0310

Please sign in to comment.