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 org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.InputStream;
@ -61,27 +62,22 @@ public final class TranslationManager {
* @param languageString language string to get
* @param sender sender to translate for
* @param includePrefix if {@link LanguageString#PREFIX} should be prepended
* @param placeholders placeholders
* @since v1-release0
*/
public static @NotNull String get(@NotNull LanguageString languageString, @NotNull CommandSender sender, boolean includePrefix) {
String language = "en";
public static @NotNull String get(@NotNull LanguageString languageString, @NotNull CommandSender sender, boolean includePrefix, @NotNull Map.Entry<@NotNull String, @NotNull String> @Nullable ... placeholders) {
String temp = "en";
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
return get(languageString, language);
}
temp = get(languageString, temp);
/**
* Returns the translation for the specified language
* string for the specified {@link CommandSender}.
*
* @param languageString language string to get
* @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);
if (placeholders != null)
for (Map.Entry<@NotNull String, @NotNull String> placeholder : placeholders)
temp = temp.replace("%" + placeholder.getKey() + "%", placeholder.getValue());
return temp;
}
/**

View file

@ -31,6 +31,8 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import static java.util.Map.entry;
/**
* Handles the {@code /clearchat} command.
*
@ -101,6 +103,6 @@ public final class ClearChatCommand extends CommandBase {
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.jetbrains.annotations.NotNull;
import static java.util.Map.entry;
/**
* Handles the {@code /psse} command.
*
@ -74,12 +76,14 @@ public final class ExtensionCommand extends CommandBase {
case "psse", "pickshadow", "server" -> {
if (arguments.length == 0)
sender.sendRichMessage(
TranslationManager.get(LanguageString.EXTENSIONCMD_GREETER, sender, true)
.replace("%codename%", ExtensionInformation.getVersioningCodename())
.replace("%version%", ExtensionInformation.getVersioningString())
.replace("%commit%", ExtensionInformation.getGitCommitIdentifierShort())
.replace("%dirty%", String.valueOf(ExtensionInformation.isGitDirty()))
);
TranslationManager.get(LanguageString.EXTENSIONCMD_GREETER,
sender,
true,
entry("codename", ExtensionInformation.getVersioningCodename()),
entry("version", ExtensionInformation.getVersioningString()),
entry("commit", ExtensionInformation.getGitCommitIdentifierShort()),
entry("%dirty%", String.valueOf(ExtensionInformation.isGitDirty()))
));
else {
switch (arguments[0]) {
case "killjvm" -> {
@ -90,7 +94,7 @@ public final class ExtensionCommand extends CommandBase {
}
case "license" -> sender.sendRichMessage(TranslationManager.get(LanguageString.EXTENSIONCMD_LICENSE, 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"))
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.completion.StaticTabCompletion;
import de.jeremystartm.pickshadow.api.command.completion.StubTabCompletion;
import de.jeremystartm.pickshadow.api.command.TabCompletion;
import de.jeremystartm.pickshadow.api.translation.LanguageString;
import de.jeremystartm.pickshadow.api.translation.TranslationManager;
@ -32,6 +31,8 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import static java.util.Map.entry;
/**
* Handles the {@code /message} command.
*
@ -84,10 +85,7 @@ public final class MessageCommand extends CommandBase {
// Check if receiver is online
if (receiver == null || !receiver.isOnline()) {
player.sendRichMessage(
TranslationManager.get(LanguageString.ERROR_PLAYER_NOT_FOUND, sender, true)
.replace("%player%", arguments[0])
);
player.sendRichMessage(TranslationManager.get(LanguageString.ERROR_PLAYER_NOT_FOUND, sender, true, entry("player", arguments[0])));
return;
}
@ -105,10 +103,14 @@ public final class MessageCommand extends CommandBase {
message.append(arguments[i]);
}
output = TranslationManager.get(LanguageString.MESSAGING_FORMAT, sender, false)
.replace("%from%", player.getName())
.replace("%to%", receiver.getName())
.replace("%message%", message.toString());
output = TranslationManager.get(
LanguageString.MESSAGING_FORMAT,
sender,
false,
entry("from", player.getName()),
entry("to", receiver.getName()),
entry("message", message.toString())
);
// Send message
player.sendRichMessage(output);