|
| 1 | +package gregtech.api.util; |
| 2 | + |
| 3 | +import net.minecraftforge.fml.common.FMLCommonHandler; |
| 4 | +import net.minecraftforge.fml.relauncher.Side; |
| 5 | + |
| 6 | +@SuppressWarnings("deprecation") |
| 7 | +public class LocalizationUtils { |
| 8 | + |
| 9 | + /** |
| 10 | + * This function calls `net.minecraft.client.resources.I18n.format` when called on client |
| 11 | + * or `net.minecraft.util.text.translation.I18n.translateToLocalFormatted` when called on server. |
| 12 | + * <ul> |
| 13 | + * <li>It is intended that translations should be done using `I18n` on the client.</li> |
| 14 | + * <li>For setting up translations on the server you should use `TextComponentTranslatable`.</li> |
| 15 | + * <li>`LocalisationUtils` is only for cases where some kind of translation is required on the server and there is no client/player in context.</li> |
| 16 | + * <li>`LocalisationUtils` is "best effort" and will probably only work properly with en-us.</li> |
| 17 | + * </ul> |
| 18 | + * @param localisationKey the localisation key passed to the underlying format function |
| 19 | + * @param substitutions the substitutions passed to the underlying format function |
| 20 | + * @return the localized string. |
| 21 | + */ |
| 22 | + public static String format(String localisationKey, Object... substitutions) { |
| 23 | + if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) { |
| 24 | + return net.minecraft.util.text.translation.I18n.translateToLocalFormatted(localisationKey, substitutions); |
| 25 | + } else { |
| 26 | + return net.minecraft.client.resources.I18n.format(localisationKey, substitutions); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * This function calls `net.minecraft.client.resources.I18n.hasKey` when called on client |
| 32 | + * or `net.minecraft.util.text.translation.I18n.canTranslate` when called on server. |
| 33 | + * <ul> |
| 34 | + * <li>It is intended that translations should be done using `I18n` on the client.</li> |
| 35 | + * <li>For setting up translations on the server you should use `TextComponentTranslatable`.</li> |
| 36 | + * <li>`LocalisationUtils` is only for cases where some kind of translation is required on the server and there is no client/player in context.</li> |
| 37 | + * <li>`LocalisationUtils` is "best effort" and will probably only work properly with en-us.</li> |
| 38 | + * </ul> |
| 39 | + * @param localisationKey the localisation key passed to the underlying hasKey function |
| 40 | + * @return a boolean indicating if the given localisation key has localisations |
| 41 | + */ |
| 42 | + public static boolean hasKey(String localisationKey) { |
| 43 | + if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) { |
| 44 | + return net.minecraft.util.text.translation.I18n.canTranslate(localisationKey); |
| 45 | + } else { |
| 46 | + return net.minecraft.client.resources.I18n.hasKey(localisationKey); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments