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 MojangAPI mojangAPI;
|
||||
private PlayerDataStore dataStore;
|
||||
private LocaleManager localeManager;
|
||||
|
||||
public NickoBukkit() { this.unitTesting = false; }
|
||||
|
||||
|
@ -102,7 +103,9 @@ public class NickoBukkit extends JavaPlugin {
|
|||
saveDefaultConfig();
|
||||
config = new NickoConfiguration(this);
|
||||
|
||||
LocaleManager.setFallbackLocale(this);
|
||||
localeManager = new LocaleManager(this);
|
||||
localeManager.findFallbackLocale();
|
||||
|
||||
|
||||
final PluginCommand command = getCommand("nicko");
|
||||
if (command != null) {
|
||||
|
|
|
@ -7,10 +7,11 @@ import org.bukkit.configuration.file.FileConfiguration;
|
|||
public class NickoConfiguration {
|
||||
private final NickoBukkit nicko;
|
||||
private String prefix;
|
||||
private String defaultLocale;
|
||||
private String fallbackLocale;
|
||||
|
||||
private Boolean bungeecordSupport;
|
||||
private Boolean localStorage;
|
||||
private Boolean customLocale;
|
||||
|
||||
private String sqlUsername, sqlPassword, sqlAddress;
|
||||
|
||||
|
@ -22,6 +23,8 @@ public class NickoConfiguration {
|
|||
|
||||
public ConfigurationSection getStorageSection() { return getConfig().getConfigurationSection("storage"); }
|
||||
|
||||
public ConfigurationSection getLocaleSection() { return getBungeecordSection().getConfigurationSection("locale"); }
|
||||
|
||||
public ConfigurationSection getRedisSection() { return getBungeecordSection().getConfigurationSection("redis"); }
|
||||
|
||||
public String getPrefix() {
|
||||
|
@ -35,15 +38,26 @@ public class NickoConfiguration {
|
|||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public String getDefaultLocale() {
|
||||
if (defaultLocale == null) {
|
||||
return defaultLocale = getConfig().getString("locale");
|
||||
public String getFallbackLocale() {
|
||||
if (fallbackLocale == null) {
|
||||
return fallbackLocale = getLocaleSection().getString("fallback");
|
||||
}
|
||||
return defaultLocale;
|
||||
return fallbackLocale;
|
||||
}
|
||||
|
||||
public void setDefaultLocale(String defaultLocale) {
|
||||
this.defaultLocale = defaultLocale;
|
||||
public void setFallbackLocale(String fallbackLocale) {
|
||||
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() {
|
||||
|
|
|
@ -21,22 +21,26 @@ public class I18N {
|
|||
return profile.get().getLocale();
|
||||
}
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
|
||||
private static ResourceBundle getBundle(Player player) {
|
||||
return ResourceBundle.getBundle("locale", LocaleUtils.toLocale(getLocale(player).getCode()));
|
||||
private static ResourceBundle getBundle(java.util.Locale locale) {
|
||||
return ResourceBundle.getBundle("locale", locale);
|
||||
}
|
||||
|
||||
public static String translate(Player player, I18NDict key, Object... arguments) {
|
||||
if (Locale.getDefault() == Locale.CUSTOM) {
|
||||
// TODO: 12/6/22 Actually return from custom language file
|
||||
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + key.key();
|
||||
final Locale locale = getLocale(player);
|
||||
String translation;
|
||||
if (locale == Locale.CUSTOM) {
|
||||
translation = "";
|
||||
} else {
|
||||
translation = getBundle(LocaleUtils.toLocale(locale.getCode())).getString(key.key());
|
||||
}
|
||||
|
||||
try {
|
||||
formatter.applyPattern(getBundle(player).getString(key.key()));
|
||||
formatter.applyPattern(translation);
|
||||
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + formatter.format(arguments);
|
||||
} catch (Exception e) {
|
||||
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + key.key();
|
||||
|
@ -44,12 +48,16 @@ public class I18N {
|
|||
}
|
||||
|
||||
public static String translateFlat(Player player, I18NDict key, Object... arguments) {
|
||||
if (Locale.getDefault() == Locale.CUSTOM) {
|
||||
// TODO: 12/6/22 Actually return from custom language file
|
||||
return key.key();
|
||||
final Locale locale = getLocale(player);
|
||||
String translation;
|
||||
if (locale == Locale.CUSTOM) {
|
||||
translation = "";
|
||||
} else {
|
||||
translation = getBundle(LocaleUtils.toLocale(locale.getCode())).getString(key.key());
|
||||
}
|
||||
|
||||
try {
|
||||
formatter.applyPattern(getBundle(player).getString(key.key()));
|
||||
formatter.applyPattern(translation);
|
||||
return formatter.format(arguments);
|
||||
} catch (Exception e) {
|
||||
return key.key();
|
||||
|
|
|
@ -34,7 +34,7 @@ public enum Locale implements Serializable {
|
|||
return getLocales().getOrDefault(code, defaultLocale);
|
||||
}
|
||||
|
||||
public static void setDefault(Locale defaultLocale) {
|
||||
public static void setFallback(Locale defaultLocale) {
|
||||
Locale.defaultLocale = defaultLocale;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,9 +5,15 @@ import net.artelnatif.nicko.NickoBukkit;
|
|||
import java.util.Arrays;
|
||||
|
||||
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();
|
||||
try {
|
||||
if (Arrays.stream(supportedLocales).noneMatch(s -> s.equalsIgnoreCase(locale))) {
|
||||
|
|
Loading…
Reference in a new issue