diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/locale/TranslationManagerImpl.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/locale/TranslationManagerImpl.java index 1c62a106d..bcab027e8 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/locale/TranslationManagerImpl.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/locale/TranslationManagerImpl.java @@ -12,6 +12,8 @@ import net.momirealms.craftengine.core.plugin.text.minimessage.ImageTag; import net.momirealms.craftengine.core.plugin.text.minimessage.IndexedArgumentTag; import net.momirealms.craftengine.core.plugin.text.minimessage.ShiftTag; import net.momirealms.craftengine.core.util.AdventureHelper; +import net.momirealms.craftengine.core.util.MiscUtils; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.yaml.snakeyaml.DumperOptions; @@ -27,6 +29,7 @@ import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.BiConsumer; import java.util.function.Function; import java.util.stream.Collectors; @@ -275,6 +278,24 @@ public class TranslationManagerImpl implements TranslationManager { } } + private static void loadLangKeyDeeply(String prefix, Map data, BiConsumer consumer) { + for (Map.Entry entry : data.entrySet()) { + if (entry.getValue() instanceof Map map) { + Map innerMap = MiscUtils.castToMap(map, false); + loadLangKeyDeeply(assembleLangKey(prefix, entry.getKey()), innerMap, consumer); + } else { + consumer.accept(assembleLangKey(prefix, entry.getKey()), String.valueOf(entry.getValue())); + } + } + } + + private static String assembleLangKey(String prefix, String lang) { + if (prefix.isEmpty()) { + return lang; + } + return prefix + "." + lang; + } + public class TranslationParser extends IdSectionConfigParser { public static final String[] CONFIG_SECTION_NAME = new String[] {"translations", "translation", "l10n", "localization", "i18n", "internationalization"}; private int count; @@ -305,25 +326,12 @@ public class TranslationManagerImpl implements TranslationManager { if (locale == null) { throw new LocalizedResourceConfigException("warning.config.translation.unknown_locale"); } - Map bundle = new HashMap<>(); - for (Map.Entry entry : section.entrySet()) { - StringBuilder key = new StringBuilder(entry.getKey()); - Object value = entry.getValue(); - for (;;) { - if (!(value instanceof Map map)) { - value = entry.getValue(); - break; - } - Map.Entry next = map.entrySet().iterator().next(); - key.append(".").append(next.getKey()); - value = next.getValue(); - } - bundle.put(key.toString(), String.valueOf(value)); - TranslationManagerImpl.this.translationKeys.add(key.toString()); - this.count++; - } - + loadLangKeyDeeply("", section, (key, value) -> { + bundle.put(key, value); + TranslationManagerImpl.this.translationKeys.add(key); + }); + this.count += bundle.size(); TranslationManagerImpl.this.registry.registerAll(locale, bundle); TranslationManagerImpl.this.installed.add(locale); } @@ -360,13 +368,12 @@ public class TranslationManagerImpl implements TranslationManager { @Override public void parseSection(Pack pack, Path path, String node, net.momirealms.craftengine.core.util.Key id, Map section) { String langId = id.value().toLowerCase(Locale.ENGLISH); - Map sectionData = section.entrySet().stream() - .collect(Collectors.toMap( - Map.Entry::getKey, - entry -> this.langProcessor.apply(String.valueOf(entry.getValue())) - )); - TranslationManagerImpl.this.addClientTranslation(langId, sectionData); + Map sectionData = new HashMap<>(); + loadLangKeyDeeply("", section, (key, value) -> { + sectionData.put(key, this.langProcessor.apply(value)); + }); this.count += sectionData.size(); + TranslationManagerImpl.this.addClientTranslation(langId, sectionData); } }