LunaChat v3-k2 : Messages localization feature. (WIP)
このコミットが含まれているのは:
1 親
db5fad0bb8
コミット
e82aa534ba
10ファイル変更
+419
-84
検索条件に一致するファイルはありません
@@ -19,6 +19,9 @@ import com.github.ucchyocean.lc3.japanize.JapanizeType;
|
||||
*/
|
||||
public class LunaChatConfig {
|
||||
|
||||
/** メッセージの言語 */
|
||||
private String lang;
|
||||
|
||||
/** チャンネルチャット機能を利用可能にするかどうか */
|
||||
private boolean enableChannelChat;
|
||||
|
||||
@@ -31,12 +34,6 @@ public class LunaChatConfig {
|
||||
/** チャンネルチャットの発言内容を、ログに残すかどうか */
|
||||
private boolean loggingChat;
|
||||
|
||||
/** チャンネルチャットの発言内容を、HawkEyeに記録するかどうか */
|
||||
private boolean loggingChatToHawkEye;
|
||||
|
||||
/** チャンネルチャットの発言内容を、Prismに記録するかどうか */
|
||||
private boolean loggingChatToPrism;
|
||||
|
||||
/** チャンネルチャットの発言内容を、コンソールに表示するかどうか */
|
||||
private boolean displayChatOnConsole;
|
||||
|
||||
@@ -171,17 +168,22 @@ public class LunaChatConfig {
|
||||
File configFile = new File(dataFolder, "config.yml");
|
||||
if ( !configFile.exists() ) {
|
||||
Utility.copyFileFromJar(jarFile, configFile, "config_ja.yml", false);
|
||||
String language = Utility.getDefaultLocale().getLanguage();
|
||||
if ( language.equals("ja") ) {
|
||||
Utility.copyFileFromJar(jarFile, configFile, "config_ja.yml", false);
|
||||
} else {
|
||||
Utility.copyFileFromJar(jarFile, configFile, "config.yml", false);
|
||||
}
|
||||
}
|
||||
|
||||
YamlConfig config = YamlConfig.load(configFile);
|
||||
|
||||
lang = config.getString("lang", "en");
|
||||
enableChannelChat = config.getBoolean("enableChannelChat", true);
|
||||
playerChatEventListenerPriority
|
||||
= getEventPriority(config.getString("playerChatEventListenerPriority"), EventPriority.HIGH);
|
||||
noJoinAsGlobal = config.getBoolean("noJoinAsGlobal", true);
|
||||
loggingChat = config.getBoolean("loggingChat", true);
|
||||
loggingChatToHawkEye = config.getBoolean("loggingChatToHawkEye", true);
|
||||
loggingChatToPrism = config.getBoolean("loggingChatToPrism", true);
|
||||
displayChatOnConsole = config.getBoolean("displayChatOnConsole", true);
|
||||
globalMarker = config.getString("globalMarker", "!");
|
||||
zeroMemberRemove = config.getBoolean("zeroMemberRemove", false);
|
||||
@@ -276,6 +278,14 @@ public class LunaChatConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* メッセージの言語
|
||||
* @return lang
|
||||
*/
|
||||
public String getLang() {
|
||||
return lang;
|
||||
}
|
||||
|
||||
/**
|
||||
* チャンネルチャット機能を利用可能にするかどうか
|
||||
* @return enableChannelChatを返す
|
||||
@@ -308,22 +318,6 @@ public class LunaChatConfig {
|
||||
return loggingChat;
|
||||
}
|
||||
|
||||
/**
|
||||
* チャンネルチャットの発言内容を、HawkEyeに記録するかどうか
|
||||
* @return loggingChatToHawkEye
|
||||
*/
|
||||
public boolean isLoggingChatToHawkEye() {
|
||||
return loggingChatToHawkEye;
|
||||
}
|
||||
|
||||
/**
|
||||
* チャンネルチャットの発言内容を、Prismに記録するかどうか
|
||||
* @return loggingChatToPrism
|
||||
*/
|
||||
public boolean isLoggingChatToPrism() {
|
||||
return loggingChatToPrism;
|
||||
}
|
||||
|
||||
/**
|
||||
* チャンネルチャットの発言内容を、コンソールに表示するかどうか
|
||||
* @return displayChatOnConsoleを返す
|
||||
|
||||
@@ -7,9 +7,8 @@ package com.github.ucchyocean.lc3;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Locale;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
/**
|
||||
* プラグインのメッセージリソース管理クラス
|
||||
@@ -17,27 +16,57 @@ import java.util.Locale;
|
||||
*/
|
||||
public class Messages {
|
||||
|
||||
private static final String BASE_NAME = "messages";
|
||||
private static final String FILE_NAME = BASE_NAME + ".yml";
|
||||
|
||||
private static YamlConfig resources;
|
||||
private static File _messageFolder;
|
||||
private static File _jar;
|
||||
|
||||
/**
|
||||
* 初期化する
|
||||
* Jarファイル内から直接 messages_en.yml をdefaultMessagesとしてロードし、
|
||||
* langに対応するメッセージをファイルからロードする。
|
||||
* @param messagesFolder メッセージ格納フォルダ
|
||||
* @param jar jarファイル
|
||||
* @param lang デフォルト言語
|
||||
*/
|
||||
protected static void initialize() {
|
||||
public static void initialize(File messagesFolder, File jar, String lang) {
|
||||
|
||||
File file = new File(
|
||||
LunaChat.getDataFolder() +
|
||||
File.separator + FILE_NAME);
|
||||
_jar = jar;
|
||||
_messageFolder = messagesFolder;
|
||||
if ( !_messageFolder.exists() ) {
|
||||
_messageFolder.mkdirs();
|
||||
}
|
||||
|
||||
// コンフィグフォルダにメッセージファイルがまだ無いなら、コピーしておく
|
||||
for ( String filename : new String[]{
|
||||
"messages_en.yml", "messages_ja.yml"} ) {
|
||||
File file = new File(_messageFolder, filename);
|
||||
if ( !file.exists() ) {
|
||||
Utility.copyFileFromJar(_jar, file, filename, true);
|
||||
}
|
||||
}
|
||||
|
||||
// デフォルトメッセージを、jarファイル内からロードする
|
||||
YamlConfig defaultMessages = null;
|
||||
try ( JarFile jarFile = new JarFile(_jar) ) {
|
||||
|
||||
ZipEntry zipEntry = jarFile.getEntry(String.format("messages_%s.yml", lang));
|
||||
if ( zipEntry == null ) {
|
||||
zipEntry = jarFile.getEntry("messages_en.yml");
|
||||
}
|
||||
|
||||
defaultMessages = YamlConfig.load(jarFile.getInputStream(zipEntry));
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 対応する言語のメッセージをロードする
|
||||
File file = new File(_messageFolder, String.format("messages_%s.yml", lang));
|
||||
if ( !file.exists() ) {
|
||||
Utility.copyFileFromJar(LunaChat.getPluginJarFile(),
|
||||
file, FILE_NAME, false);
|
||||
file = new File(_messageFolder, "messages_en.yml");
|
||||
}
|
||||
|
||||
resources = YamlConfig.load(file);
|
||||
resources.addDefaults(loadDefaultMessages());
|
||||
resources.addDefaults(defaultMessages);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,47 +75,14 @@ public class Messages {
|
||||
* @return リソース
|
||||
*/
|
||||
public static String get(String key) {
|
||||
|
||||
if ( resources == null ) {
|
||||
initialize();
|
||||
}
|
||||
return Utility.replaceColorCode(resources.getString(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Jarファイル内から直接 messages.yml を読み込み、YamlConfigにして返すメソッド
|
||||
* @return
|
||||
* 指定された言語でリロードを行う。
|
||||
* @param lang 言語
|
||||
*/
|
||||
private static YamlConfig loadDefaultMessages() {
|
||||
try {
|
||||
return YamlConfig.load(getResourceInputStream());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Jarファイル内を検索して、現在のlanguageやcountryになるべく一致するリソースを見つけて、InputStreamで返す
|
||||
* @return InputStream
|
||||
*/
|
||||
private static InputStream getResourceInputStream() {
|
||||
|
||||
String baseName = BASE_NAME;
|
||||
Locale locale = Locale.getDefault();
|
||||
|
||||
ArrayList<String> candidates = new ArrayList<String>();
|
||||
candidates.add(String.format("/%s_%s_%s.yaml", baseName, locale.getLanguage(), locale.getCountry()));
|
||||
candidates.add(String.format("/%s_%s_%s.yml", baseName, locale.getLanguage(), locale.getCountry()));
|
||||
candidates.add(String.format("/%s_%s.yaml", baseName, locale.getLanguage()));
|
||||
candidates.add(String.format("/%s_%s.yml", baseName, locale.getLanguage()));
|
||||
candidates.add("/" + baseName + ".yaml");
|
||||
candidates.add("/" + baseName + ".yml");
|
||||
|
||||
for ( String name : candidates ) {
|
||||
InputStream stream = Messages.class.getResourceAsStream(name);
|
||||
if ( stream != null ) return stream;
|
||||
}
|
||||
return null;
|
||||
public static void reload(String lang) {
|
||||
initialize(_jar, _messageFolder, lang);
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.Locale;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
@@ -32,6 +33,9 @@ public class Utility {
|
||||
public static void copyFileFromJar(
|
||||
File jarFile, File targetFile, String sourceFilePath, boolean isBinary) {
|
||||
|
||||
// でばっぐ
|
||||
System.out.println(jarFile + " - " + targetFile + " - " + sourceFilePath);
|
||||
|
||||
File parent = targetFile.getParentFile();
|
||||
if (!parent.exists()) {
|
||||
parent.mkdirs();
|
||||
@@ -178,4 +182,14 @@ public class Utility {
|
||||
// TODO 未実装
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 動作環境のロケールを取得する。
|
||||
* @return 動作環境のロケール
|
||||
*/
|
||||
public static Locale getDefaultLocale() {
|
||||
Locale locale = Locale.getDefault();
|
||||
if ( locale == null ) return Locale.ENGLISH;
|
||||
return locale;
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
/**
|
||||
* Yaml設定読み書きユーティリティクラス
|
||||
* このクラスは、階層構造のデータ形式に対応していないので注意。
|
||||
* @author ucchy
|
||||
*/
|
||||
public class YamlConfig {
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.github.ucchyocean.lc3.LunaChatAPI;
|
||||
import com.github.ucchyocean.lc3.LunaChatConfig;
|
||||
import com.github.ucchyocean.lc3.LunaChatLogger;
|
||||
import com.github.ucchyocean.lc3.LunaChatMode;
|
||||
import com.github.ucchyocean.lc3.Messages;
|
||||
import com.github.ucchyocean.lc3.PluginInterface;
|
||||
import com.github.ucchyocean.lc3.bridge.DynmapBridge;
|
||||
import com.github.ucchyocean.lc3.bridge.McMMOBridge;
|
||||
@@ -69,6 +70,7 @@ public class LunaChatBukkit extends JavaPlugin implements PluginInterface {
|
||||
|
||||
// 変数などの初期化
|
||||
config = new LunaChatConfig(getDataFolder(), getFile());
|
||||
Messages.initialize(new File(getDataFolder(), "messages"), getFile(), config.getLang());
|
||||
manager = new ChannelManager();
|
||||
normalChatLogger = new LunaChatLogger("==normalchat");
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import com.github.ucchyocean.lc3.LunaChatAPI;
|
||||
import com.github.ucchyocean.lc3.LunaChatConfig;
|
||||
import com.github.ucchyocean.lc3.LunaChatLogger;
|
||||
import com.github.ucchyocean.lc3.LunaChatMode;
|
||||
import com.github.ucchyocean.lc3.Messages;
|
||||
import com.github.ucchyocean.lc3.PluginInterface;
|
||||
import com.github.ucchyocean.lc3.channel.ChannelManager;
|
||||
|
||||
@@ -48,6 +49,7 @@ public class LunaChatBungee extends Plugin implements PluginInterface {
|
||||
|
||||
// 初期化
|
||||
config = new LunaChatConfig(getDataFolder(), getFile());
|
||||
Messages.initialize(new File(getDataFolder(), "messages"), getFile(), config.getLang());
|
||||
history = new HashMap<String, String>();
|
||||
|
||||
manager = new ChannelManager();
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする