feat(i18n): rework i18n to get locale from player

This commit is contained in:
aroooo 2022-10-27 18:39:25 +02:00
parent fa32baf26a
commit af6e031995
4 changed files with 43 additions and 18 deletions

View file

@ -7,6 +7,7 @@ import net.artelnatif.nicko.config.NickoConfiguration;
import net.artelnatif.nicko.event.PlayerJoinListener;
import net.artelnatif.nicko.event.PlayerQuitListener;
import net.artelnatif.nicko.i18n.I18N;
import net.artelnatif.nicko.i18n.LocaleManager;
import net.artelnatif.nicko.impl.Internals;
import net.artelnatif.nicko.impl.InternalsProvider;
import net.artelnatif.nicko.mojang.MojangAPI;
@ -16,7 +17,6 @@ import net.artelnatif.nicko.utils.ServerUtils;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Locale;
import java.util.logging.Level;
public class NickoBukkit extends JavaPlugin {
@ -41,6 +41,13 @@ public class NickoBukkit extends JavaPlugin {
if (getServer().getPluginManager().isPluginEnabled(this)) {
mojangAPI = new MojangAPI();
getLogger().info("Loading configuration...");
saveDefaultConfig();
nickoConfiguration = new NickoConfiguration(this);
getLogger().info("Setting default locale...");
LocaleManager.setDefaultLocale(this);
final PluginCommand command = getCommand("nicko");
if (command != null) {
command.setExecutor(new NickoCommand());
@ -50,14 +57,6 @@ public class NickoBukkit extends JavaPlugin {
getServer().getPluginManager().registerEvents(new PlayerJoinListener(), this);
getServer().getPluginManager().registerEvents(new PlayerQuitListener(), this);
getLogger().info("Loading configuration...");
saveDefaultConfig();
nickoConfiguration = new NickoConfiguration(this);
getLogger().info("Loading locale...");
Locale.setDefault(Locale.ENGLISH);
i18N = new I18N(this);
getLogger().info("Loading persistence...");
dataStore = new PlayerDataStore(this);
System.out.println(dataStore.getStorage().isError());

View file

@ -15,11 +15,7 @@ public class NickoConfiguration {
return getConfig().getString("prefix");
}
public String getLocale() { return getConfig().getString("locale"); }
public String getDisguiseKitHeader() {
return getConfig().getString("disguisekit.header");
}
public String getDefaultLocale() { return getConfig().getString("locale"); }
public ConfigurationSection getBungeecordSection() { return getConfig().getConfigurationSection("bungeecord"); }

View file

@ -0,0 +1,28 @@
package net.artelnatif.nicko.i18n;
import net.artelnatif.nicko.NickoBukkit;
import org.apache.commons.lang.LocaleUtils;
import java.util.Arrays;
import java.util.Locale;
public class LocaleManager {
private static final String[] supportedLocales = new String[]{"en", "fr", "custom"};
public static void setDefaultLocale(NickoBukkit instance) {
final String locale = instance.getNickoConfig().getDefaultLocale();
try {
if (Arrays.stream(supportedLocales).noneMatch(s -> s.equalsIgnoreCase(locale))) {
instance.getLogger().severe(locale + " is not a supported locale, defaulting to English.");
Locale.setDefault(Locale.ENGLISH);
return;
}
final Locale defaultLocale = LocaleUtils.toLocale(locale);
instance.getLogger().info("Default locale set to " + defaultLocale.getDisplayName() + ".");
Locale.setDefault(defaultLocale);
} catch (Exception e) {
instance.getLogger().severe(locale + " is not a valid locale, defaulting to English.");
Locale.setDefault(Locale.ENGLISH);
}
}
}

View file

@ -4,6 +4,7 @@ prefix: "§8[§6Nicko§8] "
bungeecord:
# Enables Bungeecord support, switching through servers will keep player's skins.
# Accepted values: false (Disabled), true (Enabled)
enabled: false
redis:
username: ""
@ -11,10 +12,11 @@ bungeecord:
# Time To Live, the time the data will be stored on a player-by-player basis.
ttl: -1
disguisekit:
# The rainbow header text in the book.
header: "Nicko's Disguise Kit"
# Localisation:
# By default, Nicko tries to get the locale from the player's options.
# If that fails, the locale fallback is the one provided by this option.
# Accepted values: fr (French), en (English), custom (Custom language file).
locale: "en"
storage:
# Indicates wherever the data will be stored