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

api: Add Translatable interface #361

Merged
merged 1 commit into from May 31, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
180 changes: 180 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Expand Up @@ -28,6 +28,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -42,6 +43,7 @@
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.ComponentSerializer;
import net.kyori.adventure.translation.Translatable;
import net.kyori.adventure.util.Buildable;
import net.kyori.adventure.util.IntFunction2;
import net.kyori.examination.Examinable;
Expand Down Expand Up @@ -1081,6 +1083,18 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return translatable(key, Style.empty());
}

/**
* Creates a translatable component with a translation key.
*
* @param translatable the translatable object to get the key from
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_ -> new", pure = true)
static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), Style.empty());
}

/**
* Creates a translatable component with a translation key and styling.
*
Expand All @@ -1094,6 +1108,19 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return new TranslatableComponentImpl(Collections.emptyList(), style, key, Collections.emptyList());
}

/**
* Creates a translatable component with a translation key and styling.
*
* @param translatable the translatable object to get the key from
* @param style the style
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_, _ -> new", pure = true)
static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @NonNull Style style) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), style);
}

/**
* Creates a translatable component with a translation key, and optional color.
*
Expand All @@ -1107,6 +1134,19 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return translatable(key, Style.style(color));
}

/**
* Creates a translatable component with a translation key, and optional color.
*
* @param translatable the translatable object to get the key from
* @param color the color
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_, _ -> new", pure = true)
static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color);
}

/**
* Creates a translatable component with a translation key, and optional color and decorations.
*
Expand All @@ -1121,6 +1161,20 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return translatable(key, Style.style(color, decorations));
}

/**
* Creates a translatable component with a translation key, and optional color and decorations.
*
* @param translatable the translatable object to get the key from
* @param color the color
* @param decorations the decorations
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_, _, _ -> new", pure = true)
static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color, final TextDecoration@NonNull... decorations) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color, decorations);
}

/**
* Creates a translatable component with a translation key, and optional color and decorations.
*
Expand All @@ -1135,6 +1189,20 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return translatable(key, Style.style(color, decorations));
}

/**
* Creates a translatable component with a translation key, and optional color and decorations.
*
* @param translatable the translatable object to get the key from
* @param color the color
* @param decorations the decorations
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_, _, _ -> new", pure = true)
static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color, final @NonNull Set<TextDecoration> decorations) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color, decorations);
}

/**
* Creates a translatable component with a translation key and arguments.
*
Expand All @@ -1148,6 +1216,19 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return translatable(key, Style.empty(), args);
}

/**
* Creates a translatable component with a translation key and arguments.
*
* @param translatable the translatable object to get the key from
* @param args the translation arguments
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_, _ -> new", pure = true)
static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @NonNull ComponentLike@NonNull... args) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), args);
}

/**
* Creates a translatable component with a translation key and styling.
*
Expand All @@ -1162,6 +1243,20 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return new TranslatableComponentImpl(Collections.emptyList(), style, key, args);
}

/**
* Creates a translatable component with a translation key and styling.
*
* @param translatable the translatable object to get the key from
* @param style the style
* @param args the translation arguments
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_, _, _ -> new", pure = true)
static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @NonNull Style style, final @NonNull ComponentLike@NonNull... args) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), style, args);
}

/**
* Creates a translatable component with a translation key, arguments, and optional color.
*
Expand All @@ -1176,6 +1271,20 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return translatable(key, Style.style(color), args);
}

/**
* Creates a translatable component with a translation key, arguments, and optional color.
*
* @param translatable the translatable object to get the key from
* @param color the color
* @param args the translation arguments
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_, _, _ -> new", pure = true)
static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color, final @NonNull ComponentLike@NonNull... args) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color, args);
}

/**
* Creates a translatable component with a translation key, arguments, and optional color and decorations.
*
Expand All @@ -1191,6 +1300,21 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return translatable(key, Style.style(color, decorations), args);
}

/**
* Creates a translatable component with a translation key, arguments, and optional color and decorations.
*
* @param translatable the translatable object to get the key from
* @param color the color
* @param decorations the decorations
* @param args the translation arguments
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_, _, _, _ -> new", pure = true)
static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color, final @NonNull Set<TextDecoration> decorations, final @NonNull ComponentLike@NonNull... args) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color, decorations, args);
}

/**
* Creates a translatable component with a translation key and arguments.
*
Expand All @@ -1204,6 +1328,19 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return new TranslatableComponentImpl(Collections.emptyList(), Style.empty(), key, args);
}

/**
* Creates a translatable component with a translation key and arguments.
*
* @param translatable the translatable object to get the key from
* @param args the translation arguments
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_, _ -> new", pure = true)
static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @NonNull List<? extends ComponentLike> args) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), args);
}

/**
* Creates a translatable component with a translation key and styling.
*
Expand All @@ -1218,6 +1355,20 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return new TranslatableComponentImpl(Collections.emptyList(), style, key, args);
}

/**
* Creates a translatable component with a translation key and styling.
*
* @param translatable the translatable object to get the key from
* @param style the style
* @param args the translation arguments
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_, _, _ -> new", pure = true)
static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @NonNull Style style, final @NonNull List<? extends ComponentLike> args) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), style, args);
}

/**
* Creates a translatable component with a translation key, arguments, and optional color.
*
Expand All @@ -1232,6 +1383,20 @@ static TranslatableComponent translatable(final @NonNull String key, final @Null
return translatable(key, Style.style(color), args);
}

/**
* Creates a translatable component with a translation key, arguments, and optional color.
*
* @param translatable the translatable object to get the key from
* @param color the color
* @param args the translation arguments
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_, _, _ -> new", pure = true)
static TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color, final @NonNull List<? extends ComponentLike> args) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color, args);
}

/**
* Creates a translatable component with a translation key, arguments, and optional color and decorations.
*
Expand All @@ -1247,6 +1412,21 @@ static TranslatableComponent translatable(final @NonNull String key, final @Null
return translatable(key, Style.style(color, decorations), args);
}

/**
* Creates a translatable component with a translation key, arguments, and optional color and decorations.
*
* @param translatable the translatable object to get the key from
* @param color the color
* @param decorations the decorations
* @param args the translation arguments
* @return a translatable component
* @since 4.8.0
*/
@Contract(value = "_, _, _, _ -> new", pure = true)
static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color, final @NonNull Set<TextDecoration> decorations, final @NonNull List<? extends ComponentLike> args) {
return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color, decorations, args);
}

