feat: support custom language file
This commit is contained in:
parent
3f297eba49
commit
84e558afb2
4 changed files with 82 additions and 34 deletions
|
@ -105,7 +105,7 @@ public class NickoBukkit extends JavaPlugin {
|
||||||
|
|
||||||
localeManager = new LocaleManager(this);
|
localeManager = new LocaleManager(this);
|
||||||
localeManager.findFallbackLocale();
|
localeManager.findFallbackLocale();
|
||||||
|
localeManager.installCustomLanguageFile();
|
||||||
|
|
||||||
final PluginCommand command = getCommand("nicko");
|
final PluginCommand command = getCommand("nicko");
|
||||||
if (command != null) {
|
if (command != null) {
|
||||||
|
@ -154,6 +154,10 @@ public class NickoBukkit extends JavaPlugin {
|
||||||
|
|
||||||
public PlayerDataStore getDataStore() { return dataStore; }
|
public PlayerDataStore getDataStore() { return dataStore; }
|
||||||
|
|
||||||
|
public LocaleManager getLocaleManager() {
|
||||||
|
return localeManager;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isUnitTesting() {
|
public boolean isUnitTesting() {
|
||||||
return unitTesting;
|
return unitTesting;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,16 +13,17 @@ public class I18N {
|
||||||
private final static MessageFormat formatter = new MessageFormat("");
|
private final static MessageFormat formatter = new MessageFormat("");
|
||||||
|
|
||||||
private static Locale getLocale(Player player) {
|
private static Locale getLocale(Player player) {
|
||||||
|
final NickoBukkit instance = NickoBukkit.getInstance();
|
||||||
try {
|
try {
|
||||||
final Optional<NickoProfile> profile = NickoBukkit.getInstance().getDataStore().getData(player.getUniqueId());
|
final Optional<NickoProfile> profile = instance.getDataStore().getData(player.getUniqueId());
|
||||||
if (profile.isEmpty()) {
|
if (profile.isEmpty()) {
|
||||||
return Locale.ENGLISH;
|
return Locale.ENGLISH;
|
||||||
} else {
|
} else {
|
||||||
return profile.get().getLocale();
|
return profile.get().getLocale();
|
||||||
}
|
}
|
||||||
} catch (IllegalArgumentException exception) {
|
} catch (IllegalArgumentException exception) {
|
||||||
NickoBukkit.getInstance().getLogger().severe("Invalid locale provided by " + player.getName() + ", defaulting to " + Locale.getDefault().getCode() + ".");
|
instance.getLogger().severe("Invalid locale provided by " + player.getName() + ", defaulting to " + LocaleManager.getFallback().getCode() + ".");
|
||||||
return Locale.getDefault();
|
return LocaleManager.getFallback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,31 +32,19 @@ public class I18N {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String translate(Player player, I18NDict key, Object... arguments) {
|
public static String translate(Player player, I18NDict key, Object... arguments) {
|
||||||
final Locale locale = getLocale(player);
|
final NickoBukkit instance = NickoBukkit.getInstance();
|
||||||
String translation;
|
final String translation = findTranslation(player, key);
|
||||||
if (locale == Locale.CUSTOM) {
|
|
||||||
translation = "";
|
|
||||||
} else {
|
|
||||||
translation = getBundle(LocaleUtils.toLocale(locale.getCode())).getString(key.key());
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
formatter.applyPattern(translation);
|
formatter.applyPattern(translation);
|
||||||
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + formatter.format(arguments);
|
return instance.getNickoConfig().getPrefix() + formatter.format(arguments);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return NickoBukkit.getInstance().getNickoConfig().getPrefix() + key.key();
|
return instance.getNickoConfig().getPrefix() + key.key();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String translateFlat(Player player, I18NDict key, Object... arguments) {
|
public static String translateFlat(Player player, I18NDict key, Object... arguments) {
|
||||||
final Locale locale = getLocale(player);
|
final String translation = findTranslation(player, key);
|
||||||
String translation;
|
|
||||||
if (locale == Locale.CUSTOM) {
|
|
||||||
translation = "";
|
|
||||||
} else {
|
|
||||||
translation = getBundle(LocaleUtils.toLocale(locale.getCode())).getString(key.key());
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
formatter.applyPattern(translation);
|
formatter.applyPattern(translation);
|
||||||
return formatter.format(arguments);
|
return formatter.format(arguments);
|
||||||
|
@ -63,4 +52,18 @@ public class I18N {
|
||||||
return key.key();
|
return key.key();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static String findTranslation(Player player, I18NDict key) {
|
||||||
|
final NickoBukkit instance = NickoBukkit.getInstance();
|
||||||
|
final Locale locale = getLocale(player);
|
||||||
|
String translation;
|
||||||
|
if (locale == Locale.CUSTOM) {
|
||||||
|
translation = instance.getLocaleManager().getCustomLanguageFile().getProperty(key.key(), key.key());
|
||||||
|
} else {
|
||||||
|
translation = getBundle(LocaleUtils.toLocale(locale.getCode())).getString(key.key());
|
||||||
|
}
|
||||||
|
|
||||||
|
return translation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ public enum Locale implements Serializable {
|
||||||
CUSTOM("custom", "Server Custom");
|
CUSTOM("custom", "Server Custom");
|
||||||
|
|
||||||
private static HashMap<String, Locale> locales;
|
private static HashMap<String, Locale> locales;
|
||||||
private static Locale defaultLocale;
|
|
||||||
|
|
||||||
private final String code;
|
private final String code;
|
||||||
private transient final String name;
|
private transient final String name;
|
||||||
|
@ -31,15 +30,7 @@ public enum Locale implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Locale fromCode(String code) {
|
public static Locale fromCode(String code) {
|
||||||
return getLocales().getOrDefault(code, defaultLocale);
|
return getLocales().getOrDefault(code, LocaleManager.getFallback());
|
||||||
}
|
|
||||||
|
|
||||||
public static void setFallback(Locale defaultLocale) {
|
|
||||||
Locale.defaultLocale = defaultLocale;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Locale getDefault() {
|
|
||||||
return defaultLocale;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCode() {
|
public String getCode() {
|
||||||
|
|
|
@ -2,15 +2,65 @@ package net.artelnatif.nicko.i18n;
|
||||||
|
|
||||||
import net.artelnatif.nicko.NickoBukkit;
|
import net.artelnatif.nicko.NickoBukkit;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
public class LocaleManager {
|
public class LocaleManager {
|
||||||
|
private static Locale fallback;
|
||||||
|
|
||||||
private final NickoBukkit instance;
|
private final NickoBukkit instance;
|
||||||
private final String[] supportedLocales = new String[]{"en", "fr", "custom"};
|
private final String[] supportedLocales = new String[]{"en", "fr", "custom"};
|
||||||
|
private final File directory = new File(NickoBukkit.getInstance().getDataFolder() + "/lang/");
|
||||||
|
private final File file = new File(directory, "custom.properties");
|
||||||
|
|
||||||
|
private Properties customLanguageFile;
|
||||||
|
|
||||||
public LocaleManager(NickoBukkit instance) {
|
public LocaleManager(NickoBukkit instance) {
|
||||||
this.instance = instance;
|
this.instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setFallback(Locale fallback) {
|
||||||
|
LocaleManager.fallback = fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Locale getFallback() {
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void installCustomLanguageFile() {
|
||||||
|
final InputStream resource = instance.getResource("locale_en.properties");
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (resource != null) {
|
||||||
|
if (!file.exists()) {
|
||||||
|
instance.getLogger().info("Installed Custom language file as \"custom.properties\"");
|
||||||
|
Files.copy(resource, file.toPath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO: 12/19/22 Handle error
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Properties getCustomLanguageFile() {
|
||||||
|
if (customLanguageFile == null) {
|
||||||
|
final Properties properties = new Properties();
|
||||||
|
try {
|
||||||
|
properties.load(new BufferedInputStream(new FileInputStream(file)));
|
||||||
|
return customLanguageFile = properties;
|
||||||
|
} catch (IOException e) {
|
||||||
|
instance.getLogger().warning("Couldn't load Custom properties language file, falling back to English.");
|
||||||
|
if (LocaleManager.getFallback() != Locale.ENGLISH) {
|
||||||
|
LocaleManager.setFallback(Locale.ENGLISH);
|
||||||
|
}
|
||||||
|
return customLanguageFile = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return customLanguageFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void findFallbackLocale() {
|
public void findFallbackLocale() {
|
||||||
|
@ -18,15 +68,15 @@ public class LocaleManager {
|
||||||
try {
|
try {
|
||||||
if (Arrays.stream(supportedLocales).noneMatch(s -> s.equalsIgnoreCase(locale))) {
|
if (Arrays.stream(supportedLocales).noneMatch(s -> s.equalsIgnoreCase(locale))) {
|
||||||
instance.getLogger().severe(locale + " is not a supported locale, defaulting to English.");
|
instance.getLogger().severe(locale + " is not a supported locale, defaulting to English.");
|
||||||
Locale.setFallback(Locale.ENGLISH);
|
LocaleManager.setFallback(Locale.ENGLISH);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final Locale defaultLocale = Locale.fromCode(locale);
|
final Locale defaultLocale = Locale.fromCode(locale);
|
||||||
instance.getLogger().info("Fallback locale set to " + defaultLocale.getName() + ".");
|
instance.getLogger().info("Fallback locale set to " + defaultLocale.getName() + ".");
|
||||||
Locale.setFallback(defaultLocale);
|
LocaleManager.setFallback(defaultLocale);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
instance.getLogger().severe(locale + " is not a valid locale, defaulting to English.");
|
instance.getLogger().severe(locale + " is not a valid locale, defaulting to English.");
|
||||||
Locale.setFallback(Locale.ENGLISH);
|
LocaleManager.setFallback(Locale.ENGLISH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue