feat: support custom language file

This commit is contained in:
aro 2022-12-20 12:01:09 +01:00
parent 3f297eba49
commit 84e558afb2
4 changed files with 82 additions and 34 deletions

View file

@ -105,7 +105,7 @@ public class NickoBukkit extends JavaPlugin {
localeManager = new LocaleManager(this);
localeManager.findFallbackLocale();
localeManager.installCustomLanguageFile();
final PluginCommand command = getCommand("nicko");
if (command != null) {
@ -154,6 +154,10 @@ public class NickoBukkit extends JavaPlugin {
public PlayerDataStore getDataStore() { return dataStore; }
public LocaleManager getLocaleManager() {
return localeManager;
}
public boolean isUnitTesting() {
return unitTesting;
}

View file

@ -13,16 +13,17 @@ public class I18N {
private final static MessageFormat formatter = new MessageFormat("");
private static Locale getLocale(Player player) {
final NickoBukkit instance = NickoBukkit.getInstance();
try {
final Optional<NickoProfile> profile = NickoBukkit.getInstance().getDataStore().getData(player.getUniqueId());
final Optional<NickoProfile> profile = instance.getDataStore().getData(player.getUniqueId());
if (profile.isEmpty()) {
return Locale.ENGLISH;
} else {
return profile.get().getLocale();
}
} catch (IllegalArgumentException exception) {
NickoBukkit.getInstance().getLogger().severe("Invalid locale provided by " + player.getName() + ", defaulting to " + Locale.getDefault().getCode() + ".");
return Locale.getDefault();
instance.getLogger().severe("Invalid locale provided by " + player.getName() + ", defaulting to " + LocaleManager.getFallback().getCode() + ".");
return LocaleManager.getFallback();
}
}
@ -31,31 +32,19 @@ public class I18N {
}
public static String translate(Player player, I18NDict key, Object... arguments) {
final Locale locale = getLocale(player);
String translation;
if (locale == Locale.CUSTOM) {
translation = "";
} else {
translation = getBundle(LocaleUtils.toLocale(locale.getCode())).getString(key.key());
}
final NickoBukkit instance = NickoBukkit.getInstance();
final String translation = findTranslation(player, key);
try {
formatter.applyPattern(translation);
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + formatter.format(arguments);
return instance.getNickoConfig().getPrefix() + formatter.format(arguments);
} catch (Exception e) {
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + key.key();
return instance.getNickoConfig().getPrefix() + key.key();
}
}
public static String translateFlat(Player player, I18NDict key, Object... arguments) {
final Locale locale = getLocale(player);
String translation;
if (locale == Locale.CUSTOM) {
translation = "";
} else {
translation = getBundle(LocaleUtils.toLocale(locale.getCode())).getString(key.key());
}
final String translation = findTranslation(player, key);
try {
formatter.applyPattern(translation);
return formatter.format(arguments);
@ -63,4 +52,18 @@ public class I18N {
return key.key();
}
}
private static String findTranslation(Player player, I18NDict key) {
final NickoBukkit instance = NickoBukkit.getInstance();
final Locale locale = getLocale(player);
String translation;
if (locale == Locale.CUSTOM) {
translation = instance.getLocaleManager().getCustomLanguageFile().getProperty(key.key(), key.key());
} else {
translation = getBundle(LocaleUtils.toLocale(locale.getCode())).getString(key.key());
}
return translation;
}
}

View file

@ -9,7 +9,6 @@ public enum Locale implements Serializable {
CUSTOM("custom", "Server Custom");
private static HashMap<String, Locale> locales;
private static Locale defaultLocale;
private final String code;
private transient final String name;
@ -31,15 +30,7 @@ public enum Locale implements Serializable {
}
public static Locale fromCode(String code) {
return getLocales().getOrDefault(code, defaultLocale);
}
public static void setFallback(Locale defaultLocale) {
Locale.defaultLocale = defaultLocale;
}
public static Locale getDefault() {
return defaultLocale;
return getLocales().getOrDefault(code, LocaleManager.getFallback());
}
public String getCode() {

View file

@ -2,15 +2,65 @@ package net.artelnatif.nicko.i18n;
import net.artelnatif.nicko.NickoBukkit;
import java.io.*;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Properties;
public class LocaleManager {
private static Locale fallback;
private final NickoBukkit instance;
private final String[] supportedLocales = new String[]{"en", "fr", "custom"};
private final File directory = new File(NickoBukkit.getInstance().getDataFolder() + "/lang/");
private final File file = new File(directory, "custom.properties");
private Properties customLanguageFile;
public LocaleManager(NickoBukkit instance) {
this.instance = instance;
}
public static void setFallback(Locale fallback) {
LocaleManager.fallback = fallback;
}
public static Locale getFallback() {
return fallback;
}
public void installCustomLanguageFile() {
final InputStream resource = instance.getResource("locale_en.properties");
try {
if (resource != null) {
if (!file.exists()) {
instance.getLogger().info("Installed Custom language file as \"custom.properties\"");
Files.copy(resource, file.toPath());
}
}
} catch (IOException e) {
// TODO: 12/19/22 Handle error
throw new RuntimeException(e);
}
}
public Properties getCustomLanguageFile() {
if (customLanguageFile == null) {
final Properties properties = new Properties();
try {
properties.load(new BufferedInputStream(new FileInputStream(file)));
return customLanguageFile = properties;
} catch (IOException e) {
instance.getLogger().warning("Couldn't load Custom properties language file, falling back to English.");
if (LocaleManager.getFallback() != Locale.ENGLISH) {
LocaleManager.setFallback(Locale.ENGLISH);
}
return customLanguageFile = null;
}
}
return customLanguageFile;
}
public void findFallbackLocale() {
@ -18,15 +68,15 @@ public class LocaleManager {
try {
if (Arrays.stream(supportedLocales).noneMatch(s -> s.equalsIgnoreCase(locale))) {
instance.getLogger().severe(locale + " is not a supported locale, defaulting to English.");
Locale.setFallback(Locale.ENGLISH);
LocaleManager.setFallback(Locale.ENGLISH);
return;
}
final Locale defaultLocale = Locale.fromCode(locale);
instance.getLogger().info("Fallback locale set to " + defaultLocale.getName() + ".");
Locale.setFallback(defaultLocale);
LocaleManager.setFallback(defaultLocale);
} catch (Exception e) {
instance.getLogger().severe(locale + " is not a valid locale, defaulting to English.");
Locale.setFallback(Locale.ENGLISH);
LocaleManager.setFallback(Locale.ENGLISH);
}
}
}