Skip to content

Commit

Permalink
Make LangKey accept supplier (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
miozune authored Aug 21, 2024
1 parent 564d34c commit 5109e3b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/cleanroommc/modularui/api/drawable/IKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object[]> argsSupplier) {
return new LangKey(key, argsSupplier);
}

/**
* Creates a translated text.
*
* @param keySupplier translation key supplier
* @return text key
*/
static IKey lang(@NotNull Supplier<String> 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<String> keySupplier, @NotNull Supplier<Object[]> argsSupplier) {
return new LangKey(keySupplier, argsSupplier);
}

/**
* Creates a string literal text.
*
Expand Down
42 changes: 28 additions & 14 deletions src/main/java/com/cleanroommc/modularui/drawable/keys/LangKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> keySupplier;
private final Supplier<Object[]> 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<Object[]> argsSupplier) {
this(() -> Objects.requireNonNull(key), argsSupplier);
}

public Object[] getArgs() {
return this.args;
public LangKey(@NotNull Supplier<String> keySupplier) {
this(keySupplier, () -> null);
}

public LangKey(@NotNull Supplier<String> keySupplier, @NotNull Supplier<Object[]> argsSupplier) {
this.keySupplier = Objects.requireNonNull(keySupplier);
this.argsSupplier = Objects.requireNonNull(argsSupplier);
}

public Supplier<String> getKeySupplier() {
return keySupplier;
}

public Supplier<Object[]> 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
Expand All @@ -54,4 +68,4 @@ public boolean equals(Object obj) {
public String toString() {
return this.get();
}
}
}

0 comments on commit 5109e3b

Please sign in to comment.