Fix locale selection, unpack resource translations automatically

This commit is contained in:
fullwall 2012-10-04 18:58:27 +08:00
parent 58ee3dfbc2
commit 236b6e52c3
4 changed files with 43 additions and 6 deletions

View File

@ -199,10 +199,10 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
getServer().getPluginManager().disablePlugin(this);
return;
}
config = new Settings(getDataFolder());
setupTranslator();
registerScriptHelpers();
config = new Settings(getDataFolder());
saves = NPCDataStore.create(getDataFolder());
if (saves == null) {
Messaging.severeTr(Messages.FAILED_LOAD_SAVES);

View File

@ -7,8 +7,6 @@ import java.util.List;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Storage;
import net.citizensnpcs.api.util.YamlStorage;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Messaging;
import com.google.common.collect.Lists;
@ -23,7 +21,6 @@ public class Settings {
config.load();
for (Setting setting : Setting.values()) {
if (!root.keyExists(setting.path)) {
Messaging.logTr(Messages.WRITING_DEFAULT_SETTING, setting.path);
setting.setAtKey(root);
} else
setting.loadFromKey(root);

View File

@ -3,6 +3,7 @@ package net.citizensnpcs.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
@ -13,6 +14,8 @@ import java.util.MissingResourceException;
import java.util.ResourceBundle;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
public class Translator {
private final Locale defaultLocale;
@ -110,13 +113,50 @@ public class Translator {
} else {
string = string.replaceFirst("/", "");
InputStream stream = Translator.class.getResourceAsStream('/' + string);
if (stream != null)
if (stream != null) {
new Thread(new SaveResourceThread(folder, string)).start();
return stream;
}
}
return super.getResourceAsStream(string);
}
}
private static class SaveResourceThread implements Runnable {
private final String fileName;
private final File rootFolder;
private SaveResourceThread(File rootFolder, String fileName) {
this.rootFolder = rootFolder;
this.fileName = fileName;
}
@Override
public void run() {
File file = new File(rootFolder, fileName);
if (file.exists())
return;
final InputStream stream = Translator.class.getResourceAsStream('/' + fileName);
if (stream == null)
return;
InputSupplier<InputStream> in = new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return stream;
}
};
try {
File to = File.createTempFile(fileName, null, rootFolder);
to.deleteOnExit();
Files.copy(in, to);
if (!file.exists())
to.renameTo(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static Translator instance;
public static final String PREFIX = "messages";

View File

@ -7,7 +7,6 @@ citizens.commands.invalid-number=That is not a valid number.
citizens.commands.npc.age.cannot-be-aged=The mob type '{0}' cannot be aged.
citizens.commands.npc.age.invalid-age=Invalid age. Valid ages are adult, baby, number between -24000 and 0
citizens.commands.npc.age.set=[[{0}]] is now [[{1}]].
citizens.notifications.missing-translations=Missing translations file for locale {0}. Defaulting to en locale.
citizens.commands.npc.behaviour.added=Behaviours added.
citizens.commands.npc.behaviour.removed=Behaviours removed.
citizens.commands.npc.controllable.not-controllable=[[{0}]] is not controllable.
@ -115,6 +114,7 @@ citizens.notifications.incompatible-version=v{0} is not compatible with Minecraf
citizens.notifications.locale=Using locale {0}.
citizens.notifications.metrics-load-error=Unable to start metrics: {0}.
citizens.notifications.metrics-started=Metrics started.
citizens.notifications.missing-translations=Missing translations file for locale {0}. Defaulting to en locale.
citizens.notifications.npc-name-not-found=Could not find a name for ID '{0}'.
citizens.notifications.npc-not-found=No NPC could be found.
citizens.notifications.npcs-loaded=Loaded {0} NPCs ({1} spawned).