2011-11-18 14:03:14 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2011-12-06 15:32:06 +00:00
|
|
|
import com.earth2me.essentials.api.II18n;
|
2011-11-18 14:03:14 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.text.MessageFormat;
|
2011-11-18 17:42:26 +00:00
|
|
|
import java.util.*;
|
2011-11-18 14:03:14 +00:00
|
|
|
import java.util.logging.Level;
|
2011-11-21 02:05:18 +00:00
|
|
|
import java.util.logging.Logger;
|
2011-11-18 14:03:14 +00:00
|
|
|
|
|
|
|
|
2011-12-06 15:32:06 +00:00
|
|
|
public class I18n implements II18n
|
2011-11-18 14:03:14 +00:00
|
|
|
{
|
|
|
|
private static I18n instance;
|
|
|
|
private static final String MESSAGES = "messages";
|
|
|
|
private final transient Locale defaultLocale = Locale.getDefault();
|
|
|
|
private transient Locale currentLocale = defaultLocale;
|
2011-11-21 01:55:26 +00:00
|
|
|
private transient ResourceBundle customBundle;
|
|
|
|
private transient ResourceBundle localeBundle;
|
|
|
|
private final transient ResourceBundle defaultBundle;
|
2011-11-18 14:03:14 +00:00
|
|
|
private final transient Map<String, MessageFormat> messageFormatCache = new HashMap<String, MessageFormat>();
|
2011-11-21 01:55:26 +00:00
|
|
|
private final transient IEssentials ess;
|
2011-11-18 14:03:14 +00:00
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
public I18n(final IEssentials ess)
|
|
|
|
{
|
|
|
|
this.ess = ess;
|
2011-12-07 10:22:02 +00:00
|
|
|
customBundle = ResourceBundle.getBundle(MESSAGES, defaultLocale, new FileResClassLoader(I18n.class.getClassLoader(), ess));
|
2011-11-21 01:55:26 +00:00
|
|
|
localeBundle = ResourceBundle.getBundle(MESSAGES, defaultLocale);
|
|
|
|
defaultBundle = ResourceBundle.getBundle(MESSAGES, Locale.ENGLISH);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onEnable()
|
2011-11-18 14:03:14 +00:00
|
|
|
{
|
|
|
|
instance = this;
|
|
|
|
}
|
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
public void onDisable()
|
|
|
|
{
|
|
|
|
instance = null;
|
|
|
|
}
|
|
|
|
|
2011-11-18 14:03:14 +00:00
|
|
|
public Locale getCurrentLocale()
|
|
|
|
{
|
|
|
|
return currentLocale;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String translate(final String string)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return customBundle.getString(string);
|
|
|
|
}
|
|
|
|
catch (MissingResourceException ex)
|
|
|
|
{
|
|
|
|
return localeBundle.getString(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (MissingResourceException ex)
|
|
|
|
{
|
2011-11-21 02:05:18 +00:00
|
|
|
Logger.getLogger("Minecraft").log(Level.WARNING, String.format("Missing translation key \"%s\" in translation file %s", ex.getKey(), localeBundle.getLocale().toString()), ex);
|
2011-11-18 14:03:14 +00:00
|
|
|
return defaultBundle.getString(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String _(final String string, final Object... objects)
|
|
|
|
{
|
2012-08-13 01:32:45 +00:00
|
|
|
if (instance == null)
|
|
|
|
{
|
2011-11-29 16:16:45 +00:00
|
|
|
return "";
|
|
|
|
}
|
2011-11-18 14:03:14 +00:00
|
|
|
if (objects.length == 0)
|
|
|
|
{
|
|
|
|
return instance.translate(string);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return instance.format(string, objects);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String format(final String string, final Object... objects)
|
|
|
|
{
|
2012-08-13 01:32:45 +00:00
|
|
|
String format = translate(string);
|
2011-11-18 14:03:14 +00:00
|
|
|
MessageFormat messageFormat = messageFormatCache.get(format);
|
|
|
|
if (messageFormat == null)
|
|
|
|
{
|
2012-08-13 01:32:45 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
messageFormat = new MessageFormat(format);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException e)
|
|
|
|
{
|
|
|
|
ess.getLogger().log(Level.SEVERE, "Invalid Translation key for '" + string + "': " + e.getMessage());
|
|
|
|
format = format.replaceAll("\\{(\\D*?)\\}", "\\[$1\\]");
|
|
|
|
messageFormat = new MessageFormat(format);
|
|
|
|
}
|
2011-11-18 14:03:14 +00:00
|
|
|
messageFormatCache.put(format, messageFormat);
|
|
|
|
}
|
|
|
|
return messageFormat.format(objects);
|
|
|
|
}
|
|
|
|
|
2011-11-21 01:55:26 +00:00
|
|
|
public void updateLocale(final String loc)
|
2011-11-18 14:03:14 +00:00
|
|
|
{
|
|
|
|
if (loc == null || loc.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final String[] parts = loc.split("[_\\.]");
|
|
|
|
if (parts.length == 1)
|
|
|
|
{
|
|
|
|
currentLocale = new Locale(parts[0]);
|
|
|
|
}
|
|
|
|
if (parts.length == 2)
|
|
|
|
{
|
|
|
|
currentLocale = new Locale(parts[0], parts[1]);
|
|
|
|
}
|
|
|
|
if (parts.length == 3)
|
|
|
|
{
|
|
|
|
currentLocale = new Locale(parts[0], parts[1], parts[2]);
|
|
|
|
}
|
2012-03-23 11:10:46 +00:00
|
|
|
ResourceBundle.clearCache();
|
2011-11-21 02:05:18 +00:00
|
|
|
Logger.getLogger("Minecraft").log(Level.INFO, String.format("Using locale %s", currentLocale.toString()));
|
2011-11-18 14:03:14 +00:00
|
|
|
customBundle = ResourceBundle.getBundle(MESSAGES, currentLocale, new FileResClassLoader(I18n.class.getClassLoader(), ess));
|
|
|
|
localeBundle = ResourceBundle.getBundle(MESSAGES, currentLocale);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String capitalCase(final String input)
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
return input == null || input.length() == 0
|
2011-11-18 14:03:14 +00:00
|
|
|
? input
|
|
|
|
: input.toUpperCase(Locale.ENGLISH).charAt(0)
|
|
|
|
+ input.toLowerCase(Locale.ENGLISH).substring(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static class FileResClassLoader extends ClassLoader
|
|
|
|
{
|
|
|
|
private final transient File dataFolder;
|
|
|
|
|
2012-09-24 02:10:54 +00:00
|
|
|
FileResClassLoader(final ClassLoader classLoader, final IEssentials ess)
|
2011-11-18 14:03:14 +00:00
|
|
|
{
|
|
|
|
super(classLoader);
|
|
|
|
this.dataFolder = ess.getDataFolder();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public URL getResource(final String string)
|
|
|
|
{
|
|
|
|
final File file = new File(dataFolder, string);
|
|
|
|
if (file.exists())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return file.toURI().toURL();
|
|
|
|
}
|
|
|
|
catch (MalformedURLException ex)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.getResource(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InputStream getResourceAsStream(final String string)
|
|
|
|
{
|
|
|
|
final File file = new File(dataFolder, string);
|
|
|
|
if (file.exists())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return new FileInputStream(file);
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException ex)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.getResourceAsStream(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|