Add experimental message translation support.

This commit is contained in:
AppleDash 2016-08-08 13:40:34 -04:00
parent 8791dc4299
commit a170e46851
9 changed files with 145 additions and 35 deletions

View File

@ -6,10 +6,10 @@
<parent>
<groupId>org.appledash</groupId>
<artifactId>SaneEconomy</artifactId>
<version>0.6.6-SNAPSHOT</version>
<version>0.6.7-SNAPSHOT</version>
</parent>
<artifactId>SaneEconomyCore</artifactId>
<version>0.6.6-SNAPSHOT</version>
<version>0.6.7-SNAPSHOT</version>
<dependencies>
<dependency>

View File

@ -9,6 +9,7 @@ import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendFlatf
import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendMySQL;
import org.appledash.saneeconomy.listeners.JoinQuitListener;
import org.appledash.saneeconomy.updates.GithubVersionChecker;
import org.appledash.saneeconomy.utils.I18n;
import org.appledash.saneeconomy.vault.VaultHook;
import org.bukkit.plugin.java.JavaPlugin;
@ -63,6 +64,7 @@ public class SaneEconomy extends JavaPlugin {
getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
economyManager.getBackend().reloadTopPlayerBalances();
}, 0, (20 * 300) /* Update baltop every 5 minutes */);
I18n.getInstance().loadTranslations();
}
@Override

View File

@ -15,6 +15,8 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import static org.appledash.saneeconomy.utils.I18n._;
/**
* Created by AppleDash on 6/13/2016.
* Blackjack is still best pony.
@ -57,7 +59,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
OfflinePlayer targetPlayer = PlayerUtils.getOfflinePlayer(sTargetPlayer);
if (targetPlayer == null) {
MessageUtils.sendMessage(sender, "That player does not exist.");
MessageUtils.sendMessage(sender, _("That player does not exist."));
return;
}
@ -67,14 +69,14 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
double amount = NumberUtils.parseAndFilter(ecoMan.getCurrency(), sAmount);
if (amount <= 0) {
MessageUtils.sendMessage(sender, "%s is not a positive number.", (amount == -1 ? sAmount : amount + ""));
MessageUtils.sendMessage(sender, _("%s is not a positive number."), (amount == -1 ? sAmount : amount + ""));
return;
}
if (subCommand.equalsIgnoreCase("give")) {
double newAmount = ecoMan.addBalance(economable, amount);
MessageUtils.sendMessage(sender, "Added %s to %s. Their balance is now %s.",
MessageUtils.sendMessage(sender, _("Added %s to %s. Their balance is now %s."),
ecoMan.getCurrency().formatAmount(amount),
sTargetPlayer,
ecoMan.getCurrency().formatAmount(newAmount)
@ -83,7 +85,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
} else if (subCommand.equalsIgnoreCase("take")) {
double newAmount = ecoMan.subtractBalance(economable, amount);
MessageUtils.sendMessage(sender, "Took %s from %s. Their balance is now %s.",
MessageUtils.sendMessage(sender, _("Took %s from %s. Their balance is now %s."),
ecoMan.getCurrency().formatAmount(amount),
sTargetPlayer,
ecoMan.getCurrency().formatAmount(newAmount)
@ -91,7 +93,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
return;
} else if (subCommand.equalsIgnoreCase("set")) {
ecoMan.setBalance(economable, amount);
MessageUtils.sendMessage(sender, "Balance for %s set to %s", sTargetPlayer, ecoMan.getCurrency().formatAmount(amount));
MessageUtils.sendMessage(sender, _("Balance for %s set to %s."), sTargetPlayer, ecoMan.getCurrency().formatAmount(amount));
return;
}

View File

@ -0,0 +1,82 @@
package org.appledash.saneeconomy.utils;
import com.google.common.collect.ImmutableMap;
import org.appledash.saneeconomy.SaneEconomy;
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;
/**
* Created by AppleDash on 8/5/2016.
* Blackjack is still best pony.
*/
public class I18n {
private static I18n INSTANCE = new I18n(SaneEconomy.getInstance());
private SaneEconomy plugin;
private Map<String, String> translations = new HashMap<>();
public I18n(SaneEconomy plugin) {
this.plugin = plugin;
}
public String translate(String input) {
return translations.containsKey(input) ? translations.get(input) : input;
}
public void loadTranslations() {
File configFile = new File(plugin.getDataFolder(), "messages.yml");
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();
for (Map fileMap : configFileYaml.getMapList("messages")) {
if (fileMap.get("message").toString().equals(key)) {
has = true;
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);
}
}
try {
configFileYaml.save(configFile);
} catch (IOException e) {
throw new RuntimeException("Failed to save translations file.", e);
}
} else {
try {
configJar.save(configFile);
} catch (IOException e) {
throw new RuntimeException("Failed to save initial translations file.", e);
}
}
YamlConfiguration configFileYaml = YamlConfiguration.loadConfiguration(configFile);
configFileYaml.getMapList("messages").stream().filter(map -> map.containsKey("translation")).forEach(map -> {
translations.put(map.get("message").toString(), map.get("translation").toString());
});
}
public static String _(String s) {
return INSTANCE.translate(s);
}
public static I18n getInstance() {
return INSTANCE;
}
}

