From 84e558afb27e4a0aa7bbad6634a9978445ae9757 Mon Sep 17 00:00:00 2001 From: aro Date: Tue, 20 Dec 2022 12:01:09 +0100 Subject: [PATCH] feat: support custom language file --- .../net/artelnatif/nicko/NickoBukkit.java | 6 +- .../java/net/artelnatif/nicko/i18n/I18N.java | 43 +++++++------- .../net/artelnatif/nicko/i18n/Locale.java | 11 +--- .../artelnatif/nicko/i18n/LocaleManager.java | 56 ++++++++++++++++++- 4 files changed, 82 insertions(+), 34 deletions(-) diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/NickoBukkit.java b/nicko-core/src/main/java/net/artelnatif/nicko/NickoBukkit.java index 80e1c31..9597007 100644 --- a/nicko-core/src/main/java/net/artelnatif/nicko/NickoBukkit.java +++ b/nicko-core/src/main/java/net/artelnatif/nicko/NickoBukkit.java @@ -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; } diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/i18n/I18N.java b/nicko-core/src/main/java/net/artelnatif/nicko/i18n/I18N.java index e030971..d425e4b 100644 --- a/nicko-core/src/main/java/net/artelnatif/nicko/i18n/I18N.java +++ b/nicko-core/src/main/java/net/artelnatif/nicko/i18n/I18N.java @@ -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 profile = NickoBukkit.getInstance().getDataStore().getData(player.getUniqueId()); + final Optional 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; + } } diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/i18n/Locale.java b/nicko-core/src/main/java/net/artelnatif/nicko/i18n/Locale.java index ee01a11..ce3b81e 100644 --- a/nicko-core/src/main/java/net/artelnatif/nicko/i18n/Locale.java +++ b/nicko-core/src/main/java/net/artelnatif/nicko/i18n/Locale.java @@ -9,7 +9,6 @@ public enum Locale implements Serializable { CUSTOM("custom", "Server Custom"); private static HashMap 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() { diff --git a/nicko-core/src/main/java/net/artelnatif/nicko/i18n/LocaleManager.java b/nicko-core/src/main/java/net/artelnatif/nicko/i18n/LocaleManager.java index b2e5ea1..722687f 100644 --- a/nicko-core/src/main/java/net/artelnatif/nicko/i18n/LocaleManager.java +++ b/nicko-core/src/main/java/net/artelnatif/nicko/i18n/LocaleManager.java @@ -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); } } }