Skip to content

Commit

Permalink
Enable Mod Menu translations on (Neo)Forge
Browse files Browse the repository at this point in the history
Unlike Fabric's ModMenu, Forge doesn't natively support mod info translations.

I worked around this using a couple of forge-specific mixins.
  • Loading branch information
MattSturgeon committed Feb 9, 2024
1 parent 65139aa commit e99d931
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and Freecam's versioning is based on [Semantic Versioning](https://semver.org/sp

### Added

- \[Fabric]: The name & description shown in the Mod Menu can now be translated ([#172](https://github.com/MinecraftFreecam/Freecam/issues/172)).
- The name & description shown in the Mod Menu can now be translated ([#172](https://github.com/MinecraftFreecam/Freecam/issues/172)).

### Changed

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.xolt.freecam.forge.mixins;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.neoforged.neoforge.client.gui.widget.ModListWidget;
import net.neoforged.neoforgespi.language.IModInfo;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import static net.xolt.freecam.Freecam.MOD_ID;

/**
* Enable translations in forge's mod list
*/
@Mixin(ModListWidget.ModEntry.class)
public class ModListEntryMixin {

@Shadow @Final private IModInfo modInfo;

@Unique
private static MutableComponent freecam$displayName() {
return Component.translatable("freecam.name");
}

@Redirect(method = "render", at = @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/network/chat/Component;literal(Ljava/lang/String;)Lnet/minecraft/network/chat/MutableComponent;"))
MutableComponent setNameToRender(String arg) {
if (MOD_ID.equals(modInfo.getModId())) {
return freecam$displayName();
}
return Component.literal(arg);
}

@Inject(method = "getNarration", at = @At(value = "HEAD"), cancellable = true)
void getNarration(CallbackInfoReturnable<Component> cir) {
if (MOD_ID.equals(modInfo.getModId())) {
cir.setReturnValue(Component.translatable("narrator.select", freecam$displayName()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.xolt.freecam.forge.mixins;

import net.minecraft.locale.Language;
import net.neoforged.neoforge.client.gui.ModListScreen;
import net.neoforged.neoforgespi.language.IModInfo;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import static net.xolt.freecam.Freecam.MOD_ID;

/**
* Enable translations in forge's mod info panel
*/
@Mixin(ModListScreen.class)
public abstract class ModListScreenMixin {

@Unique
private String freecam$translate(String key, String fallback) {
return Language.getInstance().getOrDefault(key, fallback);
}

@Redirect(method = "updateCache", at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforgespi/language/IModInfo;getDisplayName()Ljava/lang/String;"))
String getName(IModInfo instance) {
if (MOD_ID.equals(instance.getModId())) {
return freecam$translate("freecam.name", instance.getDisplayName());
}
return instance.getDisplayName();
}

@Redirect(method = "updateCache", at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforgespi/language/IModInfo;getDescription()Ljava/lang/String;"))
String getDescription(IModInfo instance) {
if (MOD_ID.equals(instance.getModId())) {
return freecam$translate("freecam.description", instance.getDescription());
}
return instance.getDescription();
}
}
2 changes: 2 additions & 0 deletions neoforge/src/main/resources/freecam-neoforge.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"compatibilityLevel": "JAVA_17",
"minVersion": "0.8",
"client": [
"ModListEntryMixin",
"ModListScreenMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit e99d931

Please sign in to comment.