From 10618e2ab03c1f0236b7b8970c414418d73ad1b3 Mon Sep 17 00:00:00 2001 From: Sekwah Date: Wed, 12 Apr 2023 12:20:48 +0100 Subject: [PATCH] feat: update lang command to add missing translation keys --- .../portal/LangUpdateSubCommand.java | 7 ------ .../subcommands/portal/ListSubCommand.java | 5 +++- .../core/services/PortalServices.java | 4 ++- .../advancedportals/core/util/Lang.java | 25 ++++++++----------- lang/src/main/resources/lang/en_GB.lang | 3 +-- 5 files changed, 18 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/LangUpdateSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/LangUpdateSubCommand.java index 7a5c1c89..c4c8e749 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/LangUpdateSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/LangUpdateSubCommand.java @@ -60,13 +60,6 @@ public void onCommand(CommandSenderContainer sender, String[] args) { Lang.loadLanguage(configRepository.getTranslation()); sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translateInsertVariables("translatedata.updated", newTranslations.size())); - - /*if(!configRepository.getTranslation().equals("en_GB")) { - // Copy this just to allow people to check the new translations. - this.portalsCore.getDataStorage().copyDefaultFile("lang/en_GB.lang", "lang/en_GB-new.lang", true); - } - this.portalsCore.getDataStorage().copyDefaultFile("lang/" + configRepository.getTranslation() + ".lang", "lang/" + configRepository.getTranslation() + "-new.lang", true); - sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("translatedata.replaced") + Lang.translate("translatedata.overwrite"));*/ } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ListSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ListSubCommand.java index 635480b3..4993c502 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ListSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ListSubCommand.java @@ -7,6 +7,8 @@ import com.sekwah.advancedportals.core.util.Lang; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; public class ListSubCommand implements SubCommand { @@ -15,7 +17,8 @@ public class ListSubCommand implements SubCommand { @Override public void onCommand(CommandSenderContainer sender, String[] args) { - sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.list")); + sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.list") + + " " + portalServices.getPortals().asList().stream().map(Map.Entry::getKey).sorted().collect(Collectors.joining(", "))); } @Override diff --git a/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java b/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java index 648b8d30..7e84e2c7 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java @@ -8,6 +8,8 @@ import javax.inject.Singleton; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.Map; @Singleton @@ -25,7 +27,7 @@ public boolean playerMove(PlayerContainer player, PlayerLocation fromLoc, Player } public ImmutableList> getPortals() { - return null; + return ImmutableList.copyOf(Collections.emptyList()); } public boolean removePortal(String name, PlayerContainer player) { diff --git a/core/src/main/java/com/sekwah/advancedportals/core/util/Lang.java b/core/src/main/java/com/sekwah/advancedportals/core/util/Lang.java index 2bd1e55e..4a471b30 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/util/Lang.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/util/Lang.java @@ -1,7 +1,6 @@ package com.sekwah.advancedportals.core.util; import com.google.inject.Inject; -import com.sekwah.advancedportals.core.AdvancedPortalsCore; import com.sekwah.advancedportals.core.data.DataStorage; import java.io.IOException; @@ -17,19 +16,14 @@ *

* The language translation file for the game. Will always load english first * so that if the translations are missing any then they are still readable and can then be translated. - * (Its better than a raw translate string) + * (It's better than a raw translate string) *

- * TODO add a loaddefault where it only loads from the plugins version of the data rather than paying attention to any - * possible changed versions in the lang folder. */ public class Lang { public static final Lang instance = new Lang(); private final HashMap languageMap = new HashMap<>(); - @Inject - private AdvancedPortalsCore portalsCore; - @Inject private DataStorage dataStorage; @@ -40,14 +34,15 @@ public class Lang { public static void loadLanguage(String fileName) { if(!DEFAULT_LANG.equals(fileName)) { - instance.injectTranslations(instance, DEFAULT_LANG); + instance.injectTranslations(DEFAULT_LANG); } - instance.injectTranslations(instance, fileName); + instance.injectTranslations(fileName); } public static String translate(String s) { if (instance.languageMap.containsKey(s)) { String translation = instance.languageMap.get(s); + // noinspection ALL (not sure what the specific warning is for escaped unicode) translation = translation.replaceAll("&([0-9a-frk-o])", "\u00A7$1"); return translation; } else { @@ -56,7 +51,7 @@ public static String translate(String s) { } public static String translateInsertVariables(String s, Object... args) { - String translation = instance.translate(s); + String translation = translate(s); for (int i = 1; i <= args.length; i++) { translation = translation.replaceAll("%" + i + "\\$s", args[i-1].toString()); } @@ -79,12 +74,12 @@ public Map getInternalLanguageMap(String fileName) { return Collections.emptyMap(); } - private void injectTranslations(Lang lang, String fileName) { + private void injectTranslations(String fileName) { try { - URL url = lang.getClass().getClassLoader().getResource("lang/" + fileName + ".lang"); + URL url = Lang.instance.getClass().getClassLoader().getResource("lang/" + fileName + ".lang"); if (url != null) { Map initialMap = Lang.parseLang(url.openStream()); - lang.languageMap.putAll(initialMap); + Lang.instance.languageMap.putAll(initialMap); } else { this.infoLogger.logWarning("Could not load " + fileName + ".lang from within Advanced Portals as it doesn't exist."); } @@ -93,8 +88,8 @@ private void injectTranslations(Lang lang, String fileName) { this.infoLogger.logWarning("Could not load " + fileName + ".lang from within Advanced Portals."); } - Map newLangMap = this.getLanguageMap("lang/" + fileName + ".lang"); - lang.languageMap.putAll(newLangMap); + Map newLangMap = this.getLanguageMap(fileName ); + Lang.instance.languageMap.putAll(newLangMap); } public static Map parseLang(InputStream inputStream) { diff --git a/lang/src/main/resources/lang/en_GB.lang b/lang/src/main/resources/lang/en_GB.lang index 14a26747..ef9bbbe2 100644 --- a/lang/src/main/resources/lang/en_GB.lang +++ b/lang/src/main/resources/lang/en_GB.lang @@ -35,8 +35,7 @@ translatedata.translationsoutdated= Some of the translations from the current translation file &e%1$s&c are out of date. translatedata.replacecommand= Use &e/portal langupdate&c to copy out a new default &een_GB&c file. translatedata.replaced= A new &een_GB&a file has been copied to the data folder. -translatedata.overwrite= Please use "&elangupdate overwrite&a" to replace the original file. -translatedata.updated= &e%1$s &atranslations have been added to the lang file. +translatedata.updated= &e%1$s &atranslations have been added to the lang file. If you want to reset the lang file, Please use "&elangupdate overwrite&a". messageprefix.positive=&a[&eAdvancedPortals&a] messageprefix.negative=&c[&7AdvancedPortals&c]