Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make LangKey accept supplier #69

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -54,6 +54,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 @@ -9,38 +9,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 @@ -56,4 +70,4 @@ public boolean equals(Object obj) {
public String toString() {
return this.get();
}
}
}