From 6fc7d2c123bad1e59f77d33a5e5d5f322c4a4746 Mon Sep 17 00:00:00 2001 From: Fallen_Breath Date: Sun, 1 Aug 2021 22:24:05 +0800 Subject: [PATCH] simple rule translation --- .../java/carpet/commands/CarpetCommand.java | 12 +++---- src/main/java/carpet/settings/ParsedRule.java | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/main/java/carpet/commands/CarpetCommand.java b/src/main/java/carpet/commands/CarpetCommand.java index e18f42f1..53d3c666 100644 --- a/src/main/java/carpet/commands/CarpetCommand.java +++ b/src/main/java/carpet/commands/CarpetCommand.java @@ -85,15 +85,15 @@ private static int displayRuleMenu(CommandSource source, ParsedRule rule) } catch (CommandSyntaxException e) { - Messenger.m(source, "w "+rule.name +" is set to: ","wb "+rule.getAsString()); + Messenger.m(source, "w "+rule.translatedName() +" is set to: ","wb "+rule.getAsString()); return 1; } Messenger.m(player, ""); - Messenger.m(player, "wb "+rule.name,"!/carpet "+rule.name,"^g refresh"); - Messenger.m(player, "w "+rule.description); + Messenger.m(player, "wb "+rule.translatedName(),"!/carpet "+rule.name,"^g refresh"); + Messenger.m(player, "w "+rule.translatedDescription()); - rule.extraInfo.forEach(s -> Messenger.m(player, "g " + s)); + rule.translatedExtras().forEach(s -> Messenger.m(player, "g " + s)); List tags = new ArrayList<>(); tags.add(Messenger.c("w Tags: ")); @@ -145,9 +145,9 @@ private static int removeDefault(CommandSource source, ParsedRule rule) private static ITextComponent displayInteractiveSetting(ParsedRule e) { List args = new ArrayList<>(); - args.add("w - "+e.name+" "); + args.add("w - "+e.translatedName()+" "); args.add("!/carpet "+e.name); - args.add("^y "+e.description); + args.add("^y "+e.translatedDescription()); for (String option: e.options) { args.add(makeSetRuleButton(e, option, true)); diff --git a/src/main/java/carpet/settings/ParsedRule.java b/src/main/java/carpet/settings/ParsedRule.java index 5842fba4..c002fd62 100644 --- a/src/main/java/carpet/settings/ParsedRule.java +++ b/src/main/java/carpet/settings/ParsedRule.java @@ -2,6 +2,7 @@ import carpet.CarpetServer; import carpet.utils.Messenger; +import carpet.utils.Translations; import com.google.common.collect.ImmutableList; import net.minecraft.command.CommandSource; @@ -12,6 +13,8 @@ import java.util.List; import java.util.Locale; +import static carpet.utils.Translations.tr; + public final class ParsedRule implements Comparable { public final Field field; public final String name; @@ -216,4 +219,33 @@ public String toString() { return this.name + ": " + getAsString(); } + + private String translationKey() + { + return String.format("rule.%s.name", name); + } + + public String translatedName(){ + String key = translationKey(); + return Translations.hasTranslation(key) ? tr(key) + String.format(" (%s)", name): name; + } + + public String translatedDescription() + { + return tr(String.format("rule.%s.desc", (name)), description); + } + + public List translatedExtras() + { + if (!Translations.hasTranslations()) return extraInfo; + String keyBase = String.format("rule.%s.extra.", name); + List extras = new ArrayList<>(); + int i = 0; + while (Translations.hasTranslation(keyBase+i)) + { + extras.add(Translations.tr(keyBase+i)); + i++; + } + return (extras.isEmpty()) ? extraInfo : extras; + } }