Added API to get a translation without color conversion

It may be necessary to read the translation without converting colors to
Bukkit colors.
This commit is contained in:
tastybento 2021-09-25 08:47:14 -07:00
parent 0a4fc83218
commit af3c0a874c
2 changed files with 31 additions and 7 deletions

View File

@ -355,7 +355,7 @@ public class User implements MetaDataAble {
}
/**
* Gets a translation of this reference for this user. Translations may be overridden by Addons
* Gets a translation of this reference for this user with colors converted. Translations may be overridden by Addons
* by using the same reference prefixed by the addon name (from the Addon Description) in lower case.
* @param reference - reference found in a locale file
* @param variables - variables to insert into translated string. Variables go in pairs, for example
@ -363,6 +363,21 @@ public class User implements MetaDataAble {
* @return Translated string with colors converted, or the reference if nothing has been found
*/
public String getTranslation(String reference, String... variables) {
// Get addonPrefix
String addonPrefix = addon == null ? "" : addon.getDescription().getName().toLowerCase(Locale.ENGLISH) + ".";
return Util.translateColorCodes(translate(addonPrefix, reference, variables));
}
/**
* Gets a translation of this reference for this user without colors translated. Translations may be overridden by Addons
* by using the same reference prefixed by the addon name (from the Addon Description) in lower case.
* @param reference - reference found in a locale file
* @param variables - variables to insert into translated string. Variables go in pairs, for example
* "[name]", "tastybento"
* @return Translated string or the reference if nothing has been found
* @since 1.17.4
*/
public String getTranslationNoColor(String reference, String... variables) {
// Get addonPrefix
String addonPrefix = addon == null ? "" : addon.getDescription().getName().toLowerCase(Locale.ENGLISH) + ".";
return translate(addonPrefix, reference, variables);
@ -407,7 +422,7 @@ public class User implements MetaDataAble {
translation = plugin.getPlaceholdersManager().replacePlaceholders(player, translation);
}
return Util.translateColorCodes(translation);
return translation;
}
}

View File

@ -63,7 +63,8 @@ import world.bentobox.bentobox.managers.PlayersManager;
@PrepareForTest({ BentoBox.class, Bukkit.class })
public class UserTest {
private static final String TEST_TRANSLATION = "mock translation [test]";
private static final String TEST_TRANSLATION = "mock &a translation &b [test]";
private static final String TEST_TRANSLATION_WITH_COLOR = "mock §atranslation §b[test]";
@Mock
private Player player;
@Mock
@ -243,7 +244,7 @@ public class UserTest {
@Test
public void testHasPermission() {
// default behaviours
// default behaviors
assertTrue(user.hasPermission(""));
assertTrue(user.hasPermission(null));
@ -276,12 +277,20 @@ public class UserTest {
@Test
public void testGetTranslation() {
assertEquals("mock translation [test]", user.getTranslation("a.reference"));
assertEquals(TEST_TRANSLATION_WITH_COLOR, user.getTranslation("a.reference"));
}
/**
* Test for {@link User#getTranslationNoColor(String, String...)}
*/
@Test
public void testGetTranslationNoColor() {
assertEquals(TEST_TRANSLATION, user.getTranslationNoColor("a.reference"));
}
@Test
public void testGetTranslationWithVariable() {
assertEquals("mock translation variable", user.getTranslation("a.reference", "[test]", "variable"));
assertEquals("mock §atranslation §bvariable", user.getTranslation("a.reference", "[test]", "variable"));
}
@Test
@ -306,7 +315,7 @@ public class UserTest {
@Test
public void testSendMessage() {
user.sendMessage("a.reference");
verify(player).sendMessage(eq(TEST_TRANSLATION));
verify(player).sendMessage(TEST_TRANSLATION_WITH_COLOR);
}
@Test