Skip to content

Instantly share code, notes, and snippets.

@dumptruckman
Last active June 30, 2018 22:17
Show Gist options
  • Save dumptruckman/caa8284ddb93ed132f3c2de4b22a6e08 to your computer and use it in GitHub Desktop.
Save dumptruckman/caa8284ddb93ed132f3c2de4b22a6e08 to your computer and use it in GitHub Desktop.
cmd.set.unknown_prop=''{0}'' is not a valid chest property!
import org.bukkit.ChatColor;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
class SimpleLocalizedMessages {
/*** USAGE ***/
public static final MutableMessageSource MESSAGE_SOURCE = new MutableMessageSource(ResourceBundle.getBundle("Messages", Locale.ENGLISH)) {
@Override
public String getString(CharSequence key) {
return bundle.getString(key.toString());
}
};
public static void main(String[] args) {
// Prints the default english message
System.out.println(Messages.CMD_SET_INVALID_PROP.get("asdf"));
// MESSAGE_SOURCE.bundle = ResourceBundle.getBundle("Messages", Locale.FRENCH);
// // Prints the french message
// System.out.println(Messages.CMD_SET_INVALID_PROP.get("asdf"));
}
public static final class Messages {
public static final LocalizedMessage CMD_SET_INVALID_PROP = new LocalizedMessage(MESSAGE_SOURCE, "cmd.set.unknown_prop");
}
/*** LIBRARY ***/
public interface MessageSource {
String getString(CharSequence key);
}
public static abstract class MutableMessageSource implements MessageSource {
protected ResourceBundle bundle;
public MutableMessageSource(ResourceBundle bundle) {
this.bundle = bundle;
}
}
public static class LocalizedMessage {
private final MessageSource messageSource;
public final String key;
public LocalizedMessage(MessageSource messageSource, String key) {
this.messageSource = messageSource;
this.key = key;
}
public String get(Object... args) {
String message = ChatColor.translateAlternateColorCodes('&', messageSource.getString(key));
return MessageFormat.format(message, args);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment