Update and expose language setting

This commit is contained in:
PikaMug 2022-06-13 03:25:05 -04:00
parent fae968c3e6
commit 5c66d01d7c
6 changed files with 77 additions and 54 deletions

View File

@ -41,6 +41,8 @@ public interface ISettings {
void setIgnoreLockedQuests(final boolean ignoreLockedQuests);
int getKillDelay();
void setKillDelay(final int killDelay);
String getLanguage();
void setLanguage(final String language);
int getMaxQuests();
void setMaxQuests(final int maxQuests);
boolean canNpcEffects();

View File

@ -37,17 +37,17 @@ import java.util.regex.Pattern;
public class Lang {
private static String iso = "en-US";
//private static String iso = "en-US";
private static final LinkedHashMap<String, String> langMap = new LinkedHashMap<>();
private static final Pattern hexPattern = Pattern.compile("(?i)%#([0-9A-F]{6})%");
public static String getISO() {
/*public static String getISO() {
return iso;
}
public static void setISO(final String iso) {
Lang.iso = iso;
}
}*/
public static Collection<String> values() {
return langMap.values();
@ -61,6 +61,14 @@ public class Lang {
* @return formatted string, plus processing through PlaceholderAPI by clip
*/
public static String get(final Player player, final String key) {
String locale;
try {
locale = player.getLocale();
} catch (NoSuchMethodError e) {
// Older version of Minecraft
locale = player.spigot().getLocale();
}
return langMap.containsKey(key) ? LangToken.convertString(player, langMap.get(key)) : "NULL";
}
@ -133,14 +141,33 @@ public class Lang {
}
}
public static void init(final QuestsAPI plugin) throws InvalidConfigurationException, IOException {
public static void init(final QuestsAPI plugin, String iso) throws InvalidConfigurationException, IOException {
final File langFile = new File(plugin.getPluginDataFolder(), File.separator + "lang" + File.separator + iso + File.separator
+ "strings.yml");
final File langFile_new = new File(plugin.getPluginDataFolder(), File.separator + "lang" + File.separator + iso
+ File.separator + "strings_new.yml");
final boolean exists_new = langFile_new.exists();
final LinkedHashMap<String, String> allStrings = new LinkedHashMap<>();
if (langFile.exists() && iso.split("-").length > 1) {
if (!(langFile.exists() && iso.split("-").length > 1)) {
plugin.getPluginLogger().severe("Failed loading lang files for " + iso
+ " because they were not found. Using default en-US");
plugin.getPluginLogger()
.info("If the plugin has not generated language files, ensure Quests has write permissions");
plugin.getPluginLogger()
.info("For help, visit https://github.com/PikaMug/Quests/wiki/Casual-%E2%80%90-Translations");
plugin.getSettings().setLanguage("en-US");
if (plugin.getSettings().getConsoleLogging() > 3) {
plugin.getPluginLogger().info("CodeSource: " + plugin.getClass().getProtectionDomain().getCodeSource()
.toString());
plugin.getPluginLogger().info("LocationPath: " + plugin.getClass().getProtectionDomain().getCodeSource()
.getLocation().getPath());
}
final FileConfiguration config = YamlConfiguration.loadConfiguration(new InputStreamReader(Objects
.requireNonNull(plugin.getPluginResource("strings.yml")), StandardCharsets.UTF_8));
for (final String key : config.getKeys(false)) {
allStrings.put(key, config.getString(key));
}
}
final FileConfiguration config= YamlConfiguration
.loadConfiguration(new InputStreamReader(new FileInputStream(langFile), StandardCharsets.UTF_8));
FileConfiguration config_new = null;
@ -172,24 +199,6 @@ public class Lang {
config_new.options().copyHeader(true);
config_new.save(langFile_new);
}
} else {
plugin.getPluginLogger().severe("Failed loading lang files for " + iso
+ " because they were not found. Using default en-US");
plugin.getPluginLogger()
.info("If the plugin has not generated language files, ensure Quests has write permissions");
plugin.getPluginLogger()
.info("For help, visit https://github.com/PikaMug/Quests/wiki/Casual-%E2%80%90-Translations");
iso = "en-US";
plugin.getPluginLogger().info("CodeSource: " + plugin.getClass().getProtectionDomain().getCodeSource()
.toString());
plugin.getPluginLogger().info("LocationPath: " + plugin.getClass().getProtectionDomain().getCodeSource()
.getLocation().getPath());
final FileConfiguration config = YamlConfiguration.loadConfiguration(new InputStreamReader(Objects
.requireNonNull(plugin.getPluginResource("strings.yml")), StandardCharsets.UTF_8));
for (final String key : config.getKeys(false)) {
allStrings.put(key, config.getString(key));
}
}
final String cmdAdd = allStrings.get("cmdAdd");
final String cmdClear = allStrings.get("cmdClear");

View File

@ -37,10 +37,10 @@ import me.blackvein.quests.exceptions.QuestFormatException;
import me.blackvein.quests.exceptions.StageFormatException;
import me.blackvein.quests.interfaces.ReloadCallback;
import me.blackvein.quests.listeners.BlockListener;
import me.blackvein.quests.listeners.CitizensListener;
import me.blackvein.quests.listeners.CommandManager;
import me.blackvein.quests.listeners.ConvoListener;
import me.blackvein.quests.listeners.ItemListener;
import me.blackvein.quests.listeners.CitizensListener;
import me.blackvein.quests.listeners.PartiesListener;
import me.blackvein.quests.listeners.PlayerListener;
import me.blackvein.quests.listeners.UniteListener;
@ -203,11 +203,13 @@ public class Quests extends JavaPlugin implements QuestsAPI {
conditionFactory = new BukkitConditionFactory(this);
depends = new Dependencies(this);
trigger = new DenizenTrigger(this);
final Metrics metrics = new Metrics(this);
metrics.addCustomChart(new Metrics.SimplePie("language", Lang::getISO));
// 2 - Load main config
settings.init();
if (settings.getLanguage().contains("-")) {
final Metrics metrics = new Metrics(this);
metrics.addCustomChart(new Metrics.SimplePie("language", () -> settings.getLanguage()));
}
// 3 - Setup language files
try {
@ -761,7 +763,7 @@ public class Quests extends JavaPlugin implements QuestsAPI {
}
/**
* Transfer language files from jar to disk
* Transfer language files from jar to disk, then initialize default
*/
private void setupLang() throws IOException, URISyntaxException {
final String path = "lang";
@ -783,7 +785,7 @@ public class Quests extends JavaPlugin implements QuestsAPI {
jar.close();
}
try {
Lang.init(this);
Lang.init(this, settings.getLanguage());
} catch (final InvalidConfigurationException e) {
e.printStackTrace();
}
@ -1719,7 +1721,7 @@ public class Quests extends JavaPlugin implements QuestsAPI {
try {
Lang.clear();
settings.init();
Lang.init(Quests.this);
Lang.init(Quests.this, settings.getLanguage());
quests.clear();
actions.clear();
conditions.clear();

View File

@ -13,7 +13,6 @@
package me.blackvein.quests;
import me.blackvein.quests.config.ISettings;
import me.blackvein.quests.util.Lang;
import org.bukkit.configuration.file.FileConfiguration;
import java.io.File;
@ -38,6 +37,7 @@ public class Settings implements ISettings {
private boolean giveJournalItem = false;
private boolean ignoreLockedQuests = false;
private int killDelay = 0;
private String language = "en-US";
private int maxQuests = 0;
private boolean npcEffects = true;
private String effect = "note";
@ -153,6 +153,12 @@ public class Settings implements ISettings {
public void setKillDelay(final int killDelay) {
this.killDelay = killDelay;
}
public String getLanguage() {
return language;
}
public void setLanguage(final String language) {
this.language = language;
}
public int getMaxQuests() {
return maxQuests;
}
@ -249,9 +255,9 @@ public class Settings implements ISettings {
killDelay = config.getInt("kill-delay", 600);
if (Objects.requireNonNull(config.getString("language")).equalsIgnoreCase("en")) {
//Legacy
Lang.setISO("en-US");
language = "en-US";
} else {
Lang.setISO(config.getString("language", "en-US"));
language = config.getString("language", "en-US");
}
maxQuests = config.getInt("max-quests", maxQuests);
npcEffects = config.getBoolean("npc-effects.enabled", true);

View File

@ -12,6 +12,7 @@
package me.blackvein.quests.convo.quests.planner;
import me.blackvein.quests.Quests;
import me.blackvein.quests.convo.quests.QuestsEditorNumericPrompt;
import me.blackvein.quests.convo.quests.QuestsEditorStringPrompt;
import me.blackvein.quests.events.editor.quests.QuestsEditorPostOpenNumericPromptEvent;
@ -30,11 +31,14 @@ import java.util.Locale;
import java.util.TimeZone;
public class DateTimePrompt extends QuestsEditorNumericPrompt {
private final Quests plugin;
private final Prompt oldPrompt;
private final String source;
public DateTimePrompt(final ConversationContext context, final Prompt old, final String origin) {
super(context);
this.plugin = (Quests)context.getPlugin();
oldPrompt = old;
source = origin;
}
@ -71,7 +75,7 @@ public class DateTimePrompt extends QuestsEditorNumericPrompt {
dateData += ChatColor.AQUA + timeFormat.format(cal.getTime()) + " ";
cal.setTimeZone(TimeZone.getTimeZone((String) context.getSessionData("tempZone")));
final String[] iso = Lang.getISO().split("-");
final String[] iso = plugin.getSettings().getLanguage().split("-");
final Locale loc = new Locale(iso[0], iso[1]);
final Double zonedHour = (double) (cal.getTimeZone().getRawOffset() / 60 / 60 / 1000);
final String[] sep = String.valueOf(zonedHour).replace("-", "").split("\\.");

View File

@ -408,7 +408,7 @@ public class PlannerPrompt extends QuestsEditorNumericPrompt {
final TimeZone tz = TimeZone.getTimeZone(date[6]);
cal.setTimeZone(tz);
final String[] iso = Lang.getISO().split("-");
final String[] iso = plugin.getSettings().getLanguage().split("-");
final Locale loc = iso.length > 1 ? new Locale(iso[0], iso[1]) : new Locale(iso[0]);
final Double zonehour = (double) (cal.getTimeZone().getRawOffset() / 60 / 60 / 1000);
final String[] sep = String.valueOf(zonehour).replace("-", "").split("\\.");