Improve lang file saving/loading, fixes #235

This commit is contained in:
HappyPikachu 2018-01-10 12:14:20 -05:00
parent 76f0076f9d
commit 91ec670842
2 changed files with 17 additions and 15 deletions

View File

@ -239,9 +239,10 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found in Quests jar"); throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found in Quests jar");
} }
File outFile = new File(getDataFolder(), outputPath); String outPath = outputPath.replaceAll("/", File.separator);
int lastIndex = resourcePath.lastIndexOf('/'); File outFile = new File(getDataFolder(), outPath);
File outDir = new File(getDataFolder(), outputPath.substring(0, lastIndex >= 0 ? lastIndex : 0)); int lastIndex = resourcePath.lastIndexOf(File.separator);
File outDir = new File(getDataFolder(), outPath.substring(0, lastIndex >= 0 ? lastIndex : 0));
if (!outDir.exists()) { if (!outDir.exists()) {
outDir.mkdirs(); outDir.mkdirs();

View File

@ -82,13 +82,10 @@ public class Lang {
public void loadLang() throws InvalidConfigurationException, IOException { public void loadLang() throws InvalidConfigurationException, IOException {
File langFile = new File(plugin.getDataFolder(), File.separator + "lang" + File.separator + iso + File.separator + "strings.yml"); File langFile = new File(plugin.getDataFolder(), File.separator + "lang" + File.separator + iso + File.separator + "strings.yml");
File langFile_new = new File(plugin.getDataFolder(), File.separator + "lang" + File.separator + iso + File.separator + "strings_new.yml"); File langFile_new = new File(plugin.getDataFolder(), File.separator + "lang" + File.separator + iso + File.separator + "strings_new.yml");
if (langFile.exists()) { LinkedHashMap<String, String> allStrings = new LinkedHashMap<String, String>();
LinkedHashMap<String, String> allStrings = new LinkedHashMap<String, String>(); FileConfiguration config = new YamlConfiguration();
FileConfiguration config = new YamlConfiguration(); FileConfiguration config_new = new YamlConfiguration();
FileConfiguration config_new = new YamlConfiguration(); if (langFile.exists() && langFile_new.exists()) {
config_new.options().header("Below are the new strings for your current version of Quests! Transfer them to the strings.yml of the"
+ " same folder to stay up-to-date and suppress console warnings.");
config_new.options().copyHeader(true);
config = loadYamlUTF8(langFile); config = loadYamlUTF8(langFile);
config_new = loadYamlUTF8(langFile_new); config_new = loadYamlUTF8(langFile_new);
//Load user's lang file and determine new strings //Load user's lang file and determine new strings
@ -105,15 +102,19 @@ public class Lang {
+ " You must transfer them to, or regenerate, strings.yml to remove this warning!"); + " You must transfer them to, or regenerate, strings.yml to remove this warning!");
} }
} }
config_new.options().header("Below are any new strings for your current version of Quests! Transfer them to the strings.yml of the"
+ " same folder to stay up-to-date and suppress console warnings.");
config_new.options().copyHeader(true);
config_new.save(langFile_new); config_new.save(langFile_new);
langMap.putAll(allStrings); langMap.putAll(allStrings);
} else { } else {
plugin.getLogger().severe("Failed loading /lang/" + iso + "/strings.yml because the file was not found. Using default en-US"); plugin.getLogger().severe("Failed loading lang files for " + iso + " because they were not found. Using default en-US");
if (!iso.equals("en-US")) { iso = "en-US";
iso = "en-US"; config.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(plugin.getResource("strings.yml"), "UTF-8"))); //TODO better than loadYamlUTF*() ?
loadLang(); for (String key : config.getKeys(false)) {
allStrings.put(key, config.getString(key));
} }
return; langMap.putAll(allStrings);
} }
plugin.getLogger().info("Loaded language " + iso + ". Translations via Crowdin"); plugin.getLogger().info("Loaded language " + iso + ". Translations via Crowdin");
} }