Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add internationalization support for service messages #36

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
compileOnly("net.luckperms:api:5.4")

implementation("com.github.MilkBowl:VaultAPI:1.7.1")
implementation("net.thenextlvl.core:i18n:1.0.20")
implementation("net.thenextlvl.core:paper:1.5.3")
implementation("org.bstats:bstats-bukkit:3.1.0")

Expand Down
16 changes: 16 additions & 0 deletions plugin/src/main/java/net/thenextlvl/service/ServicePlugin.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package net.thenextlvl.service;

import com.google.common.base.Preconditions;
import core.i18n.file.ComponentBundle;
import lombok.Getter;
import lombok.experimental.Accessors;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.milkbowl.vault.chat.Chat;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
Expand All @@ -29,11 +33,14 @@
import net.thenextlvl.service.wrapper.service.PermissionServiceWrapper;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.entity.Player;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.io.File;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.function.Function;
Expand All @@ -52,6 +59,15 @@ public class ServicePlugin extends JavaPlugin {

private PermissionController permissionController = new SuperPermsPermissionController(this);

private final ComponentBundle bundle = new ComponentBundle(new File(getDataFolder(), "translations"),
audience -> audience instanceof Player player ? player.locale() : Locale.US)
.register("service-io", Locale.US)
.register("service-io_german", Locale.GERMANY)
.miniMessage(bundle -> MiniMessage.builder().tags(TagResolver.resolver(
Placeholder.component("prefix", bundle.component(Locale.US, "prefix")),
TagResolver.standard()
)).build());

@Override
public void onLoad() {
versionChecker().checkVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
import net.thenextlvl.service.api.economy.bank.BankController;
import net.thenextlvl.service.api.group.GroupController;
import net.thenextlvl.service.api.permission.PermissionController;
import net.thenextlvl.service.command.argument.*;
import net.thenextlvl.service.command.argument.BankArgumentType;
import net.thenextlvl.service.command.argument.ChatArgumentType;
import net.thenextlvl.service.command.argument.EconomyArgumentType;
import net.thenextlvl.service.command.argument.GroupArgumentType;
import net.thenextlvl.service.command.argument.PermissionArgumentType;
import org.bukkit.OfflinePlayer;
import org.jspecify.annotations.NullMarked;

Expand Down Expand Up @@ -174,20 +178,20 @@ private <T> int convert(CommandContext<CommandSourceStack> context, Class<T> con
var sender = context.getSource().getSender();

if (conversionRunning.get()) {
sender.sendRichMessage("A conversion is already running.");
plugin.bundle().sendMessage(sender, "service.convert.running");
return 0;
}

var source = context.getArgument("source", controller);
var target = context.getArgument("target", controller);

if (source.equals(target)) {
sender.sendRichMessage("Source and target service cannot be the same.");
plugin.bundle().sendMessage(sender, "service.convert.source-target");
return 0;
}

plugin.getServer().getAsyncScheduler().runNow(plugin, scheduledTask -> {
sender.sendRichMessage("Start converting data from <source> to <target>. This may take a while.",
plugin.bundle().sendMessage(sender, "service.convert.start",
Placeholder.parsed("source", name.apply(source)),
Placeholder.parsed("target", name.apply(target)));

Expand All @@ -198,15 +202,13 @@ private <T> int convert(CommandContext<CommandSourceStack> context, Class<T> con
conversionRunning.set(false);

var time = new DecimalFormat("0.000").format((System.currentTimeMillis() - now) / 1000d);
sender.sendRichMessage("Completed conversion in <time> seconds, please verify the data before using it.",
Placeholder.parsed("time", time));
plugin.bundle().sendMessage(sender, "service.convert.done", Placeholder.parsed("time", time));

}).exceptionally(throwable -> {
conversionRunning.set(false);

var time = new DecimalFormat("0.000").format((System.currentTimeMillis() - now) / 1000d);
sender.sendRichMessage("<red>Conversion failed after <time> seconds, see the console for more information.",
Placeholder.parsed("time", time));
plugin.bundle().sendMessage(sender, "service.convert.failed", Placeholder.parsed("time", time));
plugin.getComponentLogger().error("Data conversion failed after {} seconds", time, throwable);
return null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ LiteralArgumentBuilder<CommandSourceStack> create() {
}

private int info(CommandContext<CommandSourceStack> context) {
context.getSource().getSender().sendRichMessage("ServiceIO Information (v<version>)",
plugin.bundle().sendMessage(context.getSource().getSender(), "service.version",
Placeholder.parsed("version", plugin.getPluginMeta().getVersion()));

infoBanks(context);
Expand All @@ -60,7 +60,7 @@ private int infoBanks(CommandContext<CommandSourceStack> context) {
var banks = getRegistrations(BankController.class, bank, BankController::getName);
if (sendServiceInfo(sender, "Bank", bank != null ? bank.getName() : null, banks))
return Command.SINGLE_SUCCESS;
sender.sendRichMessage("<red>No bank service found.");
plugin.bundle().sendMessage(sender, "service.bank.none");
return 0;
}

Expand All @@ -70,7 +70,7 @@ private int infoChat(CommandContext<CommandSourceStack> context) {
var chats = getRegistrations(ChatController.class, chat, ChatController::getName);
if (sendServiceInfo(sender, "Chat", chat != null ? chat.getName() : null, chats))
return Command.SINGLE_SUCCESS;
sender.sendRichMessage("<red>No chat service found.");
plugin.bundle().sendMessage(sender, "service.chat.none");
return 0;
}

Expand All @@ -80,7 +80,7 @@ private int infoEconomy(CommandContext<CommandSourceStack> context) {
var economies = getRegistrations(EconomyController.class, economy, EconomyController::getName);
if (sendServiceInfo(sender, "Economy", economy != null ? economy.getName() : null, economies))
return Command.SINGLE_SUCCESS;
sender.sendRichMessage("<red>No economy service found.");
plugin.bundle().sendMessage(sender, "service.economy.none");
return 0;
}

Expand All @@ -90,7 +90,7 @@ private int infoGroups(CommandContext<CommandSourceStack> context) {
var groups = getRegistrations(GroupController.class, group, GroupController::getName);
if (sendServiceInfo(sender, "Group", group != null ? group.getName() : null, groups))
return Command.SINGLE_SUCCESS;
sender.sendRichMessage("<red>No group service found.");
plugin.bundle().sendMessage(sender, "service.group.none");
return 0;
}

Expand All @@ -100,7 +100,7 @@ private int infoPermissions(CommandContext<CommandSourceStack> context) {
var permissions = getRegistrations(PermissionController.class, permission, PermissionController::getName);
if (sendServiceInfo(sender, "Permission", permission != null ? permission.getName() : null, permissions))
return Command.SINGLE_SUCCESS;
sender.sendRichMessage("<red>No permission service found.");
plugin.bundle().sendMessage(sender, "service.permission.none");
return 0;
}

Expand All @@ -119,9 +119,9 @@ private <T> List<TextComponent> getRegistrations(Class<T> registration, @Nullabl
}

private boolean sendServiceInfo(CommandSender sender, String type, @Nullable String provider, List<TextComponent> registrations) {
if (provider != null) sender.sendRichMessage("<#0288D1><type>: <provider>",
if (provider != null) plugin.bundle().sendMessage(sender, "service.provider.name",
Placeholder.parsed("provider", provider), Placeholder.parsed("type", type));
if (!registrations.isEmpty()) sender.sendRichMessage("<green><registered>",
if (!registrations.isEmpty()) plugin.bundle().sendMessage(sender, "service.provider.registrations",
Placeholder.component("registered", Component.join(separator, registrations)));
return provider != null || !registrations.isEmpty();
}
Expand Down
14 changes: 14 additions & 0 deletions plugin/src/main/resources/service-io.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
prefix=<blue>ServiceIO</blue> <dark_gray>»</dark_gray>
service.bank.none=<red>No bank service found</red>
service.chat.none=<red>No chat service found</red>
service.convert.done=<gray><prefix> Completed conversion in <green><time></green> seconds, please verify the data before using it</gray>
service.convert.failed=<red><prefix> Conversion failed after <dark_red><time></dark_red> seconds, see the console for more information</red>
service.convert.running=<red><prefix> A conversion is already running</red>
service.convert.source-target=<red><prefix> Source and target service cannot be the same</red>
service.convert.start=<gray><prefix> Start converting data from <green><source></green> to <green><target></green>. This may take a while</gray>
service.economy.none=<red>No economy service found</red>
service.group.none=<red>No group service found</red>
service.permission.none=<red>No permission service found</red>
service.provider.name=<#0288D1><type>: <provider></#0288D1>
service.provider.registrations=<green><registered></green>
service.version=<gray>ServiceIO Information <dark_gray>(<green>v<version></green>)</dark_gray></gray>
11 changes: 11 additions & 0 deletions plugin/src/main/resources/service-io_german.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
service.bank.none=<red>Kein Bankdienst gefunden</red>
service.chat.none=<red>Kein Chatdienst gefunden</red>
service.convert.done=<gray><prefix> Konvertierung wurde in <green><time></green> Sekunden beendet, bitte überprüfe die Daten, bevor du sie verwendest</gray>
service.convert.failed=<red><prefix> Konvertierung ist nach <dark_red><time></dark_red> Sekunden fehlgeschlagen, schaue in die Konsole für mehr Informationen</red>
service.convert.running=<red><prefix> Eine Konvertierung läuft bereits</red>
service.convert.source-target=<red><prefix> Quell- und Zieldienst können nicht identisch sein</red>
service.convert.start=<gray><prefix> Datenkonvertierung von <green><source></green> zu <green><target></green> wurde gestartet. Dies könnte einige Zeit in Anspruch nehmen</gray>
service.economy.none=<red>Kein Economy-Dienst gefunden</red>
service.group.none=<red>Kein Gruppendienst gefunden</red>
service.permission.none=<red>Kein Berechtigungsdienst gefunden</red>
service.version=<gray>Informationen zu ServiceIO <dark_gray>(<green>v<version></green>)</dark_gray></gray>
Loading