Makes translator color conversion configurable
All checks were successful
EpicKnarvik97/KnarLib/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2023-01-25 16:38:56 +01:00
parent f1ccee13c2
commit 5e8b13785d

View File

@ -31,6 +31,7 @@ public final class Translator {
private final List<TranslatableMessage> messageCategories = new ArrayList<>();
private Map<TranslatableMessage, String> translatedMessages;
private Map<TranslatableMessage, String> backupTranslatedMessages;
private ColorConversion colorConversion = ColorConversion.RGB;
/**
* Registers all translatable messages in the given message category
@ -44,6 +45,23 @@ public final class Translator {
messageCategories.add(translatableMessage);
}
/**
* Sets the color conversion to use for translated messages
*
* <p>See {@link net.knarcraft.knarlib.property.ColorConversion} and
* {@link net.knarcraft.knarlib.util.ColorHelper#translateColorCodes(String, ColorConversion)} for details, but
* basically, this allows setting which color codes should be converted, and which should not be.</p>
*
* @param colorConversion <p>The color conversion to be used</p>
*/
public void setColorConversion(ColorConversion colorConversion) {
if (colorConversion != null) {
this.colorConversion = colorConversion;
} else {
throw new IllegalArgumentException("Color conversion cannot be null");
}
}
/**
* Loads the languages used by this translator
*
@ -77,7 +95,7 @@ public final class Translator {
} else {
translatedMessage = translatableMessage.toString();
}
return ColorHelper.translateColorCodes(translatedMessage, ColorConversion.RGB);
return ColorHelper.translateColorCodes(translatedMessage, colorConversion);
}
/**
@ -115,7 +133,8 @@ public final class Translator {
try {
Bukkit.getLogger().log(Level.INFO, "Loading custom strings...");
return loadTranslatableMessages(language, new BufferedReader(new InputStreamReader(new FileInputStream(strings))));
return loadTranslatableMessages(language, new BufferedReader(new InputStreamReader(
new FileInputStream(strings))));
} catch (FileNotFoundException e) {
Bukkit.getLogger().log(Level.WARNING, "Unable to load custom messages");
return null;