Add placeholders to TranslationManager#get

This commit is contained in:
JeremyStar™ 2024-10-08 14:13:55 +02:00
parent 8fd3d8e5ef
commit f147b2000e
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
4 changed files with 36 additions and 32 deletions

View file

@ -25,6 +25,7 @@ import com.google.gson.reflect.TypeToken;
import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.logging.LoggerInstance;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -61,27 +62,22 @@ public final class TranslationManager {
* @param languageString language string to get * @param languageString language string to get
* @param sender sender to translate for * @param sender sender to translate for
* @param includePrefix if {@link LanguageString#PREFIX} should be prepended * @param includePrefix if {@link LanguageString#PREFIX} should be prepended
* @param placeholders placeholders
* @since v1-release0 * @since v1-release0
*/ */
public static @NotNull String get(@NotNull LanguageString languageString, @NotNull CommandSender sender, boolean includePrefix) { public static @NotNull String get(@NotNull LanguageString languageString, @NotNull CommandSender sender, boolean includePrefix, @NotNull Map.Entry<@NotNull String, @NotNull String> @Nullable ... placeholders) {
String language = "en"; String temp = "en";
if (includePrefix) if (includePrefix)
return get(LanguageString.PREFIX, "en") + get(languageString, language).replace("\n", "\n" + get(LanguageString.PREFIX_NEWLINE, "en")); temp = get(LanguageString.PREFIX, "en") + get(languageString, temp).replace("\n", "\n" + get(LanguageString.PREFIX_NEWLINE, "en"));
else else
return get(languageString, language); temp = get(languageString, temp);
}
/** if (placeholders != null)
* Returns the translation for the specified language for (Map.Entry<@NotNull String, @NotNull String> placeholder : placeholders)
* string for the specified {@link CommandSender}. temp = temp.replace("%" + placeholder.getKey() + "%", placeholder.getValue());
*
* @param languageString language string to get return temp;
* @param sender sender to translate for
* @since v1-release0
*/
public static @NotNull String get(@NotNull LanguageString languageString, @NotNull CommandSender sender) {
return get(languageString, sender, false);
} }
/** /**

View file

@ -31,6 +31,8 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import static java.util.Map.entry;
/** /**
* Handles the {@code /clearchat} command. * Handles the {@code /clearchat} command.
* *
@ -101,6 +103,6 @@ public final class ClearChatCommand extends CommandBase {
LanguageString.CLEARCHAT_CRINGE, LanguageString.CLEARCHAT_CRINGE,
}; };
return TranslationManager.get(strings[(int) System.currentTimeMillis() % strings.length], receiver, true).replace("%sender%", sender.getName()); return TranslationManager.get(strings[(int) System.currentTimeMillis() % strings.length], receiver, true, entry("sender", sender.getName()));
} }
} }

View file

@ -30,6 +30,8 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import static java.util.Map.entry;
/** /**
* Handles the {@code /psse} command. * Handles the {@code /psse} command.
* *
@ -74,12 +76,14 @@ public final class ExtensionCommand extends CommandBase {
case "psse", "pickshadow", "server" -> { case "psse", "pickshadow", "server" -> {
if (arguments.length == 0) if (arguments.length == 0)
sender.sendRichMessage( sender.sendRichMessage(
TranslationManager.get(LanguageString.EXTENSIONCMD_GREETER, sender, true) TranslationManager.get(LanguageString.EXTENSIONCMD_GREETER,
.replace("%codename%", ExtensionInformation.getVersioningCodename()) sender,
.replace("%version%", ExtensionInformation.getVersioningString()) true,
.replace("%commit%", ExtensionInformation.getGitCommitIdentifierShort()) entry("codename", ExtensionInformation.getVersioningCodename()),
.replace("%dirty%", String.valueOf(ExtensionInformation.isGitDirty())) entry("version", ExtensionInformation.getVersioningString()),
); entry("commit", ExtensionInformation.getGitCommitIdentifierShort()),
entry("%dirty%", String.valueOf(ExtensionInformation.isGitDirty()))
));
else { else {
switch (arguments[0]) { switch (arguments[0]) {
case "killjvm" -> { case "killjvm" -> {
@ -90,7 +94,7 @@ public final class ExtensionCommand extends CommandBase {
} }
case "license" -> sender.sendRichMessage(TranslationManager.get(LanguageString.EXTENSIONCMD_LICENSE, sender, true)); case "license" -> sender.sendRichMessage(TranslationManager.get(LanguageString.EXTENSIONCMD_LICENSE, sender, true));
case "source" -> sender.sendRichMessage(TranslationManager.get(LanguageString.EXTENSIONCMD_SOURCE, sender, true)); case "source" -> sender.sendRichMessage(TranslationManager.get(LanguageString.EXTENSIONCMD_SOURCE, sender, true));
default -> sender.sendRichMessage(TranslationManager.get(LanguageString.ERROR_INVALID_ARGUMENT, sender, true).replace("%argument%", arguments[0])); default -> sender.sendRichMessage(TranslationManager.get(LanguageString.ERROR_INVALID_ARGUMENT, sender, true, entry("argument", arguments[0])));
} }
if (arguments[0].equals("killjvm")) if (arguments[0].equals("killjvm"))
Runtime.getRuntime().halt(0); Runtime.getRuntime().halt(0);

View file

@ -21,7 +21,6 @@ package de.jeremystartm.pickshadow.command.general;
import de.jeremystartm.pickshadow.api.command.CommandBase; import de.jeremystartm.pickshadow.api.command.CommandBase;
import de.jeremystartm.pickshadow.api.command.completion.StaticTabCompletion; import de.jeremystartm.pickshadow.api.command.completion.StaticTabCompletion;
import de.jeremystartm.pickshadow.api.command.completion.StubTabCompletion;
import de.jeremystartm.pickshadow.api.command.TabCompletion; import de.jeremystartm.pickshadow.api.command.TabCompletion;
import de.jeremystartm.pickshadow.api.translation.LanguageString; import de.jeremystartm.pickshadow.api.translation.LanguageString;
import de.jeremystartm.pickshadow.api.translation.TranslationManager; import de.jeremystartm.pickshadow.api.translation.TranslationManager;
@ -32,6 +31,8 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import static java.util.Map.entry;
/** /**
* Handles the {@code /message} command. * Handles the {@code /message} command.
* *
@ -84,10 +85,7 @@ public final class MessageCommand extends CommandBase {
// Check if receiver is online // Check if receiver is online
if (receiver == null || !receiver.isOnline()) { if (receiver == null || !receiver.isOnline()) {
player.sendRichMessage( player.sendRichMessage(TranslationManager.get(LanguageString.ERROR_PLAYER_NOT_FOUND, sender, true, entry("player", arguments[0])));
TranslationManager.get(LanguageString.ERROR_PLAYER_NOT_FOUND, sender, true)
.replace("%player%", arguments[0])
);
return; return;
} }
@ -105,10 +103,14 @@ public final class MessageCommand extends CommandBase {
message.append(arguments[i]); message.append(arguments[i]);
} }
output = TranslationManager.get(LanguageString.MESSAGING_FORMAT, sender, false) output = TranslationManager.get(
.replace("%from%", player.getName()) LanguageString.MESSAGING_FORMAT,
.replace("%to%", receiver.getName()) sender,
.replace("%message%", message.toString()); false,
entry("from", player.getName()),
entry("to", receiver.getName()),
entry("message", message.toString())
);
// Send message // Send message
player.sendRichMessage(output); player.sendRichMessage(output);