public class Locale {
private final JavaPluginplugin;
private final HashMap<String, MessageFormat> messageCache = new HashMap<>();
private final Properties locale = new Properties();
private File localeFile;
public Locale(JavaPlugin plugin) {
this.plugin = aThis;
}
/**
* Инициализация класса. Должно вызываться в первую очередь. В противном
* случае вы будете получать Key "key" does not exists!
*/
public void init() {
this.locale.clear();
String loc = this.plugin.getConfig().getString("locale", "ru_RU");
this.localeFile = new File(this.plugin.getDataFolder(), loc + ".properties");
if (this.saveLocale(loc)) {
try (FileReader fr = new FileReader(this.localeFile)) {
this.locale.load(fr);
} catch (IOException ex) {
Logging.severe("Failed to load " + loc + " locale!", ex);
}
} else {
try {
this.locale.load(this.plugin.getResource("ru_RU.properties"));
} catch (IOException ex) {
Logging.severe("Failed to load ru_RU locale!", ex);
}
}
}
/**
* Получение сообщения из конфигурации Пример сообщения: "There is so many
* players." Пример вызова: getString("key");
*
* @param key ключ сообщения
* @return сообщение, иначе null
*/
public String getString(final String key) {
return this.getString(key, false, "");
}
/**
* Получение сообщения с аргументами из конфигурации Пример сообщения:
* "There is {0} players: {1}." Пример вызова: getString("key", "2", "You,
* Me");
*
* @param key ключ сообщения
* @param args аргументы сообщения
* @return сообщение, иначе null
*/
public String getString(final String key, final String... args) {
return this.getString(key, false, args);
}
/**
* Получение сообщения из конфигурации с возможностью фильтрации цвета
* Пример сообщения: "\u00a76There is so many players." Пример вызова:
* getString("key", false);
*
* @param key ключ сообщения
* @param removeColors если true, то цвета будут убраны
* @return сообщение, иначе null
*/
public String getString(final String key, final boolean removeColors) {
return this.getString(key, removeColors, "");
}
/**
* Получение сообщения с аргументами из конфигурации с возможностью
* фильтрации цвета Пример сообщения: "\u00a76There is \u00a7c{0}
* \u00a76players:\u00a7c {1}." Пример вызова: getString("key", false, "2",
* "You, Me");
*
* @param key ключ сообщения
* @param removeColors если true, то цвета будут убраны
* @param args аргументы сообщения
* @return сообщение, иначе null
*/
public String getString(final String key, final boolean removeColors, final String... args) {
String out = this.locale.getProperty(key);
if (out == null) {
return ChatColor.RED + "Key \"" + key + "\" not found!";
}
MessageFormat mf = this.messageCache.get(out);
if (mf == null) {
mf = new MessageFormat(out);
this.messageCache.put(out, mf);
}
out = mf.format(args);
if (removeColors) {
out = ChatColor.stripColor(out);
}
return CHAT_PREFIX + out;
}
private boolean saveLocale(final String name) {
if (this.localeFile.exists()) {
return true;
}
InputStream is = this.plugin.getResource(name + ".properties");
if (is == null) {
Logging.severe("Locale '" + name + "' does not exists!");
return false;
}
try {
FileUtils.copyInputStreamToFile(is, localeFile);
} catch (IOException ex) {
Logging.severe("Failed to save '" + name + ".properties'!", ex);
return false;
}
return true;
}
}