|
1 | 1 | package org.localizedmc.sublangview.helper; |
2 | 2 |
|
| 3 | +import net.minecraft.client.resource.language.TranslationStorage; |
| 4 | +import net.minecraft.component.DataComponentTypes; |
| 5 | +import net.minecraft.item.ItemStack; |
| 6 | +import net.minecraft.registry.Registries; |
| 7 | +import net.minecraft.text.Text; |
| 8 | +import net.minecraft.text.TranslatableTextContent; |
| 9 | +import net.minecraft.util.Formatting; |
| 10 | + |
3 | 11 | public class TranslationHelper { |
| 12 | + public static TranslationStorage subTranslationStorage; |
| 13 | + |
| 14 | + public static boolean isMissingSubTranslation(String key) { |
| 15 | + return subTranslationStorage == null || !subTranslationStorage.hasTranslation(key); |
| 16 | + } |
| 17 | + |
| 18 | + public static Text getOriginalText(String originalText) { |
| 19 | + return Text.translatable("gui.sublangview.original", ConfigHelper.getConfig().subLanguageCode, originalText).formatted(ConfigHelper.getConfig().textColor.formatting()); |
| 20 | + } |
| 21 | + |
| 22 | + public static Text getTranslationKeyText(String translationKey) { |
| 23 | + return Text.translatable("gui.sublangview.translation_key", translationKey).formatted(ConfigHelper.getConfig().textColor.formatting()); |
| 24 | + } |
| 25 | + |
| 26 | + public static Text getNamespaceText(String namespace) { |
| 27 | + return Text.translatable("gui.sublangview.namespace", namespace).formatted(ConfigHelper.getConfig().textColor.formatting()); |
| 28 | + } |
| 29 | + |
| 30 | + public static class Item { |
| 31 | + public static Text getOriginalText(ItemStack itemStack) { |
| 32 | + if (!isMissingSubTranslation(itemStack.getItem().getTranslationKey()) && !itemStack.hasChangedComponent(DataComponentTypes.CUSTOM_NAME)) { |
| 33 | + Text name = itemStack.getName(); |
| 34 | + String subName = subTranslationStorage.get(itemStack.getItem().getTranslationKey()); |
| 35 | + if (!subName.equals(name.getString()) && !subName.equals(itemStack.getItem().getTranslationKey())) { |
| 36 | + return Text.literal(subName); |
| 37 | + } |
| 38 | + } |
| 39 | + return itemStack.getName(); |
| 40 | + } |
| 41 | + |
| 42 | + public static Text getTranslationKey(ItemStack itemStack) { |
| 43 | + if (!itemStack.hasChangedComponent(DataComponentTypes.CUSTOM_NAME)) { |
| 44 | + String translationKey = itemStack.getItem().getTranslationKey(); |
| 45 | + return Text.literal(translationKey); |
| 46 | + } |
| 47 | + return Text.literal(itemStack.getItem().getTranslationKey()); |
| 48 | + } |
| 49 | + |
| 50 | + public static String getNamespace(ItemStack itemStack) { |
| 51 | + return Registries.ITEM.getId(itemStack.getItem()).getNamespace(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static class Advancement { |
| 56 | + public static Text getTitleOriginalText(Text titleText) { |
| 57 | + if (titleText instanceof TranslatableTextContent content) { |
| 58 | + if (!isMissingSubTranslation(content.getKey())) { |
| 59 | + String originalText = subTranslationStorage.get(content.getKey()); |
| 60 | + if (!originalText.equals(titleText.getString()) && !originalText.equals(content.getKey())) { |
| 61 | + return Text.literal(originalText); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return titleText; |
| 67 | + } |
| 68 | + |
| 69 | + public static Text getTitleTranslationKey(Text titleText) { |
| 70 | + if (titleText instanceof TranslatableTextContent content) { |
| 71 | + return Text.literal(content.getKey()); |
| 72 | + } |
| 73 | + |
| 74 | + return titleText; |
| 75 | + } |
| 76 | + |
| 77 | + public static Text getDescOriginalText(Text descText) { |
| 78 | + if (descText instanceof TranslatableTextContent content) { |
| 79 | + if (!isMissingSubTranslation(content.getKey())) { |
| 80 | + String originalText = subTranslationStorage.get(content.getKey()); |
| 81 | + if (!originalText.equals(descText.getString()) && !originalText.equals(content.getKey())) { |
| 82 | + return Text.literal(originalText); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return descText; |
| 88 | + } |
| 89 | + |
| 90 | + public static Text getDescTranslationKey(Text descText) { |
| 91 | + if (descText instanceof TranslatableTextContent content) { |
| 92 | + return Text.literal(content.getKey()); |
| 93 | + } |
| 94 | + |
| 95 | + return descText; |
| 96 | + } |
| 97 | + } |
4 | 98 | } |
0 commit comments