Skip to content

Commit

Permalink
feat: update lang command to add missing translation keys
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Nov 18, 2024
1 parent 33c9221 commit 10618e2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"));*/
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +27,7 @@ public boolean playerMove(PlayerContainer player, PlayerLocation fromLoc, Player
}

public ImmutableList<? extends Map.Entry<String, AdvancedPortal>> getPortals() {
return null;
return ImmutableList.copyOf(Collections.emptyList());
}

public boolean removePortal(String name, PlayerContainer player) {
Expand Down
25 changes: 10 additions & 15 deletions core/src/main/java/com/sekwah/advancedportals/core/util/Lang.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,19 +16,14 @@
* <p>
* 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)
* <p>
* 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<String, String> languageMap = new HashMap<>();

@Inject
private AdvancedPortalsCore portalsCore;

@Inject
private DataStorage dataStorage;

Expand All @@ -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 {
Expand All @@ -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());
}
Expand All @@ -79,12 +74,12 @@ public Map<String, String> 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<String, String> 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.");
}
Expand All @@ -93,8 +88,8 @@ private void injectTranslations(Lang lang, String fileName) {
this.infoLogger.logWarning("Could not load " + fileName + ".lang from within Advanced Portals.");
}

Map<String, String> newLangMap = this.getLanguageMap("lang/" + fileName + ".lang");
lang.languageMap.putAll(newLangMap);
Map<String, String> newLangMap = this.getLanguageMap(fileName );
Lang.instance.languageMap.putAll(newLangMap);
}

public static Map<String, String> parseLang(InputStream inputStream) {
Expand Down
3 changes: 1 addition & 2 deletions lang/src/main/resources/lang/en_GB.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 10618e2

Please sign in to comment.