/**
* Gets the unmodifiable list of children.
*
Expand Down
Expand Up @@ -25,8 +25,10 @@

import java.util.List;
import java.util.Locale;
import java.util.Objects;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.translation.GlobalTranslator;
import net.kyori.adventure.translation.Translatable;
import net.kyori.adventure.translation.TranslationRegistry;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.Contract;
Expand Down Expand Up @@ -65,6 +67,18 @@ public interface TranslatableComponent extends BuildableComponent<TranslatableCo
*/
@NonNull String key();

/**
* Sets the translation key.
*
* @param translatable the translatable object to get the key from
* @return a translatable component
* @since 4.8.0
*/
@Contract(pure = true)
default @NonNull TranslatableComponent key(final @NonNull Translatable translatable) {
return this.key(Objects.requireNonNull(translatable, "translatable").translationKey());
}

/**
* Sets the translation key.
*
Expand Down Expand Up @@ -109,6 +123,18 @@ public interface TranslatableComponent extends BuildableComponent<TranslatableCo
* @since 4.0.0
*/
interface Builder extends ComponentBuilder<TranslatableComponent, Builder> {
/**
* Sets the translation key.
*
* @param translatable the translatable object to get the key from
* @return this builder
* @since 4.8.0
*/
@Contract(pure = true)
default @NonNull Builder key(final @NonNull Translatable translatable) {
return this.key(Objects.requireNonNull(translatable, "translatable").translationKey());
}

/**
* Sets the translation key.
*
Expand Down
@@ -0,0 +1,41 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2021 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.translation;

import org.checkerframework.checker.nullness.qual.NonNull;

/**
* Something that has a translation key.
*
* @since 4.8.0
*/
public interface Translatable {
/**
* Gets the translation key.
*
* @return the translation key
* @since 4.8.0
*/
@NonNull String translationKey();
}