1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-30 14:33:35 +01:00
Jobs/com/gamingmesh/jobs/i18n/Language.java

100 lines
3.3 KiB
Java
Raw Normal View History

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;
import org.bukkit.Bukkit;
2015-08-21 15:10:08 +02:00
import org.bukkit.ChatColor;
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 {
public FileConfiguration enlocale;
public FileConfiguration customlocale;
private Jobs plugin;
2015-08-21 15:10:08 +02:00
public Language(Jobs plugin) {
this.plugin = plugin;
}
2015-08-21 15:10:08 +02: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();
if (customlocale == null)
customlocale = enlocale;
}
/**
* Get the message with the correct key
* @param key - the key of the message
* @return the message
*/
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 = "";
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;
}
/**
* 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) {
return enlocale.contains(key) == true ? ChatColor.translateAlternateColorCodes('&', enlocale.getString(key)) : "Cant find locale";
}
2015-08-21 15:10:08 +02: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) {
if (customlocale == null || !customlocale.contains(key))
return enlocale.contains(key);
return customlocale.contains(key);
}
2015-08-21 15:10:08 +02:00
}