Allow for locale specialisation for possible player-specific translations

This commit is contained in:
fullwall 2012-10-04 17:28:42 +08:00
parent ae31334a72
commit 6c7dd30c3b
2 changed files with 29 additions and 9 deletions

View File

@ -46,6 +46,7 @@ public class Messaging {
public static void sendError(CommandSender sender, Object... msg) { public static void sendError(CommandSender sender, Object... msg) {
send(sender, ChatColor.RED.toString() + SPACE.join(msg)); send(sender, ChatColor.RED.toString() + SPACE.join(msg));
} }
public static void sendErrorTr(CommandSender sender, String key, Object... msg) { public static void sendErrorTr(CommandSender sender, String key, Object... msg) {
sendMessageTo(sender, ChatColor.RED + Translator.tr(key, msg)); sendMessageTo(sender, ChatColor.RED + Translator.tr(key, msg));
} }

View File

@ -15,26 +15,38 @@ import java.util.ResourceBundle;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
public class Translator { public class Translator {
private ResourceBundle bundle; private final Locale defaultLocale;
private final Map<String, MessageFormat> messageFormatCache = Maps.newHashMap(); private final Map<String, MessageFormat> messageFormatCache = Maps.newHashMap();
private ResourceBundle preferredBundle;
private final File resourceFile; private final File resourceFile;
private Translator(File resourceFile, Locale locale) { private Translator(File resourceFile, Locale locale) {
this.resourceFile = resourceFile; this.resourceFile = resourceFile;
this.defaultLocale = locale;
try { try {
bundle = ResourceBundle.getBundle(PREFIX, locale, preferredBundle = ResourceBundle.getBundle(PREFIX, defaultLocale, new FileClassLoader(
new FileClassLoader(Translator.class.getClassLoader(), resourceFile)); Translator.class.getClassLoader(), resourceFile));
} catch (MissingResourceException e) { } catch (MissingResourceException e) {
bundle = getDefaultBundle(); preferredBundle = getDefaultBundle();
} }
} }
public String format(String key, Object... msg) { private String format(String key, Locale locale, Object... msg) {
String unreplaced = translate(key); String unreplaced = translate(key, locale);
MessageFormat formatter = getFormatter(unreplaced); MessageFormat formatter = getFormatter(unreplaced);
return formatter.format(msg); return formatter.format(msg);
} }
private ResourceBundle getBundle(Locale locale) {
try {
ResourceBundle bundle = ResourceBundle.getBundle(PREFIX, locale, new FileClassLoader(
Translator.class.getClassLoader(), resourceFile));
return bundle == null ? preferredBundle : bundle;
} catch (MissingResourceException e) {
return preferredBundle;
}
}
private ResourceBundle getDefaultBundle() { private ResourceBundle getDefaultBundle() {
return Messages.getDefaultResourceBundle(resourceFile, PREFIX + "_en.properties"); return Messages.getDefaultResourceBundle(resourceFile, PREFIX + "_en.properties");
} }
@ -46,7 +58,10 @@ public class Translator {
return formatter; return formatter;
} }
public String translate(String key) { private String translate(String key, Locale locale) {
ResourceBundle bundle = preferredBundle;
if (locale != defaultLocale)
bundle = getBundle(locale);
try { try {
return bundle.getString(key); return bundle.getString(key);
} catch (MissingResourceException e) { } catch (MissingResourceException e) {
@ -98,8 +113,12 @@ public class Translator {
instance = new Translator(resourceFile, locale); instance = new Translator(resourceFile, locale);
} }
static String tr(String key, Locale preferredLocale, Object... msg) {
return StringHelper.parseColors(msg.length == 0 ? instance.translate(key, preferredLocale) : instance
.format(key, preferredLocale, msg));
}
static String tr(String key, Object... msg) { static String tr(String key, Object... msg) {
return StringHelper return tr(key, instance.defaultLocale, msg);
.parseColors(msg.length == 0 ? instance.translate(key) : instance.format(key, msg));
} }
} }