Fix overriding vanilla translations (#4187)

This commit is contained in:
apple502j 2024-10-26 23:54:53 +09:00 committed by GitHub
parent 89cb0a4eef
commit e82f21f7e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 0 deletions

View file

@ -35,6 +35,8 @@ import org.spongepowered.asm.mixin.injection.Redirect;
import net.minecraft.util.Language;
import net.fabricmc.fabric.impl.resource.loader.ServerLanguageUtil;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
@Mixin(Language.class)
class LanguageMixin {
@ -51,6 +53,18 @@ class LanguageMixin {
return ImmutableMap.copyOf(map);
}
@Redirect(method = "load(Ljava/util/function/BiConsumer;Ljava/lang/String;)V", at = @At(value = "INVOKE", target = "Ljava/lang/Class;getResourceAsStream(Ljava/lang/String;)Ljava/io/InputStream;"))
private static InputStream readCorrectVanillaResource(Class instance, String path) throws IOException {
ModContainer mod = FabricLoader.getInstance().getModContainer("minecraft").orElseThrow();
Path langPath = mod.findPath(path).orElse(null);
if (langPath == null) {
throw new IOException("Could not read %s from minecraft ModContainer".formatted(path));
} else {
return Files.newInputStream(langPath);
}
}
private static void loadFromPath(Path path, BiConsumer<String, String> entryConsumer) {
try (InputStream stream = Files.newInputStream(path)) {
LOGGER.debug("Loading translations from {}", path);

View file

@ -27,6 +27,8 @@ public class LanguageTestMod implements DedicatedServerModInitializer {
}
private static void testTranslationLoaded() {
testTranslationLoaded("item.minecraft.potato", "Potato"); // Test that vanilla translation loads
testTranslationLoaded("text.fabric-resource-loader-v0-testmod.server.lang.override", "Vanilla override test");
testTranslationLoaded("pack.source.fabricmod", "Fabric mod");
testTranslationLoaded("text.fabric-resource-loader-v0-testmod.server.lang.test0", "Test from fabric-resource-loader-v0-testmod");
testTranslationLoaded("text.fabric-resource-loader-v0-testmod.server.lang.test1", "Test from fabric-resource-loader-v0-testmod-test1");

View file

@ -0,0 +1,3 @@
{
"text.fabric-resource-loader-v0-testmod.server.lang.override": "Vanilla override test"
}