More work on translations

This commit is contained in:
fullwall 2012-09-16 19:20:01 +08:00
parent 0baac5d002
commit 6b2b11b90d
5 changed files with 82 additions and 20 deletions

View File

@ -3,6 +3,7 @@ package net.citizensnpcs;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import java.util.Locale;
import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
@ -36,8 +37,10 @@ import net.citizensnpcs.editor.Editor;
import net.citizensnpcs.npc.CitizensNPCRegistry; import net.citizensnpcs.npc.CitizensNPCRegistry;
import net.citizensnpcs.npc.CitizensTraitFactory; import net.citizensnpcs.npc.CitizensTraitFactory;
import net.citizensnpcs.npc.NPCSelector; import net.citizensnpcs.npc.NPCSelector;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.StringHelper; import net.citizensnpcs.util.StringHelper;
import net.citizensnpcs.util.Translator;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -199,7 +202,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
config = new Settings(getDataFolder()); config = new Settings(getDataFolder());
saves = NPCDataStore.create(getDataFolder()); saves = NPCDataStore.create(getDataFolder());
if (saves == null) { if (saves == null) {
Messaging.severeF("Unable to load saves, disabling..."); Messaging.severeTr(Messages.FAILED_LOAD_SAVES);
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
return; return;
} }
@ -211,12 +214,11 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
getServer().getPluginManager().registerEvents(new EventListen(), this); getServer().getPluginManager().registerEvents(new EventListen(), this);
if (Setting.NPC_COST.asDouble() > 0) { if (Setting.NPC_COST.asDouble() > 0)
setupEconomy(); setupEconomy();
}
registerCommands(); registerCommands();
setupTranslator();
Messaging.logF("v%s enabled.", getDescription().getVersion()); Messaging.logF("v%s enabled.", getDescription().getVersion());
// Setup NPCs after all plugins have been enabled (allows for multiworld // Setup NPCs after all plugins have been enabled (allows for multiworld
@ -238,7 +240,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
@Override @Override
public void onImplementationChanged() { public void onImplementationChanged() {
Messaging.severe("Citizens implementation changed, disabling plugin."); Messaging.severeTr(Messages.CITIZENS_IMPLEMENTATION_DISABLED);
Bukkit.getPluginManager().disablePlugin(this); Bukkit.getPluginManager().disablePlugin(this);
} }
@ -303,6 +305,26 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
// allows all plugin classes to be imported. // allows all plugin classes to be imported.
} }
private void setupTranslator() {
Locale locale = Locale.getDefault();
String[] parts = Setting.LOCALE.asString().split("[\\._]");
switch (parts.length) {
case 1:
locale = new Locale(parts[0]);
break;
case 2:
locale = new Locale(parts[0], parts[1]);
break;
case 3:
locale = new Locale(parts[0], parts[1], parts[2]);
break;
default:
break;
}
Translator.setInstance(new File(getDataFolder(), "i18n"), locale);
Messaging.logF("Using locale %s.", locale);
}
private void startMetrics() { private void startMetrics() {
new Thread() { new Thread() {
@Override @Override

View File

@ -69,6 +69,7 @@ public class Settings {
value = list; value = list;
} }
}, },
LOCALE("general.translation.locale", "en"),
MAX_NPC_LIMIT_CHECKS("npc.limits.max-permission-checks", 100), MAX_NPC_LIMIT_CHECKS("npc.limits.max-permission-checks", 100),
NPC_COST("economy.npc.cost", 100D), NPC_COST("economy.npc.cost", 100D),
QUICK_SELECT("npc.selection.quick-select", false), QUICK_SELECT("npc.selection.quick-select", false),

View File

@ -3,40 +3,45 @@ package net.citizensnpcs.util;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ListResourceBundle; import java.util.ListResourceBundle;
import java.util.Properties; import java.util.Properties;
import java.util.PropertyResourceBundle; import java.util.PropertyResourceBundle;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import net.citizensnpcs.api.CitizensAPI;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
public enum Messages { public enum Messages {
; CITIZENS_IMPLEMENTATION_DISABLED("citizens.changed-implementation",
"Citizens implementation changed, disabling plugin."),
FAILED_LOAD_SAVES("citizens.saves.load-failed", "Unable to load saves, disabling...");
private String defaultTranslation; private String defaultTranslation;
private String key; private String key;
Messages(String key, String defaultTranslation) { Messages(String key, String defaultTranslation) {
this.key = key; this.key = key;
this.defaultTranslation = defaultTranslation; this.defaultTranslation = defaultTranslation;
} }
public String getKey() { public String getKey() {
return key; return key;
} }
private static ResourceBundle defaultBundle; private static ResourceBundle defaultBundle;
public static ResourceBundle getDefaultResourceBundle() { public static ResourceBundle getDefaultResourceBundle(File resourceDirectory, String fileName) {
if (defaultBundle == null) { if (defaultBundle == null) {
File dir = new File(CitizensAPI.getDataFolder(), "i18n"); resourceDirectory.mkdirs();
dir.mkdirs();
File bundleFile = new File(dir, Translator.PREFIX + "_en.properties"); File bundleFile = new File(resourceDirectory, fileName);
if (!bundleFile.exists()) if (!bundleFile.exists())
populateDefaults(bundleFile); try {
bundleFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
populateDefaults(bundleFile);
FileInputStream stream = null; FileInputStream stream = null;
try { try {
stream = new FileInputStream(bundleFile); stream = new FileInputStream(bundleFile);
@ -68,8 +73,17 @@ public enum Messages {
private static void populateDefaults(File bundleFile) { private static void populateDefaults(File bundleFile) {
Properties properties = new Properties(); Properties properties = new Properties();
FileInputStream in = null;
try {
in = new FileInputStream(bundleFile);
properties.load(in);
} catch (IOException e) {
} finally {
Closeables.closeQuietly(in);
}
for (Messages message : values()) { for (Messages message : values()) {
properties.put(message.key, message.defaultTranslation); if (!properties.containsKey(message.key))
properties.put(message.key, message.defaultTranslation);
} }
OutputStream stream = null; OutputStream stream = null;
try { try {

View File

@ -46,6 +46,14 @@ public class Messaging {
log(getFormatted(msg)); log(getFormatted(msg));
} }
public static void logTr(Messages key) {
log(Level.INFO, Translator.tr(key));
}
public static void logTr(Messages key, Object... msg) {
log(Level.INFO, Translator.tr(key, msg));
}
public static void send(CommandSender sender, Object... msg) { public static void send(CommandSender sender, Object... msg) {
sendMessageTo(sender, SPACE.join(msg)); sendMessageTo(sender, SPACE.join(msg));
} }
@ -67,11 +75,11 @@ public class Messaging {
sender.sendMessage(msg); sender.sendMessage(msg);
} }
public static void sendTr(CommandSender sender, String key) { public static void sendTr(CommandSender sender, Messages key) {
sendMessageTo(sender, Translator.tr(key)); sendMessageTo(sender, Translator.tr(key));
} }
public static void sendTr(CommandSender sender, String key, Object... msg) { public static void sendTr(CommandSender sender, Messages key, Object... msg) {
sendMessageTo(sender, Translator.tr(key, msg)); sendMessageTo(sender, Translator.tr(key, msg));
} }
@ -97,4 +105,8 @@ public class Messaging {
public static void severeF(Object... messages) { public static void severeF(Object... messages) {
log(Level.SEVERE, getFormatted(messages)); log(Level.SEVERE, getFormatted(messages));
} }
public static void severeTr(Messages key) {
log(Level.SEVERE, Translator.tr(key));
}
} }

View File

@ -17,15 +17,16 @@ import com.google.common.collect.Maps;
public class Translator { public class Translator {
private ResourceBundle bundle; private ResourceBundle bundle;
private final Map<String, MessageFormat> messageFormatCache = Maps.newHashMap(); private final Map<String, MessageFormat> messageFormatCache = Maps.newHashMap();
private final File resourceFile;
public Translator(File resourceFile, Locale locale) { private Translator(File resourceFile, Locale locale) {
this.resourceFile = resourceFile;
try { try {
bundle = ResourceBundle.getBundle(PREFIX, locale, bundle = ResourceBundle.getBundle(PREFIX, locale,
new FileClassLoader(Translator.class.getClassLoader(), resourceFile)); new FileClassLoader(Translator.class.getClassLoader(), resourceFile));
} catch (MissingResourceException e) { } catch (MissingResourceException e) {
bundle = getDefaultBundle(); bundle = getDefaultBundle();
} }
instance = this;
} }
public String format(String key, Object... msg) { public String format(String key, Object... msg) {
@ -35,7 +36,7 @@ public class Translator {
} }
private ResourceBundle getDefaultBundle() { private ResourceBundle getDefaultBundle() {
return Messages.getDefaultResourceBundle(); return Messages.getDefaultResourceBundle(resourceFile, PREFIX + "_en.properties");
} }
private MessageFormat getFormatter(String unreplaced) { private MessageFormat getFormatter(String unreplaced) {
@ -91,7 +92,19 @@ public class Translator {
} }
private static Translator instance; private static Translator instance;
public static final String PREFIX = "messages"; public static final String PREFIX = "messages";
public static void setInstance(File resourceFile, Locale locale) {
instance = new Translator(resourceFile, locale);
}
public static String tr(Messages key) {
return tr(key.getKey());
}
public static String tr(Messages key, Object... msg) {
return tr(key.getKey(), msg);
}
public static String tr(String key, Object... msg) { public static String tr(String key, Object... msg) {
return msg.length == 0 ? instance.translate(key) : instance.format(key, msg); return msg.length == 0 ? instance.translate(key) : instance.format(key, msg);