From c2d4aeaf8509fa06e3c9101fee28e71136b2b027 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Mon, 29 Apr 2019 01:25:00 +0300 Subject: [PATCH 1/5] Try to load locale from plugin data folder first --- .../gmail/nossr50/locale/LocaleLoader.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index c48fd66bc..b5b65d928 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -4,10 +4,17 @@ import com.gmail.nossr50.config.Config; import com.gmail.nossr50.mcMMO; import org.bukkit.ChatColor; +import java.io.IOException; +import java.io.Reader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.text.MessageFormat; import java.util.Locale; import java.util.MissingResourceException; +import java.util.PropertyResourceBundle; import java.util.ResourceBundle; +import java.util.logging.Level; public final class LocaleLoader { private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale"; @@ -85,7 +92,21 @@ public final class LocaleLoader { locale = new Locale(myLocale[0], myLocale[1]); } - bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); + if (locale == null) { + throw new IllegalStateException("Failed to parse locale string '" + Config.getInstance().getLocale() + "'"); + } + + Path localePath = Paths.get(mcMMO.getMainDirectory() + "locale_" + locale.toString() + ".properties"); + if (Files.exists(localePath) && Files.isRegularFile(localePath)) { + try (Reader localeReader = Files.newBufferedReader(localePath)) { + bundle = new PropertyResourceBundle(localeReader); + } catch (IOException e) { + mcMMO.p.getLogger().log(Level.WARNING, "Failed to load locale from " + localePath, e); + bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); + } + } else { + bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); + } enBundle = ResourceBundle.getBundle(BUNDLE_ROOT, Locale.US); } } From 0f2e1ea74084ce49eeb10a0447696179e375aaf4 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Mon, 29 Apr 2019 20:58:54 +0300 Subject: [PATCH 2/5] Fall back to locale loaded from JAR when filesystem one doesn't have requested key --- .../com/gmail/nossr50/locale/LocaleLoader.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index b5b65d928..906397931 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -19,6 +19,7 @@ import java.util.logging.Level; public final class LocaleLoader { private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale"; private static ResourceBundle bundle = null; + private static ResourceBundle filesystemBundle = null; private static ResourceBundle enBundle = null; private LocaleLoader() {}; @@ -39,6 +40,13 @@ public final class LocaleLoader { initialize(); } + if (filesystemBundle != null) { + try { + return getString(key, filesystemBundle, messageArguments); + } + catch (MissingResourceException ignored) {} + } + try { return getString(key, bundle, messageArguments); } @@ -99,14 +107,12 @@ public final class LocaleLoader { Path localePath = Paths.get(mcMMO.getMainDirectory() + "locale_" + locale.toString() + ".properties"); if (Files.exists(localePath) && Files.isRegularFile(localePath)) { try (Reader localeReader = Files.newBufferedReader(localePath)) { - bundle = new PropertyResourceBundle(localeReader); + filesystemBundle = new PropertyResourceBundle(localeReader); } catch (IOException e) { mcMMO.p.getLogger().log(Level.WARNING, "Failed to load locale from " + localePath, e); - bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); } - } else { - bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); } + bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); enBundle = ResourceBundle.getBundle(BUNDLE_ROOT, Locale.US); } } From 1ca48051add5f289ff79bc7584ee083a6b1a53f9 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Tue, 30 Apr 2019 09:31:33 +0300 Subject: [PATCH 3/5] Dedicated directory for locales --- src/main/java/com/gmail/nossr50/locale/LocaleLoader.java | 2 +- src/main/java/com/gmail/nossr50/mcMMO.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index 906397931..11522164b 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -104,7 +104,7 @@ public final class LocaleLoader { throw new IllegalStateException("Failed to parse locale string '" + Config.getInstance().getLocale() + "'"); } - Path localePath = Paths.get(mcMMO.getMainDirectory() + "locale_" + locale.toString() + ".properties"); + Path localePath = Paths.get(mcMMO.getLocalesDirectory() + "locale_" + locale.toString() + ".properties"); if (Files.exists(localePath) && Files.isRegularFile(localePath)) { try (Reader localeReader = Files.newBufferedReader(localePath)) { filesystemBundle = new PropertyResourceBundle(localeReader); diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 644ad1bce..9b39ecd60 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -79,6 +79,7 @@ public class mcMMO extends JavaPlugin { /* File Paths */ private static String mainDirectory; + private static String localesDirectory; private static String flatFileDirectory; private static String usersFile; private static String modDirectory; @@ -357,6 +358,10 @@ public class mcMMO extends JavaPlugin { return mainDirectory; } + public static String getLocalesDirectory() { + return localesDirectory; + } + public static String getFlatFileDirectory() { return flatFileDirectory; } @@ -432,6 +437,7 @@ public class mcMMO extends JavaPlugin { private void setupFilePaths() { mcmmo = getFile(); mainDirectory = getDataFolder().getPath() + File.separator; + localesDirectory = mainDirectory + "locales" + File.separator; flatFileDirectory = mainDirectory + "flatfile" + File.separator; usersFile = flatFileDirectory + "mcmmo.users"; modDirectory = mainDirectory + "mods" + File.separator; @@ -485,6 +491,8 @@ public class mcMMO extends JavaPlugin { File currentFlatfilePath = new File(flatFileDirectory); currentFlatfilePath.mkdirs(); + File localesDirectoryPath = new File(localesDirectory); + localesDirectoryPath.mkdirs(); } private void loadConfigFiles() { From ec574a6b63874d18ecd397b6b36cba1ebbd64788 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Tue, 30 Apr 2019 17:58:33 +0300 Subject: [PATCH 4/5] Cache raw messages from resource bundles Should speed things up a bit as next message lookups are from single hashmap instead --- .../gmail/nossr50/locale/LocaleLoader.java | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index 11522164b..c16f2c425 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -10,7 +10,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.text.MessageFormat; +import java.util.HashMap; import java.util.Locale; +import java.util.Map; import java.util.MissingResourceException; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; @@ -18,6 +20,7 @@ import java.util.logging.Level; public final class LocaleLoader { private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale"; + private static Map bundleCache = new HashMap<>(); private static ResourceBundle bundle = null; private static ResourceBundle filesystemBundle = null; private static ResourceBundle enBundle = null; @@ -40,32 +43,33 @@ public final class LocaleLoader { initialize(); } + String rawMessage = bundleCache.computeIfAbsent(key, LocaleLoader::getRawString); + return formatString(rawMessage, messageArguments); + } + + private static String getRawString(String key) { if (filesystemBundle != null) { try { - return getString(key, filesystemBundle, messageArguments); + return filesystemBundle.getString(key); } catch (MissingResourceException ignored) {} } try { - return getString(key, bundle, messageArguments); + return bundle.getString(key); } - catch (MissingResourceException ex) { - try { - return getString(key, enBundle, messageArguments); - } - catch (MissingResourceException ex2) { - if (!key.contains("Guides")) { - mcMMO.p.getLogger().warning("Could not find locale string: " + key); - } + catch (MissingResourceException ignored) {} - return '!' + key + '!'; - } + try { + return enBundle.getString(key); } - } + catch (MissingResourceException ignored) { + if (!key.contains("Guides")) { + mcMMO.p.getLogger().warning("Could not find locale string: " + key); + } - private static String getString(String key, ResourceBundle bundle, Object... messageArguments) throws MissingResourceException { - return formatString(bundle.getString(key), messageArguments); + return '!' + key + '!'; + } } public static String formatString(String string, Object... messageArguments) { From 5dc9c3f732a38fe01255cba96989f8fa15d22f73 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Tue, 30 Apr 2019 20:14:42 +0300 Subject: [PATCH 5/5] Note user that locale is loaded from locales directory --- src/main/java/com/gmail/nossr50/locale/LocaleLoader.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index c16f2c425..15b31c36b 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -111,6 +111,7 @@ public final class LocaleLoader { Path localePath = Paths.get(mcMMO.getLocalesDirectory() + "locale_" + locale.toString() + ".properties"); if (Files.exists(localePath) && Files.isRegularFile(localePath)) { try (Reader localeReader = Files.newBufferedReader(localePath)) { + mcMMO.p.getLogger().log(Level.INFO, "Loading locale from {0}", localePath); filesystemBundle = new PropertyResourceBundle(localeReader); } catch (IOException e) { mcMMO.p.getLogger().log(Level.WARNING, "Failed to load locale from " + localePath, e);