feat: initial i18n support

This commit is contained in:
aroooo 2022-10-24 12:26:52 +02:00
parent 421e1f74e6
commit 1e23c64144
6 changed files with 51 additions and 4 deletions

View file

@ -6,6 +6,7 @@ import net.artelnatif.nicko.command.NickoTabCompleter;
import net.artelnatif.nicko.config.NickoConfiguration; import net.artelnatif.nicko.config.NickoConfiguration;
import net.artelnatif.nicko.event.PlayerJoinListener; import net.artelnatif.nicko.event.PlayerJoinListener;
import net.artelnatif.nicko.event.PlayerQuitListener; import net.artelnatif.nicko.event.PlayerQuitListener;
import net.artelnatif.nicko.i18n.I18N;
import net.artelnatif.nicko.impl.Internals; import net.artelnatif.nicko.impl.Internals;
import net.artelnatif.nicko.impl.InternalsProvider; import net.artelnatif.nicko.impl.InternalsProvider;
import net.artelnatif.nicko.mojang.MojangAPI; import net.artelnatif.nicko.mojang.MojangAPI;
@ -15,6 +16,7 @@ import net.artelnatif.nicko.utils.ServerUtils;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.Locale;
import java.util.logging.Level; import java.util.logging.Level;
public class NickoBukkit extends JavaPlugin { public class NickoBukkit extends JavaPlugin {
@ -23,6 +25,7 @@ public class NickoBukkit extends JavaPlugin {
private NickoConfiguration nickoConfiguration; private NickoConfiguration nickoConfiguration;
private MojangAPI mojangAPI; private MojangAPI mojangAPI;
private PlayerDataStore dataStore; private PlayerDataStore dataStore;
private I18N i18N;
@Override @Override
public void onEnable() { public void onEnable() {
@ -38,6 +41,10 @@ public class NickoBukkit extends JavaPlugin {
if (getServer().getPluginManager().isPluginEnabled(this)) { if (getServer().getPluginManager().isPluginEnabled(this)) {
mojangAPI = new MojangAPI(); mojangAPI = new MojangAPI();
getLogger().info("Loading locale...");
Locale.setDefault(Locale.ENGLISH);
i18N = new I18N(this);
final PluginCommand command = getCommand("nicko"); final PluginCommand command = getCommand("nicko");
if (command != null) { if (command != null) {
command.setExecutor(new NickoCommand()); command.setExecutor(new NickoCommand());

View file

@ -15,6 +15,8 @@ public class NickoConfiguration {
return getConfig().getString("prefix"); return getConfig().getString("prefix");
} }
public String getLocale() { return getConfig().getString("locale"); }
public String getDisguiseKitHeader() { public String getDisguiseKitHeader() {
return getConfig().getString("disguisekit.header"); return getConfig().getString("disguisekit.header");
} }

View file

@ -1,13 +1,36 @@
package net.artelnatif.nicko.i18n; package net.artelnatif.nicko.i18n;
import net.artelnatif.nicko.NickoBukkit; import net.artelnatif.nicko.NickoBukkit;
import org.apache.commons.lang.LocaleUtils;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
public class I18N { public class I18N {
public static final class Message { private final NickoBukkit instance;
public static final String BASE = NickoBukkit.getInstance().getNickoConfig().getPrefix() + " "; private final MessageFormat formatter = new MessageFormat("");
public static final class Command { public I18N(NickoBukkit instance) {
public static final String TARGET_OFFLINE = BASE + "§cSpecified player is offline. Try again."; this.instance = instance;
formatter.setLocale(getLocale());
}
private Locale getLocale() {
try {
return LocaleUtils.toLocale(instance.getNickoConfig().getLocale());
} catch (IllegalArgumentException exception) {
instance.getLogger().severe("Invalid locale provided, defaulting to " + Locale.getDefault().getDisplayName() + ".");
return Locale.getDefault();
} }
} }
private ResourceBundle getBundle() {
return ResourceBundle.getBundle("locale", getLocale());
}
public String get(String key, Object... arguments) {
formatter.applyPattern(getBundle().getString(key));
return formatter.format(arguments);
}
} }

View file

@ -0,0 +1,13 @@
package net.artelnatif.nicko.utils;
import java.io.File;
import java.io.IOException;
public class FileUtils {
public static boolean isPresentOrCreate(File file) throws IOException {
if (!file.exists()) {
return file.createNewFile();
}
return true;
}
}

View file

@ -0,0 +1 @@
player.offline="§c{0} §fis offline, please try again."

View file

@ -0,0 +1 @@
player.offline="§c{0} §fest hors-ligne, veuillez réessayer."