refactor: fallback locale + feat: custom locale

This commit is contained in:
aro 2022-12-17 12:28:57 +01:00
parent 863aa46a7b
commit 9ed076e00d
3 changed files with 18 additions and 12 deletions

View file

@ -102,7 +102,7 @@ public class NickoBukkit extends JavaPlugin {
saveDefaultConfig(); saveDefaultConfig();
config = new NickoConfiguration(this); config = new NickoConfiguration(this);
LocaleManager.setDefaultLocale(this); LocaleManager.setFallbackLocale(this);
final PluginCommand command = getCommand("nicko"); final PluginCommand command = getCommand("nicko");
if (command != null) { if (command != null) {

View file

@ -7,20 +7,20 @@ import java.util.Arrays;
public class LocaleManager { public class LocaleManager {
private static final String[] supportedLocales = new String[]{"en", "fr", "custom"}; private static final String[] supportedLocales = new String[]{"en", "fr", "custom"};
public static void setDefaultLocale(NickoBukkit instance) { public static void setFallbackLocale(NickoBukkit instance) {
final String locale = instance.getNickoConfig().getDefaultLocale(); 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))) {
instance.getLogger().severe(locale + " is not a supported locale, defaulting to English."); instance.getLogger().severe(locale + " is not a supported locale, defaulting to English.");
Locale.setDefault(Locale.ENGLISH); Locale.setFallback(Locale.ENGLISH);
return; return;
} }
final Locale defaultLocale = Locale.fromCode(locale); final Locale defaultLocale = Locale.fromCode(locale);
instance.getLogger().info("Default locale set to " + defaultLocale.getName() + "."); instance.getLogger().info("Fallback locale set to " + defaultLocale.getName() + ".");
Locale.setDefault(defaultLocale); Locale.setFallback(defaultLocale);
} catch (Exception e) { } catch (Exception e) {
instance.getLogger().severe(locale + " is not a valid locale, defaulting to English."); instance.getLogger().severe(locale + " is not a valid locale, defaulting to English.");
Locale.setDefault(Locale.ENGLISH); Locale.setFallback(Locale.ENGLISH);
} }
} }
} }

View file

@ -3,7 +3,7 @@
prefix: "§8[§6Nicko§8] " prefix: "§8[§6Nicko§8] "
bungeecord: bungeecord:
# Enables Bungeecord support, switching through servers will keep player's skins. # Enables Bungeecord support, switching through servers will transfer player's disguise.
# Accepted values: false (Disabled), true (Enabled) # Accepted values: false (Disabled), true (Enabled)
enabled: false enabled: false
redis: redis:
@ -13,10 +13,16 @@ bungeecord:
ttl: -1 ttl: -1
# Localisation: # Localisation:
# By default, Nicko tries to get the locale from the player's preferences. locale:
# If that fails, the locale fallback is the one provided by this option. # By default, Nicko tries to get the locale from the player's preferences.
# Accepted values: fr (French), en (English), custom (Custom language file). # If that fails, the locale fallback is the one provided by this option.
locale: "en" # Accepted values: fr (French), en (English), custom (Custom language file).
fallback: "en"
# Nicko will copy the English locale as "custom.yml"
# and will use the translations in that file when "Server Custom"
# is selected as the player's locale/default locale.
use_custom_locale: false
storage: storage:
# Indicates wherever the data will be stored # Indicates wherever the data will be stored