Move some translation keys to constants

This way we can also reference them in datagen.

It might make sense to move these all into one big class. I'm really not
sure — it's nice having them defined close to where they're used, but is
a little awkward at times.
This commit is contained in:
Jonathan Coates committed 2026-11-15 18:00:48 +00:00
1 parent d2995bc6aa
commit 343d5215de
6 files changed
+67 -39

No files matched your search

@@ -51,8 +51,14 @@ public abstract class AbstractComputerScreen<T extends AbstractComputerMenu> ext
private static final Logger LOG = LoggerFactory.getLogger(AbstractComputerScreen.class);
private static final Component OK = Component.translatable("gui.ok");
private static final Component NO_RESPONSE_TITLE = Component.translatable("gui.computercraft.upload.no_response");
private static final Component NO_RESPONSE_MSG = Component.translatable("gui.computercraft.upload.no_response.msg",
public static final Component NAME_TOO_LONG = Component.translatable("gui.computercraft.upload.failed.name_too_long");
public static final Component UPLOAD_CORRUPTED = Component.translatable("gui.computercraft.upload.failed.corrupted");
public static final Component CANNOT_COMPUTE_CHECKSUM = Component.translatable("gui.computercraft.upload.failed.generic", "Cannot compute checksum");
public static final Component TOO_MANY_FILES = Component.translatable("gui.computercraft.upload.failed.too_many_files");
public static final Component NO_RESPONSE_TITLE = Component.translatable("gui.computercraft.upload.no_response");
public static final Component NO_RESPONSE_MSG = Component.translatable("gui.computercraft.upload.no_response.msg",
Component.literal("import").withStyle(ChatFormatting.DARK_GRAY));
protected @Nullable TerminalWidget terminal;
@@ -186,7 +192,7 @@ public abstract class AbstractComputerScreen<T extends AbstractComputerMenu> ext
var fileName = file.getFileName();
var name = fileName == null ? "unknown" : fileName.toString();
if (name.length() > UploadFileMessage.MAX_FILE_NAME) {
alert(UploadResult.FAILED_TITLE, Component.translatable("gui.computercraft.upload.failed.name_too_long"));
alert(UploadResult.FAILED_TITLE, NAME_TOO_LONG);
return;
}
@@ -196,19 +202,19 @@ public abstract class AbstractComputerScreen<T extends AbstractComputerMenu> ext
var digest = FileUpload.getDigest(buffer);
if (digest == null) {
alert(UploadResult.FAILED_TITLE, Component.translatable("gui.computercraft.upload.failed.corrupted"));
alert(UploadResult.FAILED_TITLE, UPLOAD_CORRUPTED);
return;
}
toUpload.add(new FileUpload(name, buffer, digest));
} catch (IOException e) {
LOG.error("Failed uploading files", e);
alert(UploadResult.FAILED_TITLE, Component.translatable("gui.computercraft.upload.failed.generic", "Cannot compute checksum"));
alert(UploadResult.FAILED_TITLE, CANNOT_COMPUTE_CHECKSUM);
}
}
if (toUpload.size() > UploadFileMessage.MAX_FILES) {
alert(UploadResult.FAILED_TITLE, Component.translatable("gui.computercraft.upload.failed.too_many_files"));
alert(UploadResult.FAILED_TITLE, TOO_MANY_FILES);
return;
}
@@ -28,6 +28,8 @@ import static dan200.computercraft.core.util.Nullability.assertNonNull;
* @param <T> The concrete type of the associated menu.
*/
public class NoTermComputerScreen<T extends AbstractComputerMenu> extends Screen implements MenuAccess<T> {
public static final Component HELP = Component.translatable("gui.computercraft.pocket_computer_overlay");
private final T menu;
protected final UserComputerInput computerInput;
protected final ClientComputerActions computerActions;
@@ -99,7 +101,7 @@ public class NoTermComputerScreen<T extends AbstractComputerMenu> extends Screen
super.render(graphics, mouseX, mouseY, partialTicks);
var font = minecraft().font;
var lines = font.split(Component.translatable("gui.computercraft.pocket_computer_overlay"), (int) (width * 0.8));
var lines = font.split(HELP, (int) (width * 0.8));
var y = 10;
for (var line : lines) {
graphics.drawString(font, line, (width / 2) - (font.width(line) / 2), y, 0xFFFFFF, true);
@@ -19,6 +19,12 @@ import java.util.function.Consumer;
* Registers buttons to interact with a computer.
*/
public final class ComputerSidebar {
public static final Component TURN_ON = Component.translatable("gui.computercraft.tooltip.turn_on");
public static final Component SHUT_DOWN_MSG = Component.translatable("gui.computercraft.tooltip.turn_off");
public static final Component SHUT_DOWN_KEY = Component.translatable("gui.computercraft.tooltip.turn_off.key");
public static final Component TERMINATE_MSG = Component.translatable("gui.computercraft.tooltip.terminate");
public static final Component TERMINATE_KEY = Component.translatable("gui.computercraft.tooltip.terminate.key");
private static final int ICON_WIDTH = 12;
private static final int ICON_HEIGHT = 12;
private static final int ICON_MARGIN = 2;
@@ -38,11 +44,8 @@ public final class ComputerSidebar {
x += CORNERS_BORDER + 1;
y += CORNERS_BORDER + ICON_MARGIN;
var turnOn = new HintedMessage(Component.translatable("gui.computercraft.tooltip.turn_on"), (Component) null);
var turnOff = new HintedMessage(
Component.translatable("gui.computercraft.tooltip.turn_off"),
Component.translatable("gui.computercraft.tooltip.turn_off.key")
);
var turnOn = new HintedMessage(TURN_ON, (Component) null);
var turnOff = new HintedMessage(SHUT_DOWN_MSG, SHUT_DOWN_KEY);
add.accept(new DynamicImageButton(
x, y, ICON_WIDTH, ICON_HEIGHT,
h -> isOn.getAsBoolean() ? GuiSprites.TURNED_ON.get(h) : GuiSprites.TURNED_OFF.get(h),
@@ -56,10 +59,7 @@ public final class ComputerSidebar {
x, y, ICON_WIDTH, ICON_HEIGHT,
GuiSprites.TERMINATE::get,
b -> actions.terminate(),
new HintedMessage(
Component.translatable("gui.computercraft.tooltip.terminate"),
Component.translatable("gui.computercraft.tooltip.terminate.key")
)
new HintedMessage(TERMINATE_MSG, TERMINATE_KEY)
));
}
@@ -10,19 +10,26 @@ import dan200.computercraft.api.ComputerCraftTags;
import dan200.computercraft.api.pocket.PocketUpgradeDataProvider;
import dan200.computercraft.api.turtle.TurtleUpgradeDataProvider;
import dan200.computercraft.api.upgrades.UpgradeBase;
import dan200.computercraft.client.gui.AbstractComputerScreen;
import dan200.computercraft.client.gui.NoTermComputerScreen;
import dan200.computercraft.client.gui.widgets.ComputerSidebar;
import dan200.computercraft.core.metrics.Metric;
import dan200.computercraft.core.metrics.Metrics;
import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.command.arguments.ComputerSelector;
import dan200.computercraft.shared.computer.metrics.basic.Aggregate;
import dan200.computercraft.shared.computer.metrics.basic.AggregatedMetric;
import dan200.computercraft.shared.computer.upload.UploadResult;
import dan200.computercraft.shared.config.ConfigFile;
import dan200.computercraft.shared.config.ConfigSpec;
import dan200.computercraft.shared.peripheral.modem.wired.CableBlockEntity;
import dan200.computercraft.shared.platform.RegistryWrappers;
import dan200.computercraft.shared.turtle.upgrades.TurtleStorage;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
@@ -124,8 +131,8 @@ public final class LanguageProvider implements DataProvider {
add(TurtleStorage.DATA_PACK_TRANSLATION, "Turtle storage upgrades");
add("chat.computercraft.wired_modem.peripheral_connected", "Peripheral \"%s\" connected to network");
add("chat.computercraft.wired_modem.peripheral_disconnected", "Peripheral \"%s\" disconnected from network");
add(CableBlockEntity.PERIPHERAL_CONNECTED, "Peripheral \"%s\" connected to network");
add(CableBlockEntity.PERIPHERAL_DISCONNECTED, "Peripheral \"%s\" disconnected from network");
// Commands
add("commands.computercraft.synopsis", "Various commands for controlling computers.");
@@ -206,21 +213,21 @@ public final class LanguageProvider implements DataProvider {
add("gui.computercraft.terminal", "Computer terminal");
add("gui.computercraft.tooltip.computer_id", "Computer ID: %s");
add("gui.computercraft.tooltip.disk_id", "Disk ID: %s");
add("gui.computercraft.tooltip.turn_on", "Turn this computer on");
add("gui.computercraft.tooltip.turn_off", "Turn this computer off");
add("gui.computercraft.tooltip.turn_off.key", "Hold Ctrl+S");
add("gui.computercraft.tooltip.terminate", "Stop the currently running code");
add("gui.computercraft.tooltip.terminate.key", "Hold Ctrl+T");
add("gui.computercraft.upload.failed", "Upload Failed");
add("gui.computercraft.upload.failed.computer_off", "You must turn the computer on before uploading files.");
add("gui.computercraft.upload.failed.too_much", "Your files are too large to be uploaded.");
add("gui.computercraft.upload.failed.name_too_long", "File names are too long to be uploaded.");
add("gui.computercraft.upload.failed.too_many_files", "Cannot upload this many files.");
add("gui.computercraft.upload.failed.generic", "Uploading files failed (%s)");
add("gui.computercraft.upload.failed.corrupted", "Files corrupted when uploading. Please try again.");
add("gui.computercraft.upload.no_response", "Transferring Files");
add("gui.computercraft.upload.no_response.msg", "Your computer has not used your transferred files. You may need to run the %s program and try again.");
add("gui.computercraft.pocket_computer_overlay", "Pocket computer open. Press ESC to close.");
add(ComputerSidebar.TURN_ON, "Turn this computer on");
add(ComputerSidebar.SHUT_DOWN_MSG, "Turn this computer off");
add(ComputerSidebar.SHUT_DOWN_KEY, "Hold Ctrl+S");
add(ComputerSidebar.TERMINATE_MSG, "Stop the currently running code");
add(ComputerSidebar.TERMINATE_KEY, "Hold Ctrl+T");
add(UploadResult.FAILED_TITLE, "Upload Failed");
add(UploadResult.COMPUTER_OFF_MSG, "You must turn the computer on before uploading files.");
add(UploadResult.TOO_MUCH_MSG, "Your files are too large to be uploaded.");
add(AbstractComputerScreen.NAME_TOO_LONG, "File names are too long to be uploaded.");
add(AbstractComputerScreen.TOO_MANY_FILES, "Cannot upload this many files.");
add(AbstractComputerScreen.CANNOT_COMPUTE_CHECKSUM, "Uploading files failed (%s)");
add(AbstractComputerScreen.UPLOAD_CORRUPTED, "Files corrupted when uploading. Please try again.");
add(AbstractComputerScreen.NO_RESPONSE_TITLE, "Transferring Files");
add(AbstractComputerScreen.NO_RESPONSE_MSG, "Your computer has not used your transferred files. You may need to run the %s program and try again.");
add(NoTermComputerScreen.HELP, "Pocket computer open. Press ESC to close.");
// Config options
addConfigEntry(ConfigSpec.computerSpaceLimit, "Computer space limit (bytes)");
@@ -310,6 +317,18 @@ public final class LanguageProvider implements DataProvider {
translations.put(id, text);
}
private void add(Component component, String text) {
Objects.requireNonNull(component, "component cannot be null");
Objects.requireNonNull(text, "text cannot be null");
if (!component.getSiblings().isEmpty()) throw new IllegalArgumentException("Component cannot have siblings");
if (!(component.getContents() instanceof TranslatableContents translatable)) {
throw new IllegalArgumentException("Component must be translatable");
}
add(translatable.getKey(), text);
}
private void add(Item item, String text) {
add(item.getDescriptionId(), text);
}
@@ -29,6 +29,9 @@ import org.jspecify.annotations.Nullable;
import java.util.Objects;
public class CableBlockEntity extends BlockEntity {
public static final String PERIPHERAL_DISCONNECTED = "chat.computercraft.wired_modem.peripheral_disconnected";
public static final String PERIPHERAL_CONNECTED = "chat.computercraft.wired_modem.peripheral_connected";
private final class CableElement extends WiredModemElement {
@Override
public Level getLevel() {
@@ -133,12 +136,10 @@ public class CableBlockEntity extends BlockEntity {
if (!Objects.equals(newName, oldName)) {
if (oldName != null) {
player.displayClientMessage(Component.translatable("chat.computercraft.wired_modem.peripheral_disconnected",
ChatHelpers.copy(oldName)), false);
player.displayClientMessage(Component.translatable(PERIPHERAL_DISCONNECTED, ChatHelpers.copy(oldName)), false);
}
if (newName != null) {
player.displayClientMessage(Component.translatable("chat.computercraft.wired_modem.peripheral_connected",
ChatHelpers.copy(newName)), false);
player.displayClientMessage(Component.translatable(PERIPHERAL_CONNECTED, ChatHelpers.copy(newName)), false);
}
}
@@ -132,8 +132,8 @@ public class WiredModemFullBlockEntity extends BlockEntity {
var periphNames = getConnectedPeripheralNames();
if (!Objects.equals(periphNames, oldPeriphNames)) {
sendPeripheralChanges(player, "chat.computercraft.wired_modem.peripheral_disconnected", oldPeriphNames);
sendPeripheralChanges(player, "chat.computercraft.wired_modem.peripheral_connected", periphNames);
sendPeripheralChanges(player, CableBlockEntity.PERIPHERAL_DISCONNECTED, oldPeriphNames);
sendPeripheralChanges(player, CableBlockEntity.PERIPHERAL_CONNECTED, periphNames);
}
return InteractionResult.CONSUME;