2012-04-27 11:47:11 +02:00
|
|
|
package com.gmail.nossr50.locale;
|
|
|
|
|
2019-01-15 07:11:58 +01:00
|
|
|
import com.gmail.nossr50.config.Config;
|
|
|
|
import com.gmail.nossr50.mcMMO;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
|
2019-04-29 00:25:00 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
2012-04-27 11:47:11 +02:00
|
|
|
import java.text.MessageFormat;
|
2019-05-18 17:12:32 +02:00
|
|
|
import java.util.*;
|
2019-04-29 00:25:00 +02:00
|
|
|
import java.util.logging.Level;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
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";
|
2019-04-30 16:58:33 +02:00
|
|
|
private static Map<String, String> bundleCache = new HashMap<>();
|
2013-02-06 07:05:09 +01:00
|
|
|
private static ResourceBundle bundle = null;
|
2019-04-29 19:58:54 +02:00
|
|
|
private static ResourceBundle filesystemBundle = null;
|
2013-02-15 05:27:03 +01:00
|
|
|
private static ResourceBundle enBundle = null;
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2020-07-13 20:39:03 +02:00
|
|
|
private LocaleLoader() {}
|
2013-01-26 23:01:55 +01:00
|
|
|
|
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-03-01 06:52:01 +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
|
|
|
|
2019-04-30 16:58:33 +02:00
|
|
|
String rawMessage = bundleCache.computeIfAbsent(key, LocaleLoader::getRawString);
|
|
|
|
return formatString(rawMessage, messageArguments);
|
|
|
|
}
|
|
|
|
|
2019-04-30 20:19:55 +02:00
|
|
|
/**
|
|
|
|
* Reloads locale
|
|
|
|
*/
|
|
|
|
public static void reloadLocale() {
|
|
|
|
bundle = null;
|
|
|
|
filesystemBundle = null;
|
|
|
|
enBundle = null;
|
|
|
|
bundleCache = new HashMap<>(); // Cheaper to replace than clear()
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
2019-04-30 16:58:33 +02:00
|
|
|
private static String getRawString(String key) {
|
2019-04-29 19:58:54 +02:00
|
|
|
if (filesystemBundle != null) {
|
|
|
|
try {
|
2019-04-30 16:58:33 +02:00
|
|
|
return filesystemBundle.getString(key);
|
2019-04-29 19:58:54 +02:00
|
|
|
}
|
|
|
|
catch (MissingResourceException ignored) {}
|
|
|
|
}
|
|
|
|
|
2013-02-06 07:05:09 +01:00
|
|
|
try {
|
2019-04-30 16:58:33 +02:00
|
|
|
return bundle.getString(key);
|
2013-02-15 05:27:03 +01:00
|
|
|
}
|
2019-04-30 16:58:33 +02:00
|
|
|
catch (MissingResourceException ignored) {}
|
2013-10-25 22:16:18 +02:00
|
|
|
|
2019-04-30 16:58:33 +02:00
|
|
|
try {
|
|
|
|
return enBundle.getString(key);
|
2013-02-15 05:27:03 +01:00
|
|
|
}
|
2019-04-30 16:58:33 +02:00
|
|
|
catch (MissingResourceException ignored) {
|
|
|
|
if (!key.contains("Guides")) {
|
|
|
|
mcMMO.p.getLogger().warning("Could not find locale string: " + key);
|
|
|
|
}
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2019-04-30 16:58:33 +02:00
|
|
|
return '!' + key + '!';
|
|
|
|
}
|
2013-08-17 12:18:29 +02:00
|
|
|
}
|
2012-04-27 11:47:11 +02:00
|
|
|
|
2013-08-17 12:18:29 +02:00
|
|
|
public static String formatString(String string, Object... messageArguments) {
|
2013-02-15 05:27:03 +01:00
|
|
|
if (messageArguments != null) {
|
|
|
|
MessageFormat formatter = new MessageFormat("");
|
2015-04-25 23:59:33 +02:00
|
|
|
formatter.applyPattern(string.replace("'", "''"));
|
2013-08-17 12:18:29 +02:00
|
|
|
string = formatter.format(messageArguments);
|
2012-04-27 11:47:11 +02:00
|
|
|
}
|
2013-02-15 05:27:03 +01:00
|
|
|
|
2013-08-17 12:18:29 +02:00
|
|
|
string = addColors(string);
|
2013-02-15 05:27:03 +01:00
|
|
|
|
2013-08-17 12:18:29 +02:00
|
|
|
return string;
|
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]);
|
|
|
|
}
|
|
|
|
|
2019-04-29 00:25:00 +02:00
|
|
|
if (locale == null) {
|
|
|
|
throw new IllegalStateException("Failed to parse locale string '" + Config.getInstance().getLocale() + "'");
|
|
|
|
}
|
|
|
|
|
2019-04-30 08:31:33 +02:00
|
|
|
Path localePath = Paths.get(mcMMO.getLocalesDirectory() + "locale_" + locale.toString() + ".properties");
|
2019-04-29 00:25:00 +02:00
|
|
|
if (Files.exists(localePath) && Files.isRegularFile(localePath)) {
|
|
|
|
try (Reader localeReader = Files.newBufferedReader(localePath)) {
|
2019-04-30 19:14:42 +02:00
|
|
|
mcMMO.p.getLogger().log(Level.INFO, "Loading locale from {0}", localePath);
|
2019-04-29 19:58:54 +02:00
|
|
|
filesystemBundle = new PropertyResourceBundle(localeReader);
|
2019-04-29 00:25:00 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
mcMMO.p.getLogger().log(Level.WARNING, "Failed to load locale from " + localePath, e);
|
|
|
|
}
|
|
|
|
}
|
2019-04-29 19:58:54 +02:00
|
|
|
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());
|
2013-01-18 19:59:20 +01:00
|
|
|
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
|
|
|
|
2019-01-21 16:40:04 +01:00
|
|
|
input = input.replaceAll("\\Q&0\\E", ChatColor.BLACK.toString());
|
|
|
|
input = input.replaceAll("\\Q&1\\E", ChatColor.DARK_BLUE.toString());
|
|
|
|
input = input.replaceAll("\\Q&2\\E", ChatColor.DARK_GREEN.toString());
|
|
|
|
input = input.replaceAll("\\Q&3\\E", ChatColor.DARK_AQUA.toString());
|
|
|
|
input = input.replaceAll("\\Q&4\\E", ChatColor.DARK_RED.toString());
|
|
|
|
input = input.replaceAll("\\Q&5\\E", ChatColor.DARK_PURPLE.toString());
|
|
|
|
input = input.replaceAll("\\Q&6\\E", ChatColor.GOLD.toString());
|
|
|
|
input = input.replaceAll("\\Q&7\\E", ChatColor.GRAY.toString());
|
|
|
|
input = input.replaceAll("\\Q&8\\E", ChatColor.DARK_GRAY.toString());
|
|
|
|
input = input.replaceAll("\\Q&9\\E", ChatColor.BLUE.toString());
|
|
|
|
input = input.replaceAll("\\Q&a\\E", ChatColor.GREEN.toString());
|
|
|
|
input = input.replaceAll("\\Q&b\\E", ChatColor.AQUA.toString());
|
|
|
|
input = input.replaceAll("\\Q&c\\E", ChatColor.RED.toString());
|
|
|
|
input = input.replaceAll("\\Q&d\\E", ChatColor.LIGHT_PURPLE.toString());
|
|
|
|
input = input.replaceAll("\\Q&e\\E", ChatColor.YELLOW.toString());
|
|
|
|
input = input.replaceAll("\\Q&f\\E", ChatColor.WHITE.toString());
|
|
|
|
input = input.replaceAll("\\Q&l\\E", ChatColor.BOLD.toString());
|
|
|
|
input = input.replaceAll("\\Q&n\\E", ChatColor.UNDERLINE.toString());
|
|
|
|
input = input.replaceAll("\\Q&o\\E", ChatColor.ITALIC.toString());
|
|
|
|
input = input.replaceAll("\\Q&m\\E", ChatColor.STRIKETHROUGH.toString());
|
|
|
|
input = input.replaceAll("\\Q&?\\E", ChatColor.MAGIC.toString());
|
|
|
|
input = input.replaceAll("\\Q&r\\E", ChatColor.RESET.toString());
|
|
|
|
|
2012-04-27 11:47:11 +02:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
}
|