Cache raw messages from resource bundles

Should speed things up a bit as next message lookups are from single
hashmap instead
This commit is contained in:
Mark Vainomaa 2019-04-30 17:58:33 +03:00
parent 1ca48051ad
commit ec574a6b63

View File

@ -10,7 +10,9 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException; import java.util.MissingResourceException;
import java.util.PropertyResourceBundle; import java.util.PropertyResourceBundle;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@ -18,6 +20,7 @@ import java.util.logging.Level;
public final class LocaleLoader { public final class LocaleLoader {
private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale"; private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale";
private static Map<String, String> bundleCache = new HashMap<>();
private static ResourceBundle bundle = null; private static ResourceBundle bundle = null;
private static ResourceBundle filesystemBundle = null; private static ResourceBundle filesystemBundle = null;
private static ResourceBundle enBundle = null; private static ResourceBundle enBundle = null;
@ -40,21 +43,27 @@ public final class LocaleLoader {
initialize(); initialize();
} }
String rawMessage = bundleCache.computeIfAbsent(key, LocaleLoader::getRawString);
return formatString(rawMessage, messageArguments);
}
private static String getRawString(String key) {
if (filesystemBundle != null) { if (filesystemBundle != null) {
try { try {
return getString(key, filesystemBundle, messageArguments); return filesystemBundle.getString(key);
} }
catch (MissingResourceException ignored) {} catch (MissingResourceException ignored) {}
} }
try { try {
return getString(key, bundle, messageArguments); return bundle.getString(key);
} }
catch (MissingResourceException ex) { catch (MissingResourceException ignored) {}
try { try {
return getString(key, enBundle, messageArguments); return enBundle.getString(key);
} }
catch (MissingResourceException ex2) { catch (MissingResourceException ignored) {
if (!key.contains("Guides")) { if (!key.contains("Guides")) {
mcMMO.p.getLogger().warning("Could not find locale string: " + key); mcMMO.p.getLogger().warning("Could not find locale string: " + key);
} }
@ -62,11 +71,6 @@ public final class LocaleLoader {
return '!' + key + '!'; return '!' + key + '!';
} }
} }
}
private static String getString(String key, ResourceBundle bundle, Object... messageArguments) throws MissingResourceException {
return formatString(bundle.getString(key), messageArguments);
}
public static String formatString(String string, Object... messageArguments) { public static String formatString(String string, Object... messageArguments) {
if (messageArguments != null) { if (messageArguments != null) {