Skip to content

Commit

Permalink
Fix overriding vanilla translations (#4187)
Browse files Browse the repository at this point in the history
  • Loading branch information
apple502j authored Oct 26, 2024
1 parent 89cb0a4 commit e82f21f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
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 {
Expand All @@ -51,6 +53,18 @@ private static Map<String, String> create(Map<String, String> map) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public void onInitializeServer() {
}

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");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"text.fabric-resource-loader-v0-testmod.server.lang.override": "Vanilla override test"
}

0 comments on commit e82f21f

Please sign in to comment.