Skip to content

Commit

Permalink
improved lang loading
Browse files Browse the repository at this point in the history
  • Loading branch information
RecursiveG committed Jul 25, 2015
1 parent 3921140 commit f9ece93
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 185 deletions.
60 changes: 2 additions & 58 deletions src/main/java/think/rpgitems/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class Plugin extends JavaPlugin {
@Override
public void onLoad() {
plugin = this;
reloadConfig();
saveDefaultConfig();
Font.load();
Power.powers.put("arrow", PowerArrow.class);
Power.powers.put("tntcannon", PowerTNTCannon.class);
Expand All @@ -71,7 +71,7 @@ public void onLoad() {

@Override
public void onEnable() {
new Locale(this);
Locale.init(this);
updateConfig();
WorldGuard.init(this);
ConfigurationSection conf = getConfig();
Expand All @@ -88,62 +88,6 @@ public void onEnable() {
new PowerTicker().runTaskTimer(this, 0, 1);
}

@Override
public void saveConfig() {
FileConfiguration config = getConfig();
FileOutputStream out = null;
try {
File f = new File(getDataFolder(), "config.yml");
if (!f.exists())
f.createNewFile();
out = new FileOutputStream(f);
out.write(config.saveToString().getBytes("UTF-8"));
} catch (FileNotFoundException e) {
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public static FileConfiguration config;

@Override
public void reloadConfig() {
FileInputStream in = null;
config = new YamlConfiguration();
try {
File f = new File(getDataFolder(), "config.yml");
in = new FileInputStream(f);
byte[] data = new byte[(int) f.length()];
in.read(data);
String str = new String(data, "UTF-8");
config.loadFromString(str);
} catch (FileNotFoundException e) {
} catch (IOException e) {
} catch (InvalidConfigurationException e) {
} finally {
try {
if (in != null)
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

public FileConfiguration getConfig() {
return config;
}

public void updateConfig() {
ConfigUpdater.updateConfig(getConfig());
saveConfig();
Expand Down
181 changes: 54 additions & 127 deletions src/main/java/think/rpgitems/data/Locale.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,156 +19,83 @@
import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import com.google.common.base.Charsets;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import think.rpgitems.Plugin;

public class Locale {

private static HashMap<String, String> localeStrings = new HashMap<String, String>();
private static File localeFolder;
private static File dataFolder;
private static String usedLocale;
private String version;
private static final String DEFAULT_LANGUAGE = "en_GB";

public Locale(Plugin plugin) {
version = plugin.getDescription().getVersion();
if (!plugin.getConfig().getString("pluginVersion", "0.0").equals(version)) {
plugin.getConfig().set("pluginVersion", version);
plugin.saveConfig();
}
dataFolder = plugin.getDataFolder();
localeFolder = new File(dataFolder.getAbsolutePath() + File.separatorChar + "locale");
if (!localeFolder.exists())
writeLocaleFolder();
usedLocale = plugin.getConfig().getString("language");
if (!plugin.getConfig().contains("language")) {
plugin.getConfig().set("language", "en_GB");
plugin.saveConfig();
usedLocale = "en_GB";
private static Locale instance = null;
private Map<String, String> languageList = new HashMap<>();

private static HashMap<String, String> localeStrings = new HashMap<>();
private String usedLocale;

public static void init(Plugin plugin) {
if (instance == null) {
instance = new Locale(plugin);
} else {
testUsedLocale();
Plugin.logger.warning("Duplicated init of Locale");
}
reloadLocales(plugin);
}

private static void testUsedLocale() {
File[] locales = localeFolder.listFiles();

if (locales != null)
for (File loc : locales)
if (usedLocale.equalsIgnoreCase(loc.getName().replace(".lang", "")))
return;
InputStream localeSource = Plugin.plugin.getResource("locale/" + usedLocale + ".lang");
if (localeSource != null) {
OutputStream localeDest = null;
try {
localeDest = new FileOutputStream(new File(localeFolder, usedLocale + ".lang"));
} catch (FileNotFoundException e1) {
}

int read = 0;
byte[] bytes = new byte[1024];

try {
while ((read = localeSource.read(bytes)) != -1) {
localeDest.write(bytes, 0, read);
}
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[RPGItems] Warning: The locale you set (" + usedLocale + ") did not exist and has to be configured!");
return;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (localeSource != null)
try {
localeSource.close();
} catch (IOException e) {
e.printStackTrace();
}
if (localeDest != null)
try {
localeDest.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[RPGItems] Error: The language you've set (" + usedLocale + ") isn't supported or doesn't exist!");
private static Locale getInstance() {
return instance;
}

private static void writeLocaleFolder() {
Bukkit.getConsoleSender().sendMessage("[RPGItems] Started writing locale folder.");
localeFolder.mkdirs();
JarFile jar = null;
try {
jar = new JarFile(dataFolder.getParentFile().getAbsolutePath() + File.separator + "RPGItems.jar");
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
String entryName = entries.nextElement().getName();
if (entryName.startsWith("locale/") && entryName.endsWith(".lang")) {
InputStream localeSource = Plugin.plugin.getResource(entryName);
OutputStream localeDest = new FileOutputStream(new File(localeFolder, entryName.replace("locale/", "")));

int read = 0;
byte[] bytes = new byte[1024];

try {
while ((read = localeSource.read(bytes)) != -1) {
localeDest.write(bytes, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (localeSource != null)
try {
localeSource.close();
} catch (IOException e) {
e.printStackTrace();
}
if (localeDest != null)
try {
localeDest.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[RPGItems] Successfully generated locale folder!");
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (jar != null)
try {
jar.close();
} catch (IOException e) {
e.printStackTrace();
}
private Locale(Plugin plugin) {
// load configures
final InputStream langStream = plugin.getResource("languages.txt");
languageList = loadLocaleStream(langStream);

usedLocale = plugin.getConfig().getString("language");
if (!plugin.getConfig().contains("language") || !languageList.containsKey(usedLocale)) {
plugin.getConfig().set("language", DEFAULT_LANGUAGE);
plugin.saveConfig();
usedLocale = DEFAULT_LANGUAGE;
}

}
// load files
plugin.saveResource("locale/" + DEFAULT_LANGUAGE + ".lang", true);
plugin.saveResource("locale/" + usedLocale + ".lang", true);

public static void reloadLocales(Plugin plugin) {
localeStrings.clear();
try {
localeStrings = loadLocaleStream(new FileInputStream(new File(localeFolder.getAbsolutePath() + File.separatorChar + usedLocale + ".lang")));
} catch (FileNotFoundException e) {
File localeFolder = new File(plugin.getDataFolder().getAbsolutePath() + File.separatorChar + "locale");
localeStrings.clear();
// use English as fallback
localeStrings.putAll(loadLocaleStream(new FileInputStream(new File(localeFolder.getAbsolutePath() + File.separatorChar + DEFAULT_LANGUAGE + ".lang"))));
localeStrings.putAll(loadLocaleStream(new FileInputStream(new File(localeFolder.getAbsolutePath() + File.separatorChar + usedLocale + ".lang"))));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
Plugin.logger.warning("Error when loading language files.");
}
}

private static HashMap<String, String> loadLocaleStream(InputStream in, HashMap<String, String> map) {
private HashMap<String, String> loadLocaleStream(InputStream in, HashMap<String, String> map) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
if (line.startsWith("#"))
String line;
for (line = reader.readLine(); line != null; line = reader.readLine()) {
line = line.trim();
if (line.startsWith("#") || line.length() == 0) {
continue;
String[] args = line.split("=");
map.put(args[0].trim(), args[1].trim());
}
String[] args = line.split("=", 2);
if (args.length == 2) {
map.put(args[0].trim(), args[1].trim());
} else {
Plugin.logger.warning("Unknown lang line: " + line);
}
}
return map;
} catch (UnsupportedEncodingException e) {
Expand All @@ -179,18 +106,18 @@ private static HashMap<String, String> loadLocaleStream(InputStream in, HashMap<
return null;
}

private static HashMap<String, String> loadLocaleStream(InputStream in) {
private HashMap<String, String> loadLocaleStream(InputStream in) {
return loadLocaleStream(in, new HashMap<String, String>());
}

public static String getServerLocale() {
return usedLocale;
return getInstance().usedLocale;
}

public static String get(String key) {
if (localeStrings == null || !localeStrings.containsKey(key)) {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[RPGItems] Unknown translation in " + usedLocale + " for " + key + ".");
return ""; // TODO: Add fallback to known language?
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[RPGItems] Unknown translation in " + getServerLocale() + " for " + key + " .");
return "<" + key + ">";
}
return localeStrings.get(key);
}
Expand Down
Empty file added src/main/resources/config.yml
Empty file.

0 comments on commit f9ece93

Please sign in to comment.