diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java index 58dfbcee5..112561a05 100644 --- a/Essentials/src/com/earth2me/essentials/Settings.java +++ b/Essentials/src/com/earth2me/essentials/Settings.java @@ -496,6 +496,6 @@ public class Settings implements IConf public String getLocale() { - return config.getString("locale", "en_US"); + return config.getString("locale", ""); } } diff --git a/Essentials/src/com/earth2me/essentials/Util.java b/Essentials/src/com/earth2me/essentials/Util.java index 79b194440..ed1150eb0 100644 --- a/Essentials/src/com/earth2me/essentials/Util.java +++ b/Essentials/src/com/earth2me/essentials/Util.java @@ -1,8 +1,21 @@ package com.earth2me.essentials; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; import java.text.DecimalFormat; +import java.text.MessageFormat; import java.util.Calendar; +import java.util.Enumeration; import java.util.GregorianCalendar; +import java.util.Locale; +import java.util.ResourceBundle; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.bukkit.Location; @@ -268,4 +281,114 @@ public class Util { return Math.round(d*100.0)/100.0; } + + private static class ConfigClassLoader extends ClassLoader { + private File dataFolder; + private ClassLoader cl; + + public ConfigClassLoader(File dataFolder, ClassLoader cl) + { + this.dataFolder = dataFolder; + this.cl = cl; + } + + @Override + public URL getResource(String string) + { + File file = new File(dataFolder, string); + if (file.exists()) + { + try + { + return file.toURI().toURL(); + } + catch (MalformedURLException ex) + { + return cl.getResource(string); + } + } + return cl.getResource(string); + } + + @Override + public synchronized void clearAssertionStatus() + { + cl.clearAssertionStatus(); + } + + @Override + public InputStream getResourceAsStream(String string) + { + File file = new File(dataFolder, string); + if (file.exists()) + { + try + { + return new FileInputStream(file); + } + catch (FileNotFoundException ex) + { + return cl.getResourceAsStream(string); + } + } + return cl.getResourceAsStream(string); + } + + @Override + public Enumeration getResources(String string) throws IOException + { + return cl.getResources(string); + } + + @Override + public Class loadClass(String string) throws ClassNotFoundException + { + return cl.loadClass(string); + } + + @Override + public synchronized void setClassAssertionStatus(String string, boolean bln) + { + cl.setClassAssertionStatus(string, bln); + } + + @Override + public synchronized void setDefaultAssertionStatus(boolean bln) + { + cl.setDefaultAssertionStatus(bln); + } + + @Override + public synchronized void setPackageAssertionStatus(String string, boolean bln) + { + cl.setPackageAssertionStatus(string, bln); + } + } + + private static final Locale defaultLocale = Locale.getDefault(); + public static Locale currentLocale = defaultLocale; + private static ResourceBundle bundle = ResourceBundle.getBundle("messages", defaultLocale); + + public static String i18n(String string) { + return bundle.getString(string); + } + + public static String format(String string, Object... objects) { + MessageFormat mf = new MessageFormat(i18n(string)); + return mf.format(objects); + } + + public static void updateLocale(String loc, File dataFolder) { + if (loc == null || loc.isEmpty()) { + return; + } + String[] parts = loc.split("_"); + if (parts.length == 1) { + currentLocale = new Locale(parts[0]); + } + if (parts.length == 2) { + currentLocale = new Locale(parts[0], parts[1]); + } + bundle = ResourceBundle.getBundle("messages", currentLocale, new ConfigClassLoader(dataFolder, Util.class.getClassLoader())); + } } diff --git a/Essentials/src/config.yml b/Essentials/src/config.yml index 5abd44543..a79f05ff6 100644 --- a/Essentials/src/config.yml +++ b/Essentials/src/config.yml @@ -185,6 +185,10 @@ sort-list-by-groups: false # More output to the console debug: false +# Set the locale for all messages +# If you don't set this, the default locale of the server will be used. +#locale: de_DE + ############################################################ # +------------------------------------------------------+ # # | EssentialsHome | #