Added args for String.format()

( cherry-picked by @main-- )
This commit is contained in:
dumptruckman 2012-01-02 14:22:32 -05:00 committed by main()
parent 451955f858
commit 3e4b042de8
2 changed files with 16 additions and 7 deletions

View File

@ -17,18 +17,20 @@ public interface MessageProvider {
* Returns a message (as {@link String}) for the specified key (as {@link MultiverseMessage}).
*
* @param key The key
* @param args Args for String.format()
* @return The message
*/
public String getMessage(MultiverseMessage key);
public String getMessage(MultiverseMessage key, Object... args);
/**
* Returns a message (as {@link String}) in a specified {@link Locale} for the specified key (as {@link MultiverseMessage}).
*
* @param key The Key
* @param locale The {@link Locale}
* @param args Args for String.format()
* @return The message
*/
public String getMessage(MultiverseMessage key, Locale locale);
public String getMessage(MultiverseMessage key, Locale locale, Object... args);
/**
* Returns the Locale this MessageProvider is currently using.

View File

@ -46,6 +46,13 @@ public class SimpleMessageProvider implements LazyLocaleMessageProvider {
+ locale.toString(), locale);
}
public String format(String string, Object... args) {
// Replaces & with the Section character
string = string.replaceAll("&", Character.toString((char) 167));
return String.format(string, args);
}
/**
* {@inheritDoc}
*/
@ -106,26 +113,26 @@ public class SimpleMessageProvider implements LazyLocaleMessageProvider {
* {@inheritDoc}
*/
@Override
public String getMessage(MultiverseMessage key) {
public String getMessage(MultiverseMessage key, Object... args) {
if (!isLocaleLoaded(locale)) {
return key.getDefault();
return format(key.getDefault(), args);
}
else
return messages.get(locale).get(key);
return format(messages.get(locale).get(key), args);
}
/**
* {@inheritDoc}
*/
@Override
public String getMessage(MultiverseMessage key, Locale locale) {
public String getMessage(MultiverseMessage key, Locale locale, Object... args) {
try {
maybeLoadLocale(locale);
} catch (LocalizationLoadingException e) {
e.printStackTrace();
return getMessage(key);
}
return messages.get(locale).get(key);
return format(messages.get(locale).get(key), args);
}
/**