Skip to content

Commit 8345aee

Browse files
feat(i18n): enhance I18n with dialogue support and placeholder helpers
Add dialogueLines() methods to I18n for splitting pipe-delimited translations into String arrays for NPC dialogue sequences. Add string() overloads with Map<String,String> placeholder support. Add DialogueSet.ofTranslation() factory methods to create dialogue sets directly from translation keys.
1 parent fac4824 commit 8345aee

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

type.generic/src/main/java/net/swofty/type/generic/entity/npc/HypixelNPC.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.swofty.type.generic.entity.npc.impl.NPCViewable;
1717
import net.swofty.type.generic.entity.npc.impl.NPCVillagerEntityImpl;
1818
import net.swofty.type.generic.event.custom.NPCInteractEvent;
19+
import net.swofty.type.generic.i18n.I18n;
1920
import net.swofty.type.generic.user.HypixelPlayer;
2021
import org.tinylog.Logger;
2122

@@ -268,5 +269,21 @@ public Entity get(HypixelNPC npc) {
268269
@Builder
269270
public record DialogueSet(String key, String[] lines, Sound sound) {
270271
public static final DialogueSet[] EMPTY = new DialogueSet[0];
272+
273+
public static DialogueSet ofTranslation(String key, String translationKey) {
274+
return new DialogueSet(key, I18n.dialogueLines(translationKey), null);
275+
}
276+
277+
public static DialogueSet ofTranslation(String key, String translationKey, Sound sound) {
278+
return new DialogueSet(key, I18n.dialogueLines(translationKey), sound);
279+
}
280+
281+
public static DialogueSet ofTranslation(String key, String translationKey, Map<String, String> placeholders) {
282+
return new DialogueSet(key, I18n.dialogueLines(translationKey, placeholders), null);
283+
}
284+
285+
public static DialogueSet ofTranslation(String key, String translationKey, Map<String, String> placeholders, Sound sound) {
286+
return new DialogueSet(key, I18n.dialogueLines(translationKey, placeholders), sound);
287+
}
271288
}
272289
}

type.generic/src/main/java/net/swofty/type/generic/i18n/I18n.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import net.kyori.adventure.translation.GlobalTranslator;
77

88
import java.util.Locale;
9+
import java.util.Map;
910

1011
public class I18n {
1112

1213
private static final LegacyComponentSerializer LEGACY =
1314
LegacyComponentSerializer.legacySection();
1415

16+
private static final String DIALOGUE_SEPARATOR = "\\|";
17+
1518
public static TranslatableComponent t(String key) {
1619
return Component.translatable(key);
1720
}
@@ -28,4 +31,34 @@ public static String string(String key, Locale locale) {
2831
public static String string(String key) {
2932
return string(key, HypixelTranslator.defaultLocale);
3033
}
34+
35+
public static String string(String key, Map<String, String> placeholders) {
36+
String result = string(key);
37+
for (Map.Entry<String, String> entry : placeholders.entrySet()) {
38+
result = result.replace("{" + entry.getKey() + "}", entry.getValue());
39+
}
40+
return result;
41+
}
42+
43+
public static String string(String key, Locale locale, Map<String, String> placeholders) {
44+
String result = string(key, locale);
45+
for (Map.Entry<String, String> entry : placeholders.entrySet()) {
46+
result = result.replace("{" + entry.getKey() + "}", entry.getValue());
47+
}
48+
return result;
49+
}
50+
51+
public static String[] dialogueLines(String key) {
52+
return dialogueLines(key, HypixelTranslator.defaultLocale);
53+
}
54+
55+
public static String[] dialogueLines(String key, Locale locale) {
56+
String resolved = string(key, locale);
57+
return resolved.split(DIALOGUE_SEPARATOR);
58+
}
59+
60+
public static String[] dialogueLines(String key, Map<String, String> placeholders) {
61+
String resolved = string(key, placeholders);
62+
return resolved.split(DIALOGUE_SEPARATOR);
63+
}
3164
}

0 commit comments

Comments
 (0)