From c13593def14e36eda04417c3f4c615926d5d014e Mon Sep 17 00:00:00 2001 From: NuclearW Date: Wed, 6 Feb 2013 01:05:09 -0500 Subject: [PATCH] Refactor LocaleLoader a bit --- .../gmail/nossr50/locale/LocaleLoader.java | 49 ++++++++++++------- src/main/java/com/gmail/nossr50/mcMMO.java | 1 - 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index 8443ea2c3..ef95ac9e8 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -10,8 +10,8 @@ import org.bukkit.ChatColor; import com.gmail.nossr50.config.Config; public final class LocaleLoader { - private static final String BUNDLE_NAME = "com.gmail.nossr50.locale.locale"; - private static ResourceBundle RESOURCE_BUNDLE = null; + private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale"; + private static ResourceBundle bundle = null; private LocaleLoader() {}; @@ -27,23 +27,12 @@ public final class LocaleLoader { * @return The properly formatted locale string */ public static String getString(String key, Object ... messageArguments) { + if (bundle == null) { + initialize(); + } + try { - if (RESOURCE_BUNDLE == null) { - Locale.setDefault(new Locale("en", "US")); - Locale locale = null; - String[] myLocale = Config.getInstance().getLocale().split("[-_ ]"); - - if (myLocale.length == 1) { - locale = new Locale(myLocale[0]); - } - else if (myLocale.length >= 2) { - locale = new Locale(myLocale[0], myLocale[1]); - } - - RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME, locale); - } - - String output = RESOURCE_BUNDLE.getString(key); + String output = bundle.getString(key); if (messageArguments != null) { MessageFormat formatter = new MessageFormat(""); @@ -60,6 +49,30 @@ public final class LocaleLoader { } } + public static Locale getCurrentLocale() { + if (bundle == null) { + initialize(); + } + return bundle.getLocale(); + } + + private static void initialize() { + if (bundle == null) { + Locale.setDefault(new Locale("en", "US")); + Locale locale = null; + String[] myLocale = Config.getInstance().getLocale().split("[-_ ]"); + + if (myLocale.length == 1) { + locale = new Locale(myLocale[0]); + } + else if (myLocale.length >= 2) { + locale = new Locale(myLocale[0], myLocale[1]); + } + + bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); + } + } + private static String addColors(String input) { input = input.replaceAll("\\Q[[BLACK]]\\E", ChatColor.BLACK.toString()); input = input.replaceAll("\\Q[[DARK_BLUE]]\\E", ChatColor.DARK_BLUE.toString()); diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 33fadccc3..1b6a5465b 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -564,4 +564,3 @@ public class mcMMO extends JavaPlugin { xpEventEnabled = !xpEventEnabled; } } -