View File

@ -4,6 +4,8 @@ import org.appledash.saneeconomy.SaneEconomy;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import static org.appledash.saneeconomy.utils.I18n._;
/**
* Created by AppleDash on 6/13/2016.
* Blackjack is still best pony.
@ -17,6 +19,7 @@ public class MessageUtils {
* @param args String#format args
*/
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));
}

View File

@ -11,34 +11,32 @@ import java.text.ParseException;
* Blackjack is still best pony.
*/
public class NumberUtils {
private static final double INVALID_DOUBLE = -1;
public static double parsePositiveDouble(String sDouble) {
try {
if (Strings.isNullOrEmpty(sDouble)) {
throw new NumberFormatException();
}
sDouble = sDouble.trim();
if (sDouble.equalsIgnoreCase("nan") || sDouble.equalsIgnoreCase("infinity") || sDouble.equalsIgnoreCase("-infinity")) {
throw new NumberFormatException();
}
double doub;
try {
doub = NumberFormat.getInstance().parse(sDouble).doubleValue();
} catch (ParseException e) {
throw new NumberFormatException();
}
if (doub < 0) {
throw new NumberFormatException();
}
return doub;
} catch (NumberFormatException e) {
return -1;
if (Strings.isNullOrEmpty(sDouble)) {
return INVALID_DOUBLE;
}
sDouble = sDouble.trim();
if (sDouble.equalsIgnoreCase("nan") || sDouble.equalsIgnoreCase("infinity") || sDouble.equalsIgnoreCase("-infinity")) {
return INVALID_DOUBLE;
}
double doub;
try {
doub = NumberFormat.getInstance().parse(sDouble).doubleValue();
} catch (ParseException | NumberFormatException e) {
return INVALID_DOUBLE;
}
if (doub < 0) {
return INVALID_DOUBLE;
}
return doub;
}
public static double filterAmount(Currency currency, double amount) {

View File

@ -0,0 +1,23 @@
# How to use this file:
# Add a translation to the messages you wish to change, like this:
# - message: That player is not online.
# translation: Ce joueur est pas en ligne.
messages:
- message: You don't have permission to check the balance of %s.
- message: That player is not online.
- message: You cannot pay yourself.
- message: "Usage: %s"
- message: That player does not exist.
- message: Balance for %s is %s.
- message: "Top %d players:"
- message: That player does not exist.
- message: "%s is not a positive number."
- message: Added %s to %s. Their balance is now %s.
- message: Took %s from %s. Their balance is now %s.
- message: Balance for %s set to %s
- message: You do not have enough money to transfer %s to %s.
- message: You have transferred %s to %s.
- message: You have received %s from %s.
- message: Reloading database...
- message: Database reloaded.
- message: You've been issued a starting balance of %s!

View File

@ -1,7 +1,7 @@
name: SaneEconomy
author: AppleDash
main: org.appledash.saneeconomy.SaneEconomy
version: 0.6.6
version: 0.6.7
softdepend: [Vault]
commands:
balance:

View File

@ -6,7 +6,7 @@
<groupId>org.appledash</groupId>
<artifactId>SaneEconomy</artifactId>
<version>0.6.6-SNAPSHOT</version>
<version>0.6.7-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>