From 8b4431b3602c32f55075f041bece46cb7c8b82d9 Mon Sep 17 00:00:00 2001 From: NebelNidas Date: Sat, 20 Apr 2024 07:23:28 +0200 Subject: [PATCH] Optimize bundle loading --- .../net/fabricmc/mappingio/i18n/I18n.java | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/fabricmc/mappingio/i18n/I18n.java b/src/main/java/net/fabricmc/mappingio/i18n/I18n.java index f49368f2..45f137d6 100644 --- a/src/main/java/net/fabricmc/mappingio/i18n/I18n.java +++ b/src/main/java/net/fabricmc/mappingio/i18n/I18n.java @@ -18,7 +18,9 @@ import java.io.IOException; import java.io.InputStreamReader; +import java.io.PrintWriter; import java.io.Reader; +import java.io.StringWriter; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.HashMap; @@ -26,6 +28,8 @@ import java.util.Map; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import org.jetbrains.annotations.ApiStatus; @@ -40,21 +44,41 @@ public static String translate(String key, Locale locale, Object... args) { public static String translate(String key, Locale locale) { try { - return messageBundles.getOrDefault(locale, load(locale)).getString(key); + return load(locale).getString(key); } catch (Exception e) { - System.err.println("Exception while translating key " + key + " to locale " + locale.toLanguageTag() + ": " + e.getMessage()); + System.err.println("Exception while translating key " + key + " to locale " + locale.toLanguageTag() + ": " + getStackTrace(e)); if (locale == fallbackLocale) return key; try { - return messageBundles.getOrDefault(fallbackLocale, load(fallbackLocale)).getString(key); + return load(fallbackLocale).getString(key); } catch (Exception e2) { - System.err.println("Exception while translating key " + key + " to fallback locale: " + e2.getMessage()); + System.err.println("Exception while translating key " + key + " to fallback locale: " + getStackTrace(e2)); return key; } } } private static ResourceBundle load(Locale locale) { + ResourceBundle bundle = messageBundles.get(locale); + + if (bundle != null) { + return bundle; + } + + bundlesLock.lock(); + + try { + if ((bundle = messageBundles.get(locale)) != null) { + return bundle; + } + + return load0(locale); + } finally { + bundlesLock.unlock(); + } + } + + private static ResourceBundle load0(Locale locale) { ResourceBundle resBundle; String resName = String.format("/mappingio/lang/%s.properties", locale.toLanguageTag().replace('-', '_').toLowerCase(Locale.ROOT)); URL resUrl = I18n.class.getResource(resName); @@ -72,6 +96,16 @@ private static ResourceBundle load(Locale locale) { } } + private static String getStackTrace(Throwable t) { + if (t == null) return null; + + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + t.printStackTrace(pw); + return sw.toString(); + } + + private static final Lock bundlesLock = new ReentrantLock(); private static final Locale fallbackLocale = Locale.US; private static final Map messageBundles = new HashMap<>(); }