diff --git a/src/main/java/com/cleanroommc/modularui/api/drawable/IKey.java b/src/main/java/com/cleanroommc/modularui/api/drawable/IKey.java index 7eeaec42..7b30e841 100644 --- a/src/main/java/com/cleanroommc/modularui/api/drawable/IKey.java +++ b/src/main/java/com/cleanroommc/modularui/api/drawable/IKey.java @@ -52,6 +52,38 @@ static IKey lang(@NotNull String key, @Nullable Object... args) { return new LangKey(key, args); } + /** + * Creates a translated text with arguments supplier. + * + * @param key translation key + * @param argsSupplier translation arguments supplier + * @return text key + */ + static IKey lang(@NotNull String key, @NotNull Supplier argsSupplier) { + return new LangKey(key, argsSupplier); + } + + /** + * Creates a translated text. + * + * @param keySupplier translation key supplier + * @return text key + */ + static IKey lang(@NotNull Supplier keySupplier) { + return new LangKey(keySupplier); + } + + /** + * Creates a translated text with arguments supplier. + * + * @param keySupplier translation key supplier + * @param argsSupplier translation arguments supplier + * @return text key + */ + static IKey lang(@NotNull Supplier keySupplier, @NotNull Supplier argsSupplier) { + return new LangKey(keySupplier, argsSupplier); + } + /** * Creates a string literal text. * diff --git a/src/main/java/com/cleanroommc/modularui/drawable/keys/LangKey.java b/src/main/java/com/cleanroommc/modularui/drawable/keys/LangKey.java index 93314b21..d8cb0a35 100644 --- a/src/main/java/com/cleanroommc/modularui/drawable/keys/LangKey.java +++ b/src/main/java/com/cleanroommc/modularui/drawable/keys/LangKey.java @@ -7,38 +7,52 @@ import org.jetbrains.annotations.Nullable; import java.util.Objects; +import java.util.function.Supplier; public class LangKey implements IKey { - private final String key; - private final Object[] args; + private final Supplier keySupplier; + private final Supplier argsSupplier; private String string; private long time = 0; public LangKey(@NotNull String key) { - this(key, null); + this(key, () -> null); } public LangKey(@NotNull String key, @Nullable Object[] args) { - this.key = Objects.requireNonNull(key); - this.args = args == null || args.length == 0 ? null : args; + this(() -> Objects.requireNonNull(key), () -> args == null || args.length == 0 ? null : args); } - public String getKey() { - return this.key; + public LangKey(@NotNull String key, @NotNull Supplier argsSupplier) { + this(() -> Objects.requireNonNull(key), argsSupplier); } - public Object[] getArgs() { - return this.args; + public LangKey(@NotNull Supplier keySupplier) { + this(keySupplier, () -> null); + } + + public LangKey(@NotNull Supplier keySupplier, @NotNull Supplier argsSupplier) { + this.keySupplier = Objects.requireNonNull(keySupplier); + this.argsSupplier = Objects.requireNonNull(argsSupplier); + } + + public Supplier getKeySupplier() { + return keySupplier; + } + + public Supplier getArgsSupplier() { + return argsSupplier; } @Override public String get() { - if (this.string == null || (this.args != null && this.time != ClientEventHandler.getTicks())) { - this.time = ClientEventHandler.getTicks(); - this.string = I18n.format(this.key, this.args); + if (this.time == ClientEventHandler.getTicks()) { + return this.string; } - return this.string; + this.time = ClientEventHandler.getTicks(); + this.string = I18n.format(Objects.requireNonNull(this.keySupplier.get()), this.argsSupplier.get()); + return string; } @Override @@ -54,4 +68,4 @@ public boolean equals(Object obj) { public String toString() { return this.get(); } -} \ No newline at end of file +}