From dbd8dded42f65fbf8fc2cc3f941325b7841cfc44 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 7 Feb 2017 21:27:32 +0200 Subject: [PATCH] Added Locale grab from github - initLocale() checks for locale files and checks for external locale if setting is not default/files are not found --- .../main/java/com/djrapitops/plan/Plan.java | 73 ++++++++++++++++--- .../java/com/djrapitops/plan/Settings.java | 5 +- Plan/src/main/resources/config.yml | 1 + 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/Plan.java b/Plan/src/main/java/com/djrapitops/plan/Plan.java index 31238e359..8a370fd4f 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/src/main/java/com/djrapitops/plan/Plan.java @@ -9,6 +9,12 @@ import com.djrapitops.plan.database.databases.*; import com.djrapitops.plan.data.cache.*; import com.djrapitops.plan.data.listeners.*; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; import java.util.Date; import main.java.com.djrapitops.plan.ui.webserver.WebSocketServer; import org.bukkit.plugin.java.JavaPlugin; @@ -60,17 +66,9 @@ public class Plan extends JavaPlugin { */ @Override public void onEnable() { - File dataFolder = getDataFolder(); - dataFolder.mkdirs(); + getDataFolder().mkdirs(); - File localeFile = new File(dataFolder, "locale.txt"); - if (localeFile.exists()) { - Phrase.loadLocale(localeFile); - } - File htmlLocale = new File(dataFolder, "htmlLocale.txt"); - if (htmlLocale.exists()) { - Html.loadLocale(htmlLocale); - } + initLocale(); databases = new HashSet<>(); databases.add(new MySQLDB(this)); @@ -313,4 +311,59 @@ public class Plan extends JavaPlugin { public int getBootAnalysisTaskID() { return bootAnalysisTaskID; } + + private void initLocale() { + String locale = Settings.LOCALE.toString().toUpperCase(); + File localeFile = new File(getDataFolder(), "locale.txt"); + File htmlLocale = new File(getDataFolder(), "htmlLocale.txt"); + boolean skipLoc = false; + boolean skipHtmlLoc = false; + String usingLocale = ""; + if (localeFile.exists()) { + Phrase.loadLocale(localeFile); + skipLoc = true; + usingLocale = "locale.txt"; + } + if (htmlLocale.exists()) { + Html.loadLocale(htmlLocale); + skipHtmlLoc = true; + } + if (!locale.equals("DEFAULT")) { + try { + if (!skipLoc) { + URL localeURL = new URL("https://raw.githubusercontent.com/Rsl1122/Plan-PlayerAnalytics/master/Plan/localization/locale_" + locale + ".txt"); + InputStream inputStream = localeURL.openStream(); + OutputStream outputStream = new FileOutputStream(localeFile); + int read = 0; + byte[] bytes = new byte[1024]; + while ((read = inputStream.read(bytes)) != -1) { + outputStream.write(bytes, 0, read); + } + Phrase.loadLocale(localeFile); + usingLocale = locale; + localeFile.delete(); + } + if (!skipHtmlLoc) { + URL localeURL = new URL("https://raw.githubusercontent.com/Rsl1122/Plan-PlayerAnalytics/master/Plan/localization/htmlLocale_" + locale + ".txt"); + InputStream inputStream = localeURL.openStream(); + OutputStream outputStream = new FileOutputStream(htmlLocale); + int read = 0; + byte[] bytes = new byte[1024]; + while ((read = inputStream.read(bytes)) != -1) { + outputStream.write(bytes, 0, read); + } + Html.loadLocale(htmlLocale); + htmlLocale.delete(); + } + } catch (FileNotFoundException ex) { + logError("Attempted using locale that doesn't exist."); + usingLocale = "Default: EN"; + } catch (IOException e) { + e.printStackTrace(); + } + } else { + usingLocale = "Default: EN"; + } + log("Using locale: " + usingLocale); + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/Settings.java b/Plan/src/main/java/com/djrapitops/plan/Settings.java index a076f8d10..b3acdfba4 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Settings.java +++ b/Plan/src/main/java/com/djrapitops/plan/Settings.java @@ -30,9 +30,10 @@ public enum Settings { DEM_TRIGGERS("Customization.DemographicsTriggers.Trigger"), DEM_FEMALE("Customization.DemographicsTriggers.Female"), DEM_MALE("Customization.DemographicsTriggers.Male"), - DEM_IGNORE("Customization.DemographicsTriggers.IgnoreWhen"),; + DEM_IGNORE("Customization.DemographicsTriggers.IgnoreWhen"), + LOCALE("Settings.Locale"); - private String configPath; + private final String configPath; private Settings(String path) { this.configPath = path; diff --git a/Plan/src/main/resources/config.yml b/Plan/src/main/resources/config.yml index 98647d1ba..854a2e053 100644 --- a/Plan/src/main/resources/config.yml +++ b/Plan/src/main/resources/config.yml @@ -1,4 +1,5 @@ Settings: + Locale: default Data: GatherLocations: true Analysis: