|
1 | 1 | package dev.qixils.crowdcontrol.common; |
2 | 2 |
|
3 | 3 | import net.kyori.adventure.key.Key; |
4 | | -import net.kyori.adventure.text.Component; |
5 | | -import net.kyori.adventure.text.TextComponent; |
6 | | -import net.kyori.adventure.text.TranslatableComponent; |
7 | | -import net.kyori.adventure.text.minimessage.tag.Tag; |
8 | | -import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; |
9 | | -import net.kyori.adventure.text.renderer.TranslatableComponentRenderer; |
10 | | -import net.kyori.adventure.translation.GlobalTranslator; |
11 | | -import net.kyori.adventure.translation.TranslationRegistry; |
| 4 | +import net.kyori.adventure.text.minimessage.translation.MiniMessageTranslationStore; |
12 | 5 | import net.kyori.adventure.translation.Translator; |
13 | 6 | import org.jetbrains.annotations.NotNull; |
14 | 7 | import org.jetbrains.annotations.Nullable; |
|
21 | 14 | import java.nio.file.Files; |
22 | 15 | import java.nio.file.Path; |
23 | 16 | import java.nio.file.Paths; |
24 | | -import java.text.MessageFormat; |
25 | 17 | import java.util.*; |
26 | 18 | import java.util.jar.JarFile; |
27 | 19 | import java.util.regex.Matcher; |
28 | 20 | import java.util.regex.Pattern; |
29 | 21 | import java.util.stream.Collectors; |
30 | 22 | import java.util.stream.Stream; |
31 | 23 |
|
32 | | -import static net.kyori.adventure.text.minimessage.MiniMessage.miniMessage; |
33 | | - |
34 | 24 | // TODO: remove sponge cope |
35 | 25 |
|
36 | | -public final class KyoriTranslator extends TranslatableComponentRenderer<Locale> implements TranslationRegistry { |
| 26 | +public final class KyoriTranslator { |
37 | 27 | private static final Logger logger = LoggerFactory.getLogger("CrowdControl/KyoriTranslator"); |
38 | 28 | private final String prefix; |
39 | | - private final TranslationRegistry translator; |
| 29 | + private final KyoriStoreAccessor intermediate; |
| 30 | + private final MiniMessageTranslationStore translator; |
40 | 31 | private final ClassLoader pluginClassLoader; |
41 | 32 |
|
42 | 33 | private static @Nullable URL loadFirst(String path, ClassLoader... classLoaders) { |
@@ -120,8 +111,10 @@ public KyoriTranslator(@NotNull String modId, @NotNull String prefix, @NotNull P |
120 | 111 | logger.debug("Registering translator"); |
121 | 112 |
|
122 | 113 | // create translator |
123 | | - Key name = Key.key(modId, "translations"); |
124 | | - translator = TranslationRegistry.create(name); |
| 114 | + intermediate = new KyoriStoreAccessor(Key.key(modId, "intermediate_translations")); |
| 115 | + intermediate.defaultLocale(Objects.requireNonNull(Translator.parseLocale("en_US"))); |
| 116 | + |
| 117 | + translator = MiniMessageTranslationStore.create(Key.key(modId, "translations")); |
125 | 118 | translator.defaultLocale(Objects.requireNonNull(Translator.parseLocale("en_US"))); |
126 | 119 |
|
127 | 120 | // load locales |
@@ -154,79 +147,20 @@ public KyoriTranslator(@NotNull String modId, @NotNull String prefix, @NotNull P |
154 | 147 | } |
155 | 148 | } |
156 | 149 |
|
157 | | - @Override |
158 | | - public @NotNull Key name() { |
159 | | - return translator.name(); |
160 | | - } |
161 | | - |
162 | | - @Override |
163 | | - public boolean contains(@NotNull String key) { |
164 | | - return translator.contains(key); |
165 | | - } |
166 | | - |
167 | | - @Override |
168 | | - public boolean contains(@NotNull String key, @NotNull Locale locale) { |
169 | | - return translator.contains(key, locale); |
170 | | - } |
171 | | - |
172 | | - @Override |
173 | | - public void defaultLocale(@NotNull Locale locale) { |
174 | | - translator.defaultLocale(locale); |
175 | | - } |
176 | | - |
177 | | - @Override |
178 | | - public void register(@NotNull String key, @NotNull Locale locale, @NotNull MessageFormat format) { |
179 | | - translator.register(key, locale, format); |
180 | | - } |
181 | | - |
182 | | - @Override |
183 | | - public void unregister(@NotNull String key) { |
184 | | - translator.unregister(key); |
185 | | - } |
186 | | - |
187 | 150 | private void register(Locale locale) { |
188 | 151 | ResourceBundle bundle = ResourceBundle.getBundle("i18n." + prefix, locale, pluginClassLoader); |
189 | | - translator.registerAll(locale, bundle, false); |
190 | | - logger.info("Registered locale {}", locale); |
191 | | - } |
| 152 | + intermediate.registerAll(locale, bundle, false); |
| 153 | + for (String key : bundle.keySet()) { |
| 154 | + String value = intermediate.getTranslationString(key, locale); |
| 155 | + if (value == null) continue; // !? |
192 | 156 |
|
193 | | - @Override |
194 | | - public @Nullable Component translate(final @NotNull TranslatableComponent component, final @NotNull Locale context) { |
195 | | - if (translate(component.key(), context) == null) return null; |
196 | | - return renderTranslatable(component, context); |
197 | | - } |
| 157 | + translator.register(key, locale, value.replaceAll("<(\\d+)>", "<arg:$1>")); |
| 158 | + } |
198 | 159 |
|
199 | | - @Override |
200 | | - public @Nullable MessageFormat translate(@NotNull String key, @NotNull Locale locale) { |
201 | | - logger.debug("Plainly translating {} for {}", key, locale); |
202 | | - return translator.translate(key, locale); |
| 160 | + logger.info("Registered locale {}", locale); |
203 | 161 | } |
204 | 162 |
|
205 | | - @Override |
206 | | - protected @NotNull Component renderTranslatable(@NotNull TranslatableComponent component, @NotNull Locale context) { |
207 | | - logger.debug("Richly translating {} for {}", component.key(), context); |
208 | | - final @Nullable MessageFormat format = translate(component.key(), context); |
209 | | - |
210 | | - // this probably shouldn't cause a stack overflow because of the top-level check for null in the #translate method |
211 | | - // (and in fact, it hasn't from a lot of testing) |
212 | | - // also this needs to be here because #optionallyRenderChildrenAppendAndBuild calls this method to, well, render children |
213 | | - // although that's not to say that this couldn't be improved. it probably could be. |
214 | | - if (format == null) |
215 | | - return GlobalTranslator.renderer().render(component, context); |
216 | | - |
217 | | - final TextComponent.Builder builder = Component.text(); // mostly just a dummy for appending children |
218 | | - this.mergeStyle(component, builder, context); |
219 | | - |
220 | | - final List<Component> args = component.args(); |
221 | | - if (args.isEmpty()) { |
222 | | - // no arguments makes this render very simple |
223 | | - builder.append(miniMessage().deserialize(format.format(null, new StringBuffer(), null).toString())); |
224 | | - } else { |
225 | | - final TagResolver.Builder resolver = TagResolver.builder(); |
226 | | - for (int i = 0; i < args.size(); i++) |
227 | | - resolver.tag(String.valueOf(i), Tag.selfClosingInserting(this.render(args.get(i), context))); |
228 | | - builder.append(miniMessage().deserialize(format.format(null, new StringBuffer(), null).toString(), resolver.build())); |
229 | | - } |
230 | | - return this.optionallyRenderChildrenAppendAndBuild(component.children(), builder, context); |
| 163 | + public MiniMessageTranslationStore getTranslator() { |
| 164 | + return translator; |
231 | 165 | } |
232 | 166 | } |
0 commit comments