Skip to content

Commit

Permalink
Cache raw messages from resource bundles
Browse files Browse the repository at this point in the history
Should speed things up a bit as next message lookups are from single
hashmap instead
  • Loading branch information
mikroskeem committed Apr 30, 2019
1 parent 1ca4805 commit ec574a6
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/main/java/com/gmail/nossr50/locale/LocaleLoader.java
Expand Up @@ -10,14 +10,17 @@
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;
import java.util.logging.Level;

public final class LocaleLoader {
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 filesystemBundle = null;
private static ResourceBundle enBundle = null;
Expand All @@ -40,32 +43,33 @@ public static String getString(String key, Object... messageArguments) {
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) {
Expand Down

0 comments on commit ec574a6

Please sign in to comment.