Skip to content

Commit 443e519

Browse files
committed
mixin to locale and I18n to access the key map directly to fix LangKey
1 parent 27d78ee commit 443e519

File tree

6 files changed

+86
-1
lines changed

6 files changed

+86
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package gregtech.api.mui;
2+
3+
import net.minecraft.client.resources.Locale;
4+
5+
public interface LocaleAccessor {
6+
7+
String gregtech$getRawKey(String s);
8+
9+
ThreadLocal<LocaleAccessor> accessor = new ThreadLocal<>();
10+
11+
static String getRawKey(String s) {
12+
if (accessor.get() == null) return s;
13+
return accessor.get().gregtech$getRawKey(s);
14+
}
15+
16+
static void setLocale(Locale locale) {
17+
accessor.set((LocaleAccessor) locale);
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package gregtech.mixins.minecraft;
2+
3+
import gregtech.api.mui.LocaleAccessor;
4+
5+
import net.minecraft.client.resources.I18n;
6+
import net.minecraft.client.resources.Locale;
7+
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.injection.At;
10+
import org.spongepowered.asm.mixin.injection.Inject;
11+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
12+
13+
@Mixin(I18n.class)
14+
public abstract class L18nMixin {
15+
16+
@Inject(method = "setLocale", at = @At("HEAD"))
17+
private static void getLocale(Locale i18nLocaleIn, CallbackInfo ci) {
18+
LocaleAccessor.setLocale(i18nLocaleIn);
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package gregtech.mixins.minecraft;
2+
3+
import gregtech.api.mui.LocaleAccessor;
4+
5+
import net.minecraft.client.resources.Locale;
6+
7+
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.Shadow;
9+
10+
import java.util.Map;
11+
12+
@Mixin(Locale.class)
13+
public abstract class LocaleMixin implements LocaleAccessor {
14+
15+
@Shadow
16+
Map<String, String> properties;
17+
18+
@Override
19+
public String gregtech$getRawKey(String s) {
20+
return this.properties.get(s);
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package gregtech.mixins.mui2;
2+
3+
import gregtech.api.mui.LocaleAccessor;
4+
5+
import com.cleanroommc.modularui.drawable.text.BaseKey;
6+
import com.cleanroommc.modularui.drawable.text.LangKey;
7+
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.injection.At;
9+
import org.spongepowered.asm.mixin.injection.Redirect;
10+
11+
@Mixin(value = LangKey.class, remap = false)
12+
public abstract class LangKeyMixin extends BaseKey {
13+
14+
@Redirect(method = "getFormatted",
15+
at = @At(value = "INVOKE",
16+
target = "Lnet/minecraft/client/resources/I18n;format(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;"))
17+
public String getTranslateKey(String translateKey, Object[] parameters) {
18+
return LocaleAccessor.getRawKey(translateKey);
19+
}
20+
}

src/main/resources/mixins.gregtech.minecraft.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
"client": [
1818
"BlockMixin",
1919
"EntityRendererMixin",
20+
"L18nMixin",
2021
"LayerArmorBaseMixin",
2122
"LayerCustomHeadMixin",
23+
"LocaleMixin",
2224
"RecipeRepairItemMixin",
2325
"RegionRenderCacheBuilderMixin",
2426
"RenderChunkMixin",

src/main/resources/mixins.gregtech.mui2.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"maxShiftBy": 10
99
},
1010
"mixins": [],
11-
"client": [],
11+
"client": [
12+
"LangKeyMixin"
13+
],
1214
"server": []
1315
}

0 commit comments

Comments
 (0)