Skip to content

Commit

Permalink
Automatically translate all untranslated TranslatableTexts
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomate0613 committed Nov 11, 2023
1 parent 09cd8af commit f8696ad
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/main/java/net/schmanguage/Schmanguage.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package net.schmanguage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Schmanguage {
public static Set<String> languageKeys = new CopyOnWriteArraySet<>();
private static final Pattern PATTERN = Pattern.compile("(§[0-9a-fklmnor]|\\b)([b-df-hj-np-tv-xz]*[aeiouy])(\\w*)\\b", Pattern.CASE_INSENSITIVE);

public static String translate(String text) {
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/net/schmanguage/mixin/ClientLanguageMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.schmanguage.mixin;

import net.minecraft.client.resources.language.ClientLanguage;
import net.minecraft.locale.Language;
import net.minecraft.server.packs.resources.Resource;
import net.schmanguage.Schmanguage;
import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Mixin(ClientLanguage.class)
public abstract class ClientLanguageMixin {
@Shadow @Final private static Logger LOGGER;

@Redirect(method = "loadFrom", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/resources/language/ClientLanguage;appendFrom(Ljava/lang/String;Ljava/util/List;Ljava/util/Map;)V"))
private static void appendFrom(String languageIdentifier, List<Resource> resourceStack, Map<String, String> map) {
for (Resource resource : resourceStack) {
try {
try (InputStream inputStream = resource.open()) {
Language.loadFromJson(inputStream, (key, value) -> {
if(languageIdentifier.equals("en_schm"))
Schmanguage.languageKeys.add(key);

map.put(key, value);
});
}
} catch (IOException iOException) {
LOGGER.warn("Failed to load translations for {} from pack {}", languageIdentifier, resource.sourcePackId(), iOException);
}
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/net/schmanguage/mixin/TranslatableContentsMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.schmanguage.mixin;

import net.minecraft.network.chat.contents.TranslatableContents;
import net.schmanguage.Schmanguage;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;

@Mixin(TranslatableContents.class)
public abstract class TranslatableContentsMixin {
@Shadow @Final private String key;


@ModifyVariable(method = "decompose", at = @At(value = "STORE", ordinal = 0), ordinal = 0)
String translate(String string) {
if(Schmanguage.languageKeys.contains(this.key))
return string;

return Schmanguage.translate(string);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/schmanguage.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"minVersion": "0.8",
"package": "net.schmanguage.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": ["LiteralContentsMixin"],
"mixins": ["LiteralContentsMixin", "TranslatableContentsMixin", "ClientLanguageMixin"],
"client": [],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit f8696ad

Please sign in to comment.