feat: continuing work on custom language support
This commit is contained in:
parent
9ed076e00d
commit
3f297eba49
5 changed files with 53 additions and 22 deletions
|
@ -34,6 +34,7 @@ public class NickoBukkit extends JavaPlugin {
|
||||||
private NickoConfiguration config;
|
private NickoConfiguration config;
|
||||||
private MojangAPI mojangAPI;
|
private MojangAPI mojangAPI;
|
||||||
private PlayerDataStore dataStore;
|
private PlayerDataStore dataStore;
|
||||||
|
private LocaleManager localeManager;
|
||||||
|
|
||||||
public NickoBukkit() { this.unitTesting = false; }
|
public NickoBukkit() { this.unitTesting = false; }
|
||||||
|
|
||||||
|
@ -102,7 +103,9 @@ public class NickoBukkit extends JavaPlugin {
|
||||||
saveDefaultConfig();
|
saveDefaultConfig();
|
||||||
config = new NickoConfiguration(this);
|
config = new NickoConfiguration(this);
|
||||||
|
|
||||||
LocaleManager.setFallbackLocale(this);
|
localeManager = new LocaleManager(this);
|
||||||
|
localeManager.findFallbackLocale();
|
||||||
|
|
||||||
|
|
||||||
final PluginCommand command = getCommand("nicko");
|
final PluginCommand command = getCommand("nicko");
|
||||||
if (command != null) {
|
if (command != null) {
|
||||||
|
|
|
@ -7,10 +7,11 @@ import org.bukkit.configuration.file.FileConfiguration;
|
||||||
public class NickoConfiguration {
|
public class NickoConfiguration {
|
||||||
private final NickoBukkit nicko;
|
private final NickoBukkit nicko;
|
||||||
private String prefix;
|
private String prefix;
|
||||||
private String defaultLocale;
|
private String fallbackLocale;
|
||||||
|
|
||||||
private Boolean bungeecordSupport;
|
private Boolean bungeecordSupport;
|
||||||
private Boolean localStorage;
|
private Boolean localStorage;
|
||||||
|
private Boolean customLocale;
|
||||||
|
|
||||||
private String sqlUsername, sqlPassword, sqlAddress;
|
private String sqlUsername, sqlPassword, sqlAddress;
|
||||||
|
|
||||||
|
@ -22,6 +23,8 @@ public class NickoConfiguration {
|
||||||
|
|
||||||
public ConfigurationSection getStorageSection() { return getConfig().getConfigurationSection("storage"); }
|
public ConfigurationSection getStorageSection() { return getConfig().getConfigurationSection("storage"); }
|
||||||
|
|
||||||
|
public ConfigurationSection getLocaleSection() { return getBungeecordSection().getConfigurationSection("locale"); }
|
||||||
|
|
||||||
public ConfigurationSection getRedisSection() { return getBungeecordSection().getConfigurationSection("redis"); }
|
public ConfigurationSection getRedisSection() { return getBungeecordSection().getConfigurationSection("redis"); }
|
||||||
|
|
||||||
public String getPrefix() {
|
public String getPrefix() {
|
||||||
|
@ -35,15 +38,26 @@ public class NickoConfiguration {
|
||||||
this.prefix = prefix;
|
this.prefix = prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDefaultLocale() {
|
public String getFallbackLocale() {
|
||||||
if (defaultLocale == null) {
|
if (fallbackLocale == null) {
|
||||||
return defaultLocale = getConfig().getString("locale");
|
return fallbackLocale = getLocaleSection().getString("fallback");
|
||||||
}
|
}
|
||||||
return defaultLocale;
|
return fallbackLocale;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDefaultLocale(String defaultLocale) {
|
public void setFallbackLocale(String fallbackLocale) {
|
||||||
this.defaultLocale = defaultLocale;
|
this.fallbackLocale = fallbackLocale;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCustomLocaleEnabled() {
|
||||||
|
if (customLocale == null) {
|
||||||
|
return customLocale = getStorageSection().getBoolean("local");
|
||||||
|
}
|
||||||
|
return customLocale;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomLocaleEnabled(Boolean localStorage) {
|
||||||
|
this.localStorage = localStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isBungeecordEnabled() {
|
public boolean isBungeecordEnabled() {
|
||||||
|
|
|
@ -21,22 +21,26 @@ public class I18N {
|
||||||
return profile.get().getLocale();
|
return profile.get().getLocale();
|
||||||
}
|
}
|
||||||
} catch (IllegalArgumentException exception) {
|
} catch (IllegalArgumentException exception) {
|
||||||
NickoBukkit.getInstance().getLogger().severe("Invalid locale provided, defaulting to " + Locale.getDefault().getCode() + ".");
|
NickoBukkit.getInstance().getLogger().severe("Invalid locale provided by " + player.getName() + ", defaulting to " + Locale.getDefault().getCode() + ".");
|
||||||
return Locale.getDefault();
|
return Locale.getDefault();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ResourceBundle getBundle(Player player) {
|
private static ResourceBundle getBundle(java.util.Locale locale) {
|
||||||
return ResourceBundle.getBundle("locale", LocaleUtils.toLocale(getLocale(player).getCode()));
|
return ResourceBundle.getBundle("locale", locale);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String translate(Player player, I18NDict key, Object... arguments) {
|
public static String translate(Player player, I18NDict key, Object... arguments) {
|
||||||
if (Locale.getDefault() == Locale.CUSTOM) {
|
final Locale locale = getLocale(player);
|
||||||
// TODO: 12/6/22 Actually return from custom language file
|
String translation;
|
||||||
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + key.key();
|
if (locale == Locale.CUSTOM) {
|
||||||
|
translation = "";
|
||||||
|
} else {
|
||||||
|
translation = getBundle(LocaleUtils.toLocale(locale.getCode())).getString(key.key());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
formatter.applyPattern(getBundle(player).getString(key.key()));
|
formatter.applyPattern(translation);
|
||||||
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + formatter.format(arguments);
|
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + formatter.format(arguments);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + key.key();
|
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + key.key();
|
||||||
|
@ -44,12 +48,16 @@ public class I18N {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String translateFlat(Player player, I18NDict key, Object... arguments) {
|
public static String translateFlat(Player player, I18NDict key, Object... arguments) {
|
||||||
if (Locale.getDefault() == Locale.CUSTOM) {
|
final Locale locale = getLocale(player);
|
||||||
// TODO: 12/6/22 Actually return from custom language file
|
String translation;
|
||||||
return key.key();
|
if (locale == Locale.CUSTOM) {
|
||||||
|
translation = "";
|
||||||
|
} else {
|
||||||
|
translation = getBundle(LocaleUtils.toLocale(locale.getCode())).getString(key.key());
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
formatter.applyPattern(getBundle(player).getString(key.key()));
|
formatter.applyPattern(translation);
|
||||||
return formatter.format(arguments);
|
return formatter.format(arguments);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return key.key();
|
return key.key();
|
||||||
|
|
|
@ -34,7 +34,7 @@ public enum Locale implements Serializable {
|
||||||
return getLocales().getOrDefault(code, defaultLocale);
|
return getLocales().getOrDefault(code, defaultLocale);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setDefault(Locale defaultLocale) {
|
public static void setFallback(Locale defaultLocale) {
|
||||||
Locale.defaultLocale = defaultLocale;
|
Locale.defaultLocale = defaultLocale;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,15 @@ import net.artelnatif.nicko.NickoBukkit;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class LocaleManager {
|
public class LocaleManager {
|
||||||
private static final String[] supportedLocales = new String[]{"en", "fr", "custom"};
|
private final NickoBukkit instance;
|
||||||
|
private final String[] supportedLocales = new String[]{"en", "fr", "custom"};
|
||||||
|
|
||||||
public static void setFallbackLocale(NickoBukkit instance) {
|
public LocaleManager(NickoBukkit instance) {
|
||||||
|
this.instance = instance;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void findFallbackLocale() {
|
||||||
final String locale = instance.getNickoConfig().getFallbackLocale();
|
final String locale = instance.getNickoConfig().getFallbackLocale();
|
||||||
try {
|
try {
|
||||||
if (Arrays.stream(supportedLocales).noneMatch(s -> s.equalsIgnoreCase(locale))) {
|
if (Arrays.stream(supportedLocales).noneMatch(s -> s.equalsIgnoreCase(locale))) {
|
||||||
|
|
Loading…
Reference in a new issue