Skip to content

Commit

Permalink
Replace json-styled translations with string-value translations (#1490)
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryLoenwind authored Sep 3, 2024
1 parent 9b2c819 commit 72d5ebf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ public boolean existsWithFallback(final String translationKey) {
return true;
}

/**
* If the given translation key exists, returns it formatted with the given style(s) and as prefixed with the given component.
* Otherwise returns an empty Component.
*/
public Component optional(final Component prefix, final String translationKey, final ChatFormatting... style) {
if (I18n.exists(translationKey)) {
return Component.empty().append(prefix).append(Component.translatable(translationKey).withStyle(style));
}
return Component.empty();
}

public void finish() {
if (CLIENT.logUntranslatedConfigurationWarnings.get() && !FMLLoader.isProduction() && (!untranslatables.isEmpty() || !untranslatablesWithFallback.isEmpty())) {
StringBuilder stringBuilder = new StringBuilder();
Expand Down Expand Up @@ -200,7 +211,8 @@ public void finish() {
/**
* The breadcrumb separator. Default: "%s > %s"
*/
private static final String CRUMB = LANG_PREFIX + "breadcrumb";
public static final Component CRUMB_SEPARATOR = Component.translatable(LANG_PREFIX + "breadcrumb.separator").withStyle(ChatFormatting.GOLD, ChatFormatting.BOLD);
private static final String CRUMB = LANG_PREFIX + "breadcrumb.order";
/**
* The label of list elements. Will be supplied the index into the list. Default: "%s:"
*/
Expand All @@ -209,20 +221,26 @@ public void finish() {
* How the range will be added to the tooltip when using translated tooltips. Mimics what the comment does in ModConfigSpec.
*/
private static final String RANGE_TOOLTIP = LANG_PREFIX + "rangetooltip";
private static final ChatFormatting RANGE_TOOLTIP_STYLE = ChatFormatting.GRAY;
/**
* How the filename will be added to the tooltip.
*/
private static final String FILENAME_TOOLTIP = LANG_PREFIX + "filenametooltip";
private static final ChatFormatting FILENAME_TOOLTIP_STYLE = ChatFormatting.GRAY;
/**
* A literal to create an empty line in a tooltip.
*/
private static final MutableComponent EMPTY_LINE = Component.literal("\n\n");

public static final Component TOOLTIP_CANNOT_EDIT_THIS_WHILE_ONLINE = Component.translatable(LANG_PREFIX + "notonline");
public static final Component TOOLTIP_CANNOT_EDIT_THIS_WHILE_OPEN_TO_LAN = Component.translatable(LANG_PREFIX + "notlan");
public static final Component TOOLTIP_CANNOT_EDIT_NOT_LOADED = Component.translatable(LANG_PREFIX + "notloaded");
public static final Component TOOLTIP_CANNOT_EDIT_THIS_WHILE_ONLINE = Component.translatable(LANG_PREFIX + "notonline").withStyle(ChatFormatting.RED);
public static final Component TOOLTIP_CANNOT_EDIT_THIS_WHILE_OPEN_TO_LAN = Component.translatable(LANG_PREFIX + "notlan").withStyle(ChatFormatting.RED);
public static final Component TOOLTIP_CANNOT_EDIT_NOT_LOADED = Component.translatable(LANG_PREFIX + "notloaded").withStyle(ChatFormatting.RED);
public static final Component NEW_LIST_ELEMENT = Component.translatable(LANG_PREFIX + "newlistelement");
public static final Component MOVE_LIST_ELEMENT_UP = Component.translatable(LANG_PREFIX + "listelementup");
public static final Component MOVE_LIST_ELEMENT_DOWN = Component.translatable(LANG_PREFIX + "listelementdown");
public static final Component REMOVE_LIST_ELEMENT = Component.translatable(LANG_PREFIX + "listelementremove");
public static final Component UNSUPPORTED_ELEMENT = Component.translatable(LANG_PREFIX + "unsupportedelement");
public static final Component LONG_STRING = Component.translatable(LANG_PREFIX + "longstring");
public static final Component UNSUPPORTED_ELEMENT = Component.translatable(LANG_PREFIX + "unsupportedelement").withStyle(ChatFormatting.RED);
public static final Component LONG_STRING = Component.translatable(LANG_PREFIX + "longstring").withStyle(ChatFormatting.RED);
public static final Component GAME_RESTART_TITLE = Component.translatable(LANG_PREFIX + "restart.game.title");
public static final Component GAME_RESTART_MESSAGE = Component.translatable(LANG_PREFIX + "restart.game.text");
public static final Component GAME_RESTART_YES = Component.translatable("menu.quit"); // TitleScreen.init() et.al.
Expand All @@ -231,7 +249,7 @@ public void finish() {
public static final Component RETURN_TO_MENU = Component.translatable("menu.returnToMenu"); // PauseScreen.RETURN_TO_MENU
public static final Component SAVING_LEVEL = Component.translatable("menu.savingLevel"); // PauseScreen.SAVING_LEVEL
public static final Component RESTART_NO = Component.translatable(LANG_PREFIX + "restart.return");
public static final Component RESTART_NO_TOOLTIP = Component.translatable(LANG_PREFIX + "restart.return.tooltip");
public static final Component RESTART_NO_TOOLTIP = Component.translatable(LANG_PREFIX + "restart.return.tooltip").withStyle(ChatFormatting.RED, ChatFormatting.BOLD);
public static final Component UNDO = Component.translatable(LANG_PREFIX + "undo");
public static final Component UNDO_TOOLTIP = Component.translatable(LANG_PREFIX + "undo.tooltip");
public static final Component RESET = Component.translatable(LANG_PREFIX + "reset");
Expand Down Expand Up @@ -281,19 +299,19 @@ protected void addOptions() {
button -> minecraft.setScreen(sectionScreen.apply(this, type, modConfig, translatableConfig(modConfig, ".title", LANG_PREFIX + "title." + type.name().toLowerCase(Locale.ROOT))))).width(BIG_BUTTON_WIDTH).build();
MutableComponent tooltip = Component.empty();
if (!((ModConfigSpec) modConfig.getSpec()).isLoaded()) {
tooltip.append(TOOLTIP_CANNOT_EDIT_NOT_LOADED).append(Component.literal("\n\n"));
tooltip.append(TOOLTIP_CANNOT_EDIT_NOT_LOADED).append(EMPTY_LINE);
btn.active = false;
count = 99; // prevent autoClose
} else if (type == Type.SERVER && minecraft.getCurrentServer() != null && !minecraft.isSingleplayer()) {
tooltip.append(TOOLTIP_CANNOT_EDIT_THIS_WHILE_ONLINE).append(Component.literal("\n\n"));
tooltip.append(TOOLTIP_CANNOT_EDIT_THIS_WHILE_ONLINE).append(EMPTY_LINE);
btn.active = false;
count = 99; // prevent autoClose
} else if (type == Type.SERVER && minecraft.hasSingleplayerServer() && minecraft.getSingleplayerServer().isPublished()) {
tooltip.append(TOOLTIP_CANNOT_EDIT_THIS_WHILE_OPEN_TO_LAN).append(Component.literal("\n\n"));
tooltip.append(TOOLTIP_CANNOT_EDIT_THIS_WHILE_OPEN_TO_LAN).append(EMPTY_LINE);
btn.active = false;
count = 99; // prevent autoClose
}
tooltip.append(Component.translatable(FILENAME_TOOLTIP, modConfig.getFileName()));
tooltip.append(Component.translatable(FILENAME_TOOLTIP, modConfig.getFileName()).withStyle(FILENAME_TOOLTIP_STYLE));
btn.setTooltip(Tooltip.create(tooltip));
list.addSmall(btn, null);
count++;
Expand Down Expand Up @@ -514,7 +532,7 @@ public ConfigurationSectionScreen(final Screen parent, final ModConfig.Type type
*/
public ConfigurationSectionScreen(final Context parentContext, final Screen parent, final Map<String, Object> valueSpecs, final String key,
final Set<? extends Entry> entrySet, Component title) {
this(Context.section(parentContext, parent, entrySet, valueSpecs, key), Component.translatable(CRUMB, parent.getTitle(), title));
this(Context.section(parentContext, parent, entrySet, valueSpecs, key), Component.translatable(CRUMB, parent.getTitle(), CRUMB_SEPARATOR, title));
}

@SuppressWarnings("resource")
Expand Down Expand Up @@ -558,10 +576,13 @@ protected Component getTooltipComponent(final String key, @Nullable Range<?> ran
final boolean hasTranslatedTooltip = translationChecker.existsWithFallback(tooltipKey);
MutableComponent component = Component.empty().append(getTranslationComponent(key).withStyle(ChatFormatting.BOLD));
if (hasTranslatedTooltip || !Strings.isBlank(comment)) {
component = component.append(Component.literal("\n\n")).append(Component.translatableWithFallback(tooltipKey, comment));
component = component.append(EMPTY_LINE).append(Component.translatableWithFallback(tooltipKey, comment));
}
// The "tooltip.warning" key is to be considered an internal API. It will be removed once Neo has a
// generic styling mechanism for translation texts. Use at your own risk.
component = component.append(translationChecker.optional(EMPTY_LINE, tooltipKey + ".warning", ChatFormatting.RED, ChatFormatting.BOLD));
if (hasTranslatedTooltip && range != null) {
component = component.append(Component.translatable(RANGE_TOOLTIP, range.toString()));
component = component.append(EMPTY_LINE).append(Component.translatable(RANGE_TOOLTIP, range.toString()).withStyle(RANGE_TOOLTIP_STYLE));
}
return component;
}
Expand Down Expand Up @@ -917,7 +938,7 @@ protected <T> Element createList(final String key, final ListValueSpec spec, fin
return new Element(Component.translatable(SECTION, getTranslationComponent(key)), getTooltipComponent(key, null),
Button.builder(Component.translatable(SECTION, Component.translatable(translationChecker.check(getTranslationKey(key) + ".button", SECTION_TEXT))),
button -> minecraft.setScreen(sectionCache.computeIfAbsent(key,
k -> new ConfigurationListScreen<>(Context.list(context, this), key, Component.translatable(CRUMB, this.getTitle(), getTranslationComponent(key)), spec, list)).rebuild()))
k -> new ConfigurationListScreen<>(Context.list(context, this), key, Component.translatable(CRUMB, this.getTitle(), CRUMB_SEPARATOR, getTranslationComponent(key)), spec, list)).rebuild()))
.tooltip(Tooltip.create(getTooltipComponent(key, null))).build(),
false);
}
Expand Down
104 changes: 14 additions & 90 deletions src/main/resources/assets/neoforge/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,51 +147,15 @@
"neoforge.configuration.uitext.title.common": "%s Common Configuration",
"neoforge.configuration.uitext.title.startup": "%s Startup Configuration",

"neoforge.configuration.uitext.notonline": [
{
"text": "Settings in here are determined by the server and cannot be changed while online.",
"color": "red"
}
],
"neoforge.configuration.uitext.notlan": [
{
"text": "Settings in here cannot be edited while your game is open to LAN. Please return to the main menu and load the world again.",
"color": "red"
}
],
"neoforge.configuration.uitext.notloaded": [
{
"text": "Settings in here are only available while a world is loaded.",
"color": "red"
}
],
"neoforge.configuration.uitext.unsupportedelement": [
{
"text": "This value cannot be edited in the UI. Please contact the mod author about providing a custom UI for it.",
"color": "red"
}
],
"neoforge.configuration.uitext.longstring": [
{
"text": "This value is too long to be edited in the UI. Please edit it in the config file.",
"color": "red"
}
],
"neoforge.configuration.uitext.notonline": "Settings in here are determined by the server and cannot be changed while online.",
"neoforge.configuration.uitext.notlan": "Settings in here cannot be edited while your game is open to LAN. Please return to the main menu and load the world again.",
"neoforge.configuration.uitext.notloaded": "Settings in here are only available while a world is loaded.",
"neoforge.configuration.uitext.unsupportedelement": "This value cannot be edited in the UI. Please contact the mod author about providing a custom UI for it.",
"neoforge.configuration.uitext.longstring": "This value is too long to be edited in the UI. Please edit it in the config file.",
"neoforge.configuration.uitext.section": "%s...",
"neoforge.configuration.uitext.sectiontext": "Edit",
"neoforge.configuration.uitext.breadcrumb": [
{
"index": 0
},
{
"text": " > ",
"color": "gold",
"bold": true
},
{
"index": 1
}
],
"neoforge.configuration.uitext.breadcrumb.order": "%1$s %2$s %3$s",
"neoforge.configuration.uitext.breadcrumb.separator": ">",
"neoforge.configuration.uitext.listelement": "%s:",
"neoforge.configuration.uitext.undo": "Undo",
"neoforge.configuration.uitext.undo.tooltip": "Reverts changes on this screen only.",
Expand All @@ -201,30 +165,8 @@
"neoforge.configuration.uitext.listelementup": "\u23f6",
"neoforge.configuration.uitext.listelementdown": "\u23f7",
"neoforge.configuration.uitext.listelementremove": "\u274c",
"neoforge.configuration.uitext.rangetooltip": [
{
"text": "\n\nRange: ",
"color": "gray"
},
{
"index": 0,
"color": "gray"
}
],
"neoforge.configuration.uitext.filenametooltip": [
{
"text": "File: \"",
"color": "gray"
},
{
"index": 0,
"color": "gray"
},
{
"text": "\"",
"color": "gray"
}
],
"neoforge.configuration.uitext.rangetooltip": "Range: %s",
"neoforge.configuration.uitext.filenametooltip": "File: \"%s\"",
"neoforge.configuration.uitext.common": "Common Options",
"neoforge.configuration.uitext.client": "Client Options",
"neoforge.configuration.uitext.server": "Server Options",
Expand All @@ -234,13 +176,7 @@
"neoforge.configuration.uitext.restart.server.title": "World needs to be reloaded",
"neoforge.configuration.uitext.restart.server.text": "One or more of the configuration option that were changed will only take effect when the world is reloaded.",
"neoforge.configuration.uitext.restart.return": "Ignore",
"neoforge.configuration.uitext.restart.return.tooltip": [
{
"text": "Your changes will have no effect until you restart!",
"color": "red",
"bold": true
}
],
"neoforge.configuration.uitext.restart.return.tooltip": "Your changes will have no effect until you restart!",

"neoforge.configuration.title": "NeoForge Configuration",
"neoforge.configuration.section.neoforge.client.toml": "Client settings",
Expand All @@ -264,23 +200,11 @@
"neoforge.configgui.permissionHandler": "Permission Handler",
"neoforge.configgui.permissionHandler.tooltip": "The permission handler used by the server. Defaults to neoforge:default_handler if no such handler with that name is registered.",
"neoforge.configgui.removeErroringBlockEntities": "Remove Erroring Block Entities",
"neoforge.configgui.removeErroringBlockEntities.tooltip": [
"Set this to true to remove any Entity that throws an error in its update method instead of closing the server and reporting a crash log.\n\n",
{
"text": "BE WARNED THIS COULD SCREW UP EVERYTHING.\nUSE SPARINGLY.\nWE ARE NOT RESPONSIBLE FOR DAMAGES.",
"color": "red",
"bold": true
}
],
"neoforge.configgui.removeErroringBlockEntities.tooltip": "Set this to true to remove any BlockEntity that throws an error in its update method instead of closing the server and reporting a crash log.",
"neoforge.configgui.removeErroringBlockEntities.tooltip.warning": "BE WARNED THIS COULD SCREW UP EVERYTHING.\nUSE SPARINGLY.\nWE ARE NOT RESPONSIBLE FOR DAMAGES.",
"neoforge.configgui.removeErroringEntities": "Remove Erroring Entities",
"neoforge.configgui.removeErroringEntities.tooltip": [
"Set this to true to remove any BlockEntity that throws an error in its update method instead of closing the server and reporting a crash log.\n\n",
{
"text": "BE WARNED THIS COULD SCREW UP EVERYTHING.\nUSE SPARINGLY.\nWE ARE NOT RESPONSIBLE FOR DAMAGES.",
"color": "red",
"bold": true
}
],
"neoforge.configgui.removeErroringEntities.tooltip": "Set this to true to remove any Entity that throws an error in its update method instead of closing the server and reporting a crash log.",
"neoforge.configgui.removeErroringEntities.tooltip.warning": "BE WARNED THIS COULD SCREW UP EVERYTHING.\nUSE SPARINGLY.\nWE ARE NOT RESPONSIBLE FOR DAMAGES.",
"neoforge.configgui.showLoadWarnings": "Show Load Warnings",
"neoforge.configgui.showLoadWarnings.tooltip": "When enabled, NeoForge will show any warnings that occurred during loading.",
"neoforge.configgui.useCombinedDepthStencilAttachment": "Use combined DEPTH_STENCIL Attachment",
Expand Down

0 comments on commit 72d5ebf

Please sign in to comment.