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

feat(api): hasAnyTranslations #945

Merged
merged 1 commit into from
Jul 12, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.translation.Translator;
import net.kyori.adventure.util.TriState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -79,9 +80,13 @@ public abstract class TranslatableComponentRenderer<C> extends AbstractComponent

@Override
protected @NotNull Component renderTranslatable(final @NotNull TranslatableComponent component, final @NotNull Locale context) {
final @Nullable Component translated = source.translate(component, context);
if (translated != null) return translated;
return super.renderTranslatable(component, context);
final TriState anyTranslations = source.hasAnyTranslations();
if (anyTranslations == TriState.TRUE || anyTranslations == TriState.NOT_SET) {
final @Nullable Component translated = source.translate(component, context);
if (translated != null) return translated;
return super.renderTranslatable(component, context);
}
return component;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.renderer.TranslatableComponentRenderer;
import net.kyori.adventure.util.TriState;
import net.kyori.examination.ExaminableProperty;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -71,6 +72,14 @@ public boolean removeSource(final @NotNull Translator source) {
return this.sources.remove(source);
}

@Override
public @NotNull TriState hasAnyTranslations() {
if (!this.sources.isEmpty()) {
return TriState.TRUE;
}
return TriState.FALSE;
}

@Override
public @Nullable MessageFormat translate(final @NotNull String key, final @NotNull Locale locale) {
requireNonNull(key, "key");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.stream.Stream;
import net.kyori.adventure.internal.Internals;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.util.TriState;
import net.kyori.examination.Examinable;
import net.kyori.examination.ExaminableProperty;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -67,6 +68,14 @@ public boolean contains(final @NotNull String key) {
return this.translations.containsKey(key);
}

@Override
public @NotNull TriState hasAnyTranslations() {
if (!this.translations.isEmpty()) {
return TriState.TRUE;
}
return TriState.FALSE;
}

@Override
public @Nullable MessageFormat translate(final @NotNull String key, final @NotNull Locale locale) {
final Translation translation = this.translations.get(key);
Expand Down
11 changes: 11 additions & 0 deletions api/src/main/java/net/kyori/adventure/translation/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.util.TriState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -78,6 +79,16 @@ public interface Translator {
*/
@NotNull Key name();

/**
* Checks if this translator has any translations.
*
* @return {@link TriState#TRUE} if any, {@link TriState#NOT_SET} if unknown, or {@link TriState#FALSE} if none
* @since 4.15.0
*/
default @NotNull TriState hasAnyTranslations() {
return TriState.NOT_SET;
}

/**
* Gets a message format from a key and locale.
*
Expand Down