Try and improve translation loading code.

This commit is contained in:
AppleDash 2016-12-15 19:53:43 -05:00
parent a0d8cb2d5d
commit 8bef764622
5 changed files with 74 additions and 21 deletions

View File

@ -33,7 +33,7 @@ public abstract class SaneEconomyCommand implements CommandExecutor {
MessageUtils.sendMessage(sender, e.getMessage());
for (String s : getUsage()) {
MessageUtils.sendMessage(sender, String.format("Usage: %s", s.replace("<command>", label)));
MessageUtils.sendMessage(sender, "Usage: {1}", s.replace("<command>", label));
}
} catch (CommandException e) {
MessageUtils.sendMessage(sender, e.getMessage());

View File

@ -51,7 +51,7 @@ public class BalanceCommand extends SaneEconomyCommand {
playerName = args[0];
if (!sender.hasPermission("saneeconomy.balance.other")) {
MessageUtils.sendMessage(sender, "You don't have permission to check the balance of %s.", playerIdentifier);
MessageUtils.sendMessage(sender, "You don't have permission to check the balance of {0}.", playerIdentifier);
return;
}
}

View File

@ -8,10 +8,9 @@ import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by AppleDash on 8/5/2016.
@ -31,27 +30,50 @@ public class I18n {
YamlConfiguration configJar = YamlConfiguration.loadConfiguration(new InputStreamReader(this.getClass().getResourceAsStream("/messages.yml")));
if (configFile.exists()) { // Attempt to merge any new keys from the JAR's messages.yml into the copy in the plugin's data folder
YamlConfiguration configFileYaml = YamlConfiguration.loadConfiguration(configFile);
for (Map jarMap : configJar.getMapList("messages")) {
boolean has = false;
String key = jarMap.get("message").toString();
YamlConfiguration configDisk = YamlConfiguration.loadConfiguration(configFile);
for (Map fileMap : configFileYaml.getMapList("messages")) {
if (fileMap.get("message").toString().equals(key)) {
has = true;
List<Map<?, ?>> finalKeys = configDisk.getMapList("messages");
for (Map jarObject : configJar.getMapList("messages")) { // For every translation in the template config in the JAR
String jarMessage = String.valueOf(jarObject.get("message")); // Key for this translation
Map equivalentOnDisk = null; // Equivalent of this translation in the config file on disk
for (Map diskMap : configDisk.getMapList("messages")) { // For every translation in the config on disk
if (String.valueOf(diskMap.get("message")).equals(jarMessage)) { // If the translation key on this object on disk is the same as the current one in the JAR
equivalentOnDisk = diskMap;
break;
}
}
if (!has) { // Folder messages.yml does not have this key, add it.
List<Map> map = new ArrayList<>(configFileYaml.getMapList("messages"));
map.add(ImmutableMap.of("message", key));
configFileYaml.set("messages", map);
if (equivalentOnDisk == null) { // This one isn't on disk yet - add it.
finalKeys.add(jarObject);
} else {
String currentKey = String.valueOf(equivalentOnDisk.get("message"));
String convertedKey = convertOldTranslations(currentKey);
if (!currentKey.equals(convertedKey)) { // Key needs conversion
String convertedValue = convertOldTranslations(String.valueOf(equivalentOnDisk.get("translation")));
// Remove current key from map of things to go to the disk
Iterator<Map<?, ?>> iter = finalKeys.iterator();
while (iter.hasNext()) {
if (String.valueOf(iter.next().get("message")).equals(equivalentOnDisk.get("message"))) {
iter.remove();
}
}
// Add the converted one.
finalKeys.add(ImmutableMap.of("message", convertedKey, "translation", convertedValue));
}
}
}
configDisk.set("messages", finalKeys);
try {
configFileYaml.save(configFile);
configDisk.save(configFile);
} catch (IOException e) {
throw new RuntimeException("Failed to save translations file.", e);
}
@ -69,6 +91,21 @@ public class I18n {
});
}
private String convertOldTranslations(String input) {
Matcher m = Pattern.compile("(%s)").matcher(input);
StringBuffer converted = new StringBuffer();
int index = 1;
while (m.find()) {
m.appendReplacement(converted, String.format("{%d}", index));
index++;
}
m.appendTail(converted);
return converted.toString();
}
private String translate(String input) {
return translations.containsKey(input) ? ChatColor.translateAlternateColorCodes('&', translations.get(input)) : input;
}

View File

@ -23,11 +23,21 @@ public class MessageUtils {
*/
public static void sendMessage(CommandSender target, String fmt, Object... args) {
fmt = _(fmt);
String prefix = ChatColor.translateAlternateColorCodes('&', SaneEconomy.getInstance().getConfig().getString("chat.prefix", ""));
target.sendMessage(prefix + String.format(fmt, (Object[])args));
String formatted;
if (fmt.contains("%s")) { // Legacy support.
formatted = String.format(fmt, (Object[]) args);
} else {
formatted = indexedFormat(fmt, (Object[]) args);
}
target.sendMessage(prefix + formatted);
}
public static String indexedFormat(String fmt, String... arguments) {
public static String indexedFormat(String fmt, Object... arguments) {
Matcher m = Pattern.compile("\\{([0-9]+)\\}").matcher(fmt);
StringBuffer formatted = new StringBuffer();
@ -38,7 +48,7 @@ public class MessageUtils {
throw new IllegalArgumentException("Index must be within the range of the given arguments.");
}
m.appendReplacement(formatted, arguments[index]);
m.appendReplacement(formatted, String.valueOf(arguments[index]));
}
m.appendTail(formatted);

View File

@ -14,5 +14,11 @@ public class MessageUtilsTest {
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("{1}, {2}!", "Hello", "world"));
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("Hello, {1}!", "world", "discarded"));
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("Hello, {2}!", "discarded", "world"));
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("Hello, world!", "this", "shouldn't", "change"));
}
@Test(expected = IllegalArgumentException.class)
public void testBadIndexedFormat() {
MessageUtils.indexedFormat("Hello, {3}!", "world", "something");
}
}