Move translator creation to the translate methods

This commit is contained in:
fullwall 2012-10-31 10:08:34 +08:00
parent 9a6bdaa170
commit 18d9d412c8
2 changed files with 30 additions and 30 deletions

View File

@ -3,7 +3,6 @@ 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;
@ -43,7 +42,6 @@ import net.citizensnpcs.npc.NPCSelector;
import net.citizensnpcs.util.Messages; 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;
@ -209,7 +207,6 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
return; return;
} }
config = new Settings(getDataFolder()); config = new Settings(getDataFolder());
setupTranslator();
registerScriptHelpers(); registerScriptHelpers();
saves = NPCDataStore.create(getDataFolder()); saves = NPCDataStore.create(getDataFolder());
@ -318,29 +315,6 @@ 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 setting = Setting.LOCALE.asString();
if (!setting.isEmpty()) {
String[] parts = setting.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(), "lang"), locale);
Messaging.logTr(Messages.LOCALE_NOTIFICATION, locale);
}
private void startMetrics() { private void startMetrics() {
try { try {
metrics = new Metrics(Citizens.this); metrics = new Metrics(Citizens.this);

View File

@ -19,6 +19,9 @@ import java.util.Properties;
import java.util.PropertyResourceBundle; import java.util.PropertyResourceBundle;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
import com.google.common.io.Files; import com.google.common.io.Files;
@ -246,16 +249,39 @@ public class Translator {
} }
} }
public static void setInstance(File resourceFile, Locale locale) {
instance = new Translator(resourceFile, locale);
}
public static String translate(String key, Locale preferredLocale, Object... msg) { public static String translate(String key, Locale preferredLocale, Object... msg) {
if (instance == null)
createInstance();
return StringHelper.parseColors(msg.length == 0 ? instance.translate(key, preferredLocale) : instance return StringHelper.parseColors(msg.length == 0 ? instance.translate(key, preferredLocale) : instance
.format(key, preferredLocale, msg)); .format(key, preferredLocale, msg));
} }
public static String translate(String key, Object... msg) { public static String translate(String key, Object... msg) {
if (instance == null)
createInstance();
return translate(key, instance.defaultLocale, msg); return translate(key, instance.defaultLocale, msg);
} }
private static void createInstance() {
Locale locale = Locale.getDefault();
String setting = Setting.LOCALE.asString();
if (!setting.isEmpty()) {
String[] parts = setting.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;
}
}
instance = new Translator(new File(CitizensAPI.getDataFolder(), "lang"), locale);
Messaging.logTr(Messages.LOCALE_NOTIFICATION, locale);
}
} }