mcMMO/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java

114 lines
4.4 KiB
Java
Raw Normal View History

2012-04-27 11:47:11 +02:00
package com.gmail.nossr50.locale;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.bukkit.ChatColor;
import com.gmail.nossr50.config.Config;
2013-01-26 23:01:55 +01:00
public final class LocaleLoader {
2013-02-06 07:05:09 +01:00
private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale";
private static ResourceBundle bundle = null;
2013-02-15 05:27:03 +01:00
private static ResourceBundle enBundle = null;
2012-04-27 11:47:11 +02:00
2013-01-26 23:01:55 +01:00
private LocaleLoader() {};
2012-04-27 11:47:11 +02:00
public static String getString(String key) {
2013-02-02 08:55:49 +01:00
return getString(key, (Object[]) null);
2012-04-27 11:47:11 +02:00
}
/**
* Gets the appropriate string from the Locale files.
*
* @param key The key to look up the string with
* @param messageArguments Any arguments to be added to the string
* @return The properly formatted locale string
*/
2013-02-02 08:55:49 +01:00
public static String getString(String key, Object ... messageArguments) {
2013-02-06 07:05:09 +01:00
if (bundle == null) {
initialize();
}
2012-04-27 11:47:11 +02:00
2013-02-06 07:05:09 +01:00
try {
2013-02-15 05:27:03 +01:00
return getString(key, bundle, messageArguments);
}
catch (MissingResourceException ex) {
try {
return getString(key, enBundle, messageArguments);
2012-04-27 11:47:11 +02:00
}
2013-02-15 05:27:03 +01:00
catch (MissingResourceException ex2) {
return '!' + key + '!';
}
}
}
2012-04-27 11:47:11 +02:00
2013-02-15 05:27:03 +01:00
private static String getString(String key, ResourceBundle bundle, Object ... messageArguments) throws MissingResourceException {
String output = bundle.getString(key);
2012-04-27 11:47:11 +02:00
2013-02-15 05:27:03 +01:00
if (messageArguments != null) {
MessageFormat formatter = new MessageFormat("");
formatter.applyPattern(output);
output = formatter.format(messageArguments);
2012-04-27 11:47:11 +02:00
}
2013-02-15 05:27:03 +01:00
output = addColors(output);
return output;
2012-04-27 11:47:11 +02:00
}
2013-02-06 07:05:09 +01:00
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);
2013-02-15 05:27:03 +01:00
enBundle = ResourceBundle.getBundle(BUNDLE_ROOT, Locale.US);
2013-02-06 07:05:09 +01:00
}
}
2012-04-27 11:47:11 +02:00
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());
input = input.replaceAll("\\Q[[DARK_GREEN]]\\E", ChatColor.DARK_GREEN.toString());
input = input.replaceAll("\\Q[[DARK_AQUA]]\\E", ChatColor.DARK_AQUA.toString());
input = input.replaceAll("\\Q[[DARK_RED]]\\E", ChatColor.DARK_RED.toString());
input = input.replaceAll("\\Q[[DARK_PURPLE]]\\E", ChatColor.DARK_PURPLE.toString());
input = input.replaceAll("\\Q[[GOLD]]\\E", ChatColor.GOLD.toString());
input = input.replaceAll("\\Q[[GRAY]]\\E", ChatColor.GRAY.toString());
input = input.replaceAll("\\Q[[DARK_GRAY]]\\E", ChatColor.DARK_GRAY.toString());
input = input.replaceAll("\\Q[[BLUE]]\\E", ChatColor.BLUE.toString());
input = input.replaceAll("\\Q[[GREEN]]\\E", ChatColor.GREEN.toString());
input = input.replaceAll("\\Q[[AQUA]]\\E", ChatColor.AQUA.toString());
input = input.replaceAll("\\Q[[RED]]\\E", ChatColor.RED.toString());
input = input.replaceAll("\\Q[[LIGHT_PURPLE]]\\E", ChatColor.LIGHT_PURPLE.toString());
input = input.replaceAll("\\Q[[YELLOW]]\\E", ChatColor.YELLOW.toString());
input = input.replaceAll("\\Q[[WHITE]]\\E", ChatColor.WHITE.toString());
input = input.replaceAll("\\Q[[BOLD]]\\E", ChatColor.BOLD.toString());
input = input.replaceAll("\\Q[[UNDERLINE]]\\E", ChatColor.UNDERLINE.toString());
input = input.replaceAll("\\Q[[ITALIC]]\\E", ChatColor.ITALIC.toString());
input = input.replaceAll("\\Q[[STRIKE]]\\E", ChatColor.STRIKETHROUGH.toString());
input = input.replaceAll("\\Q[[MAGIC]]\\E", ChatColor.MAGIC.toString());
input = input.replaceAll("\\Q[[RESET]]\\E", ChatColor.RESET.toString());
2012-04-27 11:47:11 +02:00
return input;
}
}