2015-08-21 15:10:08 +02:00
|
|
|
/**
|
|
|
|
* Jobs Plugin for Bukkit
|
|
|
|
* Copyright (C) 2011 Zak Ford <zak.j.ford@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.gamingmesh.jobs.i18n;
|
|
|
|
|
2017-01-10 16:28:35 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2015-08-21 15:10:08 +02:00
|
|
|
import org.bukkit.ChatColor;
|
2017-01-10 16:28:35 +01:00
|
|
|
import org.bukkit.command.ConsoleCommandSender;
|
2015-08-21 15:10:08 +02:00
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
2016-03-30 15:42:36 +02:00
|
|
|
import com.gamingmesh.jobs.Jobs;
|
2015-08-21 15:10:08 +02:00
|
|
|
import com.gamingmesh.jobs.config.YmlMaker;
|
|
|
|
|
|
|
|
public class Language {
|
2016-08-08 16:36:56 +02:00
|
|
|
public FileConfiguration enlocale;
|
|
|
|
public FileConfiguration customlocale;
|
|
|
|
private Jobs plugin;
|
2015-08-21 15:10:08 +02:00
|
|
|
|
2016-08-08 16:36:56 +02:00
|
|
|
public Language(Jobs plugin) {
|
2016-01-06 14:40:27 +01:00
|
|
|
this.plugin = plugin;
|
|
|
|
}
|
2015-08-21 15:10:08 +02:00
|
|
|
|
2016-01-06 14:40:27 +01:00
|
|
|
/**
|
|
|
|
* Reloads the config
|
|
|
|
*/
|
2016-06-25 15:27:01 +02:00
|
|
|
public void reload() {
|
|
|
|
customlocale = new YmlMaker(plugin, "locale/messages_" + Jobs.getGCManager().localeString + ".yml").getConfig();
|
|
|
|
enlocale = new YmlMaker(plugin, "locale/messages_en.yml").getConfig();
|
2016-01-06 14:40:27 +01:00
|
|
|
if (customlocale == null)
|
|
|
|
customlocale = enlocale;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the message with the correct key
|
|
|
|
* @param key - the key of the message
|
|
|
|
* @return the message
|
|
|
|
*/
|
2017-01-15 20:28:46 +01:00
|
|
|
public String getMessage(String key) {
|
|
|
|
return getMessage(key, "");
|
|
|
|
}
|
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
public String getMessage(String key, Object... variables) {
|
|
|
|
String missing = "Missing locale for " + key + " ";
|
|
|
|
String msg = "";
|
2017-01-10 16:28:35 +01:00
|
|
|
try {
|
|
|
|
if (customlocale == null || !customlocale.contains(key))
|
|
|
|
msg = enlocale.contains(key) == true ? ChatColor.translateAlternateColorCodes('&', enlocale.getString(key)) : missing;
|
|
|
|
else
|
|
|
|
msg = customlocale.contains(key) == true ? ChatColor.translateAlternateColorCodes('&', customlocale.getString(key)) : missing;
|
|
|
|
} catch (Exception e) {
|
|
|
|
String message = ChatColor.translateAlternateColorCodes('&', "&e[Jobs] &2Cant read language file. Plugin will be disabled.");
|
|
|
|
ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
|
|
|
|
console.sendMessage(message);
|
|
|
|
throw e;
|
|
|
|
}
|
2016-03-30 15:42:36 +02:00
|
|
|
if (variables.length > 0)
|
|
|
|
for (int i = 0; i < variables.length; i++) {
|
|
|
|
if (variables.length >= i + 2)
|
|
|
|
msg = msg.replace(String.valueOf(variables[i]), String.valueOf(variables[i + 1]));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
2016-01-06 14:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the message with the correct key
|
|
|
|
* @param key - the key of the message
|
|
|
|
* @return the message
|
|
|
|
*/
|
2016-03-30 15:42:36 +02:00
|
|
|
public String getDefaultMessage(String key) {
|
2016-01-06 14:40:27 +01:00
|
|
|
return enlocale.contains(key) == true ? ChatColor.translateAlternateColorCodes('&', enlocale.getString(key)) : "Cant find locale";
|
|
|
|
}
|
2015-08-21 15:10:08 +02:00
|
|
|
|
2016-01-06 14:40:27 +01:00
|
|
|
/**
|
|
|
|
* Check if key exists
|
|
|
|
* @param key - the key of the message
|
|
|
|
* @return true/false
|
|
|
|
*/
|
2016-03-30 15:42:36 +02:00
|
|
|
public boolean containsKey(String key) {
|
2016-01-06 14:40:27 +01:00
|
|
|
if (customlocale == null || !customlocale.contains(key))
|
|
|
|
return enlocale.contains(key);
|
|
|
|
return customlocale.contains(key);
|
|
|
|
}
|
2015-08-21 15:10:08 +02:00
|
|
|
}
|