From d47a51b91d9aacbf2e51283fcbb703116d1f1c75 Mon Sep 17 00:00:00 2001 From: ile123ile Date: Thu, 28 Aug 2014 18:22:30 -0700 Subject: [PATCH] Added a way to add new localization files --- .../gmail/nossr50/locale/LocaleLoader.java | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index 85d18e9f8..1258ccd77 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -1,6 +1,8 @@ package com.gmail.nossr50.locale; import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; @@ -12,6 +14,8 @@ import com.gmail.nossr50.config.Config; public final class LocaleLoader { private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale"; + private static List bundles = new ArrayList(); + private static List defaultBundles = new ArrayList(); private static ResourceBundle bundle = null; private static ResourceBundle enBundle = null; @@ -33,6 +37,16 @@ public final class LocaleLoader { initialize(); } + for(ResourceBundle bundle : bundles) { + if(bundle.containsKey(key)) { + return getString(key, bundle, messageArguments); + } + } + for(ResourceBundle bundle : defaultBundles) { + if(bundle.containsKey(key)) { + return getString(key, bundle, messageArguments); + } + } try { return getString(key, bundle, messageArguments); } @@ -75,22 +89,33 @@ public final class LocaleLoader { 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]); - } + Locale locale = getLocalFromConfig(); bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); enBundle = ResourceBundle.getBundle(BUNDLE_ROOT, Locale.US); } } + private static Locale getLocalFromConfig() { + 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]); + } + return locale; + } + + public static void addResourceBundle(String bundleRoot, Locale defaultLocale) { + Locale locale = getLocalFromConfig(); + bundles.add(ResourceBundle.getBundle(bundleRoot, locale)); + defaultBundles.add(ResourceBundle.getBundle(bundleRoot, defaultLocale)); + } + 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());