-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically translate all untranslated TranslatableTexts
- Loading branch information
1 parent
09cd8af
commit f8696ad
Showing
4 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/net/schmanguage/mixin/ClientLanguageMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
src/main/java/net/schmanguage/mixin/TranslatableContentsMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters