1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-24 02:08:48 +00:00

feat: 添加国际化工具

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-10-14 18:46:25 +08:00
parent 3ca6d79f6d
commit 399a0b4668
3 changed files with 127 additions and 94 deletions

View File

@ -1,19 +1,19 @@
package pw.yumc.YumCore.bukkit;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import java.io.File;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
/**
* 插件日志输出类
*
* @since 2016年7月23日 上午9:11:01
* @author
* @since 2016年7月23日 上午9:11:01
*/
public class Log {
private static boolean debug = new File(String.format("plugins%1$sYumCore%1$sdebug", File.separatorChar)).exists();
@ -31,10 +31,8 @@ public class Log {
* Typically the root Logger is configured with a set of Handlers
* that essentially act as default handlers for all loggers.
*
* @param handler
* a logging Handler
* @exception SecurityException
* if a security manager exists and if
* @param handler a logging Handler
* @throws SecurityException if a security manager exists and if
* the caller does not have LoggingPermission("control").
*/
public static void addHandler(Handler handler) throws SecurityException {
@ -44,8 +42,7 @@ public class Log {
/**
* Sends console a message
*
* @param message
* Message to be displayed
* @param message Message to be displayed
*/
public static void console(String message) {
console.sendMessage(prefix + message);
@ -54,10 +51,8 @@ public class Log {
/**
* Sends console a message
*
* @param message
* 消息
* @param object
* 格式化参数
* @param message 消息
* @param object 格式化参数
*/
public static void console(String message, Object... object) {
console.sendMessage(prefix + String.format(message, object));
@ -66,8 +61,7 @@ public class Log {
/**
* Sends console a message
*
* @param message
* Message to be displayed
* @param msg Message to be displayed
*/
public static void console(String[] msg) {
for (String str : msg) {
@ -78,10 +72,8 @@ public class Log {
/**
* 调试消息
*
* @param msg
* 消息
* @param object
* 参数
* @param msg 消息
* @param object 参数
*/
public static void d(String msg, Object... object) {
debug(String.format(msg, object));
@ -90,8 +82,7 @@ public class Log {
/**
* 格式化调试消息
*
* @param msg
* 消息
* @param msg 消息
*/
public static void debug(String msg) {
if (debug) {
@ -102,10 +93,8 @@ public class Log {
/**
* 调试消息
*
* @param msg
* 消息
* @param object
* 参数
* @param msg 消息
* @param object 参数
*/
public static void debug(String msg, Object... object) {
if (debug) {
@ -116,10 +105,8 @@ public class Log {
/**
* 调试消息
*
* @param msg
* 消息
* @param e
* 异常
* @param msg 消息
* @param e 异常
*/
public static void debug(String msg, Throwable e) {
if (debug) {
@ -131,8 +118,7 @@ public class Log {
/**
* 调试消息
*
* @param e
* 异常
* @param e 异常
*/
public static void debug(Throwable e) {
if (debug) {
@ -147,6 +133,10 @@ public class Log {
return prefix;
}
public static void i(String msg, Object... objs) {
logger.info(String.format(msg, objs));
}
/**
* Log an INFO message.
* <p>
@ -155,8 +145,7 @@ public class Log {
* registered output Handler objects.
* <p>
*
* @param msg
* The string message (or a key in the message catalog)
* @param msg The string message (or a key in the message catalog)
*/
public static void info(String msg) {
logger.info(msg);
@ -170,10 +159,8 @@ public class Log {
* registered output Handler objects.
* <p>
*
* @param level
* One of the message level identifiers, e.g., SEVERE
* @param msg
* The string message (or a key in the message catalog)
* @param level One of the message level identifiers, e.g., SEVERE
* @param msg The string message (or a key in the message catalog)
*/
public static void log(Level level, String msg) {
logger.log(level, msg);
@ -187,12 +174,9 @@ public class Log {
* to all the registered output Handler objects.
* <p>
*
* @param level
* One of the message level identifiers, e.g., SEVERE
* @param msg
* The string message (or a key in the message catalog)
* @param param1
* parameter to the message
* @param level One of the message level identifiers, e.g., SEVERE
* @param msg The string message (or a key in the message catalog)
* @param param1 parameter to the message
*/
public static void log(Level level, String msg, Object param1) {
logger.log(level, msg, param1);
@ -206,12 +190,9 @@ public class Log {
* to all the registered output Handler objects.
* <p>
*
* @param level
* One of the message level identifiers, e.g., SEVERE
* @param msg
* The string message (or a key in the message catalog)
* @param params
* array of parameters to the message
* @param level One of the message level identifiers, e.g., SEVERE
* @param msg The string message (or a key in the message catalog)
* @param params array of parameters to the message
*/
public static void log(Level level, String msg, Object[] params) {
logger.log(level, msg, params);
@ -230,20 +211,16 @@ public class Log {
* as a formatting parameter to the LogRecord message property.
* <p>
*
* @param level
* One of the message level identifiers, e.g., SEVERE
* @param msg
* The string message (or a key in the message catalog)
* @param thrown
* Throwable associated with log message.
* @param level One of the message level identifiers, e.g., SEVERE
* @param msg The string message (or a key in the message catalog)
* @param thrown Throwable associated with log message.
*/
public static void log(Level level, String msg, Throwable thrown) {
logger.log(level, msg, thrown);
}
/**
* @param prefix
* 插件前缀
* @param prefix 插件前缀
*/
public static void setPrefix(String prefix) {
Log.prefix = ChatColor.translateAlternateColorCodes('&', prefix);
@ -254,8 +231,7 @@ public class Log {
* <p>
* If the logger is currently enabled for the SEVERE message level then the given message is forwarded to all the registered output Handler objects.
*
* @param msg
* The string message (or a key in the message catalog)
* @param msg The string message (or a key in the message catalog)
*/
public static void severe(String msg) {
logger.severe(msg);
@ -264,10 +240,8 @@ public class Log {
/**
* Sends this sender a message
*
* @param sender
* 命令发送者
* @param msg
* 消息
* @param sender 命令发送者
* @param msg 消息
*/
public static void toSender(CommandSender sender, String msg) {
sender.sendMessage(prefix + msg);
@ -276,12 +250,9 @@ public class Log {
/**
* Sends this sender a message
*
* @param sender
* 命令发送者
* @param msg
* 消息
* @param objs
* 参数
* @param sender 命令发送者
* @param msg 消息
* @param objs 参数
*/
public static void toSender(CommandSender sender, String msg, Object... objs) {
sender.sendMessage(prefix + String.format(msg, objs));
@ -290,10 +261,8 @@ public class Log {
/**
* Sends this sender a message
*
* @param sender
* 命令发送者
* @param msg
* 消息
* @param sender 命令发送者
* @param msg 消息
*/
public static void toSender(CommandSender sender, String[] msg) {
for (String str : msg) {
@ -304,10 +273,8 @@ public class Log {
/**
* 格式化警告消息
*
* @param string
* 消息
* @param objects
* 参数
* @param string 消息
* @param objects 参数
*/
public static void w(String string, Object... objects) {
logger.warning(String.format(string, objects));
@ -321,8 +288,7 @@ public class Log {
* registered output Handler objects.
* <p>
*
* @param msg
* The string message (or a key in the message catalog)
* @param msg The string message (or a key in the message catalog)
*/
public static void warning(String msg) {
logger.warning(msg);

View File

@ -1,22 +1,14 @@
package pw.yumc.YumCore.commands;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.commands.annotation.Help;
import pw.yumc.YumCore.commands.info.CommandInfo;
import pw.yumc.YumCore.commands.interfaces.CommandHelpParse;
import java.util.*;
/**
* 命令帮助生成类
*
@ -28,9 +20,9 @@ public class CommandHelp {
* 消息配置
*/
private static String prefix = String.format("§6[§b%s§6] ", P.instance.getName());
private static String commandNotFound = prefix + "§c当前插件未注册任何子命令!";
private static String commandNotFound = prefix + "§c当前插件未注册默认命令以及子命令!";
private static String pageNotFound = prefix + "§c不存在的帮助页面 §b请输入 /%s help §e1-%s";
private static String helpTitle = String.format("§6========= %s §6插件帮助列表=========", prefix);
private static String helpTitle = String.format("§6========= %s §6插件帮助列表 §aBy §b喵♂呜 §6=========", prefix);
private static String helpBody = "§6/%1$s §a%2$s §e%3$s §6- §b%4$s";
private static String helpFooter = "§6查看更多的帮助页面 §b请输入 /%s help §e1-%s";
/**

View File

@ -0,0 +1,75 @@
package pw.yumc.YumCore.global;
import org.bukkit.command.CommandSender;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.config.ext.YumConfig;
import java.util.HashMap;
import java.util.Map;
/**
* 国际化工具类
* Created by on 2016/10/14 0014.
*/
public class I18N {
private static String LANG = "zh_CN";
private static Map<String, String> content;
static {
content = new HashMap<>();
Log.info("Async init I18N tool ...");
load();
}
private I18N() {
}
public static String $(String key) {
return $(key, "");
}
public static String $(String key, String def) {
return content.containsKey(key) ? content.get(key) : def;
}
public static void info(String str, Object... objs) {
Log.i($(str), objs);
}
public static void info(String str, String def, Object... objs) {
Log.i($(str, def), objs);
}
public static void send(CommandSender sender, String message, Object... objs) {
send(sender, message, "", objs);
}
public static void send(CommandSender sender, String message, String def, Object... objs) {
Log.toSender(sender, $(message, def), objs);
}
/**
* 载入数据
*/
private static void load() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Map<String, String> local = YumConfig.getLocal(LANG).getContentMap();
Map<String, String> remote = YumConfig.getRemote(LANG).getContentMap();
if (local != null) {
content.putAll(local);
}
if (remote != null) {
content.putAll(remote);
}
Log.info("本地化工具初始化完毕...");
} catch (Exception e) {
Log.warning(String.format("本地化工具初始化失败: %s %s", e.getClass().getName(), e.getMessage()));
Log.debug(LANG, e);
}
}
}).start();
}
}