forked from KyoriPowered/adventure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranslatableComponent.java
199 lines (185 loc) · 6.58 KB
/
TranslatableComponent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* 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.text;
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;
/**
* A component that can display translated text.
*
* <p>This component consists of:</p>
* <dl>
* <dt>key</dt>
* <dd>a translation key used together with the viewer locale to fetch a translated string.</dd>
* <dt>args(optional)</dt>
* <dd>components that can be used as arguments in the translated string.
* <p>(e.g "You picked up <b>{0}</b>." -> "You picked up <b>Carrot</b>")</p></dd>
* </dl>
*
* <p>Displaying this component through an {@link Audience} will run it through the {@link GlobalTranslator} by default,
* rendering the key as translated text if a translation with a key matching this components key is found in the viewers locale,
* optionally switching arguments with any placeholders in the discovered translation. If no translation is registered for the viewers locale
* adventure will first try to find similar locales that has a valid translation, and then find a translation in the default language({@link TranslationRegistry#defaultLocale(Locale) relevant method}).</p>
*
* <p>In addition to the initial attempts, if no translation is found in the serverside registry,
* the translation key and arguments will be passed through to the client which will perform translation using any
* keys defined in an active resource pack. (Hint: vanilla Minecraft is also considered a resource pack)</p>
*
* @see GlobalTranslator
* @see TranslationRegistry
* @since 4.0.0
*/
public interface TranslatableComponent extends BuildableComponent<TranslatableComponent, TranslatableComponent.Builder>, ScopedComponent<TranslatableComponent> {
/**
* Gets the translation key.
*
* @return the translation key
* @since 4.0.0
*/
@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.
*
* @param key the translation key
* @return a translatable component
* @since 4.0.0
*/
@Contract(pure = true)
@NonNull TranslatableComponent key(final @NonNull String key);
/**
* Gets the unmodifiable list of translation arguments.
*
* @return the unmodifiable list of translation arguments
* @since 4.0.0
*/
@NonNull List<Component> args();
/**
* Sets the translation arguments for this component.
*
* @param args the translation arguments
* @return a translatable component
* @since 4.0.0
*/
@Contract(pure = true)
@NonNull TranslatableComponent args(final @NonNull ComponentLike@NonNull... args);
/**
* Sets the translation arguments for this component.
*
* @param args the translation arguments
* @return a translatable component
* @since 4.0.0
*/
@Contract(pure = true)
@NonNull TranslatableComponent args(final @NonNull List<? extends ComponentLike> args);
/**
* A text component builder.
*
* @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.
*
* @param key the translation key
* @return this builder
* @since 4.0.0
*/
@Contract("_ -> this")
@NonNull Builder key(final @NonNull String key);
/**
* Sets the translation args.
*
* @param arg the translation arg
* @return this builder
* @since 4.0.0
*/
@Contract("_ -> this")
@NonNull Builder args(final @NonNull ComponentBuilder<?, ?> arg);
/**
* Sets the translation args.
*
* @param args the translation args
* @return this builder
* @since 4.0.0
*/
@Contract("_ -> this")
@SuppressWarnings("checkstyle:GenericWhitespace")
@NonNull Builder args(final @NonNull ComponentBuilder<?, ?>@NonNull... args);
/**
* Sets the translation args.
*
* @param arg the translation arg
* @return this builder
* @since 4.0.0
*/
@Contract("_ -> this")
@NonNull Builder args(final @NonNull Component arg);
/**
* Sets the translation args.
*
* @param args the translation args
* @return this builder
* @since 4.0.0
*/
@Contract("_ -> this")
@NonNull Builder args(final @NonNull ComponentLike@NonNull... args);
/**
* Sets the translation args.
*
* @param args the translation args
* @return this builder
* @since 4.0.0
*/
@Contract("_ -> this")
@NonNull Builder args(final @NonNull List<? extends ComponentLike> args);
}
}