feat(i18n): rework i18n to get locale from player
This commit is contained in:
parent
fa32baf26a
commit
af6e031995
4 changed files with 43 additions and 18 deletions
|
@ -7,6 +7,7 @@ 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.i18n.I18N;
|
||||||
|
import net.artelnatif.nicko.i18n.LocaleManager;
|
||||||
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;
|
||||||
|
@ -16,7 +17,6 @@ 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 {
|
||||||
|
@ -41,6 +41,13 @@ public class NickoBukkit extends JavaPlugin {
|
||||||
if (getServer().getPluginManager().isPluginEnabled(this)) {
|
if (getServer().getPluginManager().isPluginEnabled(this)) {
|
||||||
mojangAPI = new MojangAPI();
|
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");
|
final PluginCommand command = getCommand("nicko");
|
||||||
if (command != null) {
|
if (command != null) {
|
||||||
command.setExecutor(new NickoCommand());
|
command.setExecutor(new NickoCommand());
|
||||||
|
@ -50,14 +57,6 @@ public class NickoBukkit extends JavaPlugin {
|
||||||
getServer().getPluginManager().registerEvents(new PlayerJoinListener(), this);
|
getServer().getPluginManager().registerEvents(new PlayerJoinListener(), this);
|
||||||
getServer().getPluginManager().registerEvents(new PlayerQuitListener(), 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...");
|
getLogger().info("Loading persistence...");
|
||||||
dataStore = new PlayerDataStore(this);
|
dataStore = new PlayerDataStore(this);
|
||||||
System.out.println(dataStore.getStorage().isError());
|
System.out.println(dataStore.getStorage().isError());
|
||||||
|
|
|
@ -15,11 +15,7 @@ public class NickoConfiguration {
|
||||||
return getConfig().getString("prefix");
|
return getConfig().getString("prefix");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLocale() { return getConfig().getString("locale"); }
|
public String getDefaultLocale() { return getConfig().getString("locale"); }
|
||||||
|
|
||||||
public String getDisguiseKitHeader() {
|
|
||||||
return getConfig().getString("disguisekit.header");
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConfigurationSection getBungeecordSection() { return getConfig().getConfigurationSection("bungeecord"); }
|
public ConfigurationSection getBungeecordSection() { return getConfig().getConfigurationSection("bungeecord"); }
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ prefix: "§8[§6Nicko§8] "
|
||||||
|
|
||||||
bungeecord:
|
bungeecord:
|
||||||
# Enables Bungeecord support, switching through servers will keep player's skins.
|
# Enables Bungeecord support, switching through servers will keep player's skins.
|
||||||
|
# Accepted values: false (Disabled), true (Enabled)
|
||||||
enabled: false
|
enabled: false
|
||||||
redis:
|
redis:
|
||||||
username: ""
|
username: ""
|
||||||
|
@ -11,10 +12,11 @@ bungeecord:
|
||||||
# Time To Live, the time the data will be stored on a player-by-player basis.
|
# Time To Live, the time the data will be stored on a player-by-player basis.
|
||||||
ttl: -1
|
ttl: -1
|
||||||
|
|
||||||
|
# Localisation:
|
||||||
disguisekit:
|
# By default, Nicko tries to get the locale from the player's options.
|
||||||
# The rainbow header text in the book.
|
# If that fails, the locale fallback is the one provided by this option.
|
||||||
header: "Nicko's Disguise Kit"
|
# Accepted values: fr (French), en (English), custom (Custom language file).
|
||||||
|
locale: "en"
|
||||||
|
|
||||||
storage:
|
storage:
|
||||||
# Indicates wherever the data will be stored
|
# Indicates wherever the data will be stored
|
||||||
|
|
Loading…
Reference in a new issue