Begin work on translation

This commit is contained in:
fullwall 2012-09-16 16:47:41 +08:00
parent 7610f03bb1
commit eb0943ad0d
3 changed files with 190 additions and 0 deletions

View File

@ -0,0 +1,83 @@
package net.citizensnpcs.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ListResourceBundle;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import net.citizensnpcs.api.CitizensAPI;
import com.google.common.io.Closeables;
public enum Messages {
;
private String defaultTranslation;
private String key;
Messages(String key, String defaultTranslation) {
this.key = key;
this.defaultTranslation = defaultTranslation;
}
public String getKey() {
return key;
}
private static ResourceBundle defaultBundle;
public static ResourceBundle getDefaultResourceBundle() {
if (defaultBundle == null) {
File dir = new File(CitizensAPI.getDataFolder(), "i18n");
dir.mkdirs();
File bundleFile = new File(dir, Translator.PREFIX + "_en.properties");
if (!bundleFile.exists())
populateDefaults(bundleFile);
FileInputStream stream = null;
try {
stream = new FileInputStream(bundleFile);
defaultBundle = new PropertyResourceBundle(stream);
} catch (Exception e) {
e.printStackTrace();
defaultBundle = getFallbackResourceBundle();
} finally {
Closeables.closeQuietly(stream);
}
}
return defaultBundle;
}
private static ResourceBundle getFallbackResourceBundle() {
return new ListResourceBundle() {
@Override
protected Object[][] getContents() {
Messages[] values = values();
Object[][] contents = new Object[values.length][2];
for (int i = 0; i < values.length; i++) {
Messages message = values[i];
contents[i] = new Object[] { message.key, message.defaultTranslation };
}
return contents;
}
};
}
private static void populateDefaults(File bundleFile) {
Properties properties = new Properties();
for (Messages message : values()) {
properties.put(message.key, message.defaultTranslation);
}
OutputStream stream = null;
try {
properties.store(stream = new FileOutputStream(bundleFile), "");
} catch (Exception e) {
e.printStackTrace();
} finally {
Closeables.closeQuietly(stream);
}
}
}

View File

@ -67,6 +67,14 @@ public class Messaging {
sender.sendMessage(msg);
}
public static void sendTr(CommandSender sender, String key) {
sendMessageTo(sender, Translator.tr(key));
}
public static void sendTr(CommandSender sender, String key, Object... msg) {
sendMessageTo(sender, Translator.tr(key, msg));
}
public static void sendWithNPC(CommandSender sender, Object msg, NPC npc) {
String send = msg.toString();

View File

@ -0,0 +1,99 @@
package net.citizensnpcs.util;
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;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import com.google.common.collect.Maps;
public class Translator {
private ResourceBundle bundle;
private final Map<String, MessageFormat> messageFormatCache = Maps.newHashMap();
public Translator(File resourceFile, Locale locale) {
try {
bundle = ResourceBundle.getBundle(PREFIX, locale,
new FileClassLoader(Translator.class.getClassLoader(), resourceFile));
} catch (MissingResourceException e) {
bundle = getDefaultBundle();
}
instance = this;
}
public String format(String key, Object... msg) {
String unreplaced = translate(key);
MessageFormat formatter = getFormatter(unreplaced);
return formatter.format(msg);
}
private ResourceBundle getDefaultBundle() {
return Messages.getDefaultResourceBundle();
}
private MessageFormat getFormatter(String unreplaced) {
MessageFormat formatter = messageFormatCache.get(unreplaced);
if (formatter == null)
messageFormatCache.put(unreplaced, formatter = new MessageFormat(unreplaced));
return formatter;
}
public String translate(String key) {
try {
return bundle.getString(key);
} catch (MissingResourceException e) {
try {
return getDefaultBundle().getString(key);
} catch (MissingResourceException ex) {
return "?" + key + "?";
}
}
}
private static class FileClassLoader extends ClassLoader {
private final File folder;
public FileClassLoader(ClassLoader classLoader, File folder) {
super(classLoader);
this.folder = folder;
}
@Override
public URL getResource(String string) {
File file = new File(folder, string);
if (file.exists()) {
try {
return file.toURI().toURL();
} catch (MalformedURLException ex) {
}
}
return super.getResource(string);
}
@Override
public InputStream getResourceAsStream(String string) {
File file = new File(folder, string);
if (file.exists()) {
try {
return new FileInputStream(file);
} catch (FileNotFoundException ex) {
}
}
return super.getResourceAsStream(string);
}
}
private static Translator instance;
public static final String PREFIX = "messages";
public static String tr(String key, Object... msg) {
return msg.length == 0 ? instance.translate(key) : instance.format(key, msg);
}
}