Move utils to I18NUtils

This commit is contained in:
Dreeam 2024-05-29 20:34:27 +08:00
parent c53f622e94
commit 89817d420c
No known key found for this signature in database
GPG Key ID: 0998F8AFD8F667AB
4 changed files with 116 additions and 100 deletions

View File

@ -8,7 +8,7 @@ import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.util.FileUtils;
import fr.xephi.authme.util.PlayerUtils;
import fr.xephi.authme.util.message.I18NUtils;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@ -122,7 +122,7 @@ public abstract class AbstractMessageFileHandler implements Reloadable {
i18nConfiguration = new ConcurrentHashMap<>();
}
locale = PlayerUtils.LocaleToCode(locale, settings);
locale = I18NUtils.localeToCode(locale, settings);
if (i18nConfiguration.containsKey(locale)) {
return i18nConfiguration.get(locale);

View File

@ -4,8 +4,8 @@ import com.google.common.collect.ImmutableMap;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.util.PlayerUtils;
import fr.xephi.authme.util.expiring.Duration;
import fr.xephi.authme.util.message.I18NUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -121,7 +121,7 @@ public class Messages {
* @return The message from the file
*/
private String retrieveMessage(MessageKey key, CommandSender sender) {
String locale = PlayerUtils.getLocale(sender);
String locale = I18NUtils.getLocale(sender);
String message = messagesFileHandler.getMessageByLocale(key.getKey(), locale);
String displayName = sender.getName();
if (sender instanceof Player) {

View File

@ -1,14 +1,7 @@
package fr.xephi.authme.util;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
/**
* Player utilities.
*/
@ -19,10 +12,6 @@ public final class PlayerUtils {
}
private static final boolean IS_LEAVES_SERVER = Utils.isClassLoaded("top.leavesmc.leaves.LeavesConfig");
private static final List<String> LOCALE_LIST = Arrays.asList(
"en", "bg", "de", "eo", "es", "et", "eu", "fi", "fr", "gl", "hu", "id", "it", "ja", "ko", "lt", "nl", "pl",
"pt", "ro", "ru", "sk", "sr", "tr", "uk"
);
/**
* Returns the IP of the given player.
@ -47,89 +36,4 @@ public final class PlayerUtils {
return player.hasMetadata("NPC");
}
}
/**
* Returns the locale that player uses.
*
* @param sender The player
*/
public static String getLocale(CommandSender sender) {
String locale = null;
if (sender instanceof Player) {
Player player = (Player) sender;
if (Utils.majorVersion >= 12) {
locale = player.getLocale().toLowerCase();
} else {
try {
Method spigotMethod = player.getClass().getMethod("spigot");
Object spigot = spigotMethod.invoke(player);
Method spigotGetLocaleMethod = spigot.getClass().getMethod("getLocale");
locale = ((String) spigotGetLocaleMethod.invoke(spigot)).toLowerCase();
} catch (Exception ignored) {
}
}
}
return locale;
}
/**
* Returns the AuthMe messages file language code, by given locale and settings.
* Dreeam - Hard mapping, based on mc1.20.6, 5/29/2024
*
* @param locale The locale that player client setting uses.
* @param settings The AuthMe settings, for default/fallback language usage.
*/
public static String LocaleToCode(String locale, Settings settings) {
// Certain locale code to AuthMe language code redirect
if (!settings.getProperty(PluginSettings.I18N_CODE_REDIRECT).isEmpty()) {
for (String raw : settings.getProperty(PluginSettings.I18N_CODE_REDIRECT)) {
String[] split = raw.split(":");
if (locale.equalsIgnoreCase(split[0])) {
return split[1];
}
}
}
// Match certain locale code
switch (locale) {
case "pt_br":
return "br";
case "cs_cz":
return "cz";
case "nds_de":
case "sxu":
case "swg":
return "de";
case "rpr":
return "ru";
case "sl_si":
return "si";
case "vi_vn":
return "vn";
case "lzh":
case "zh_cn":
return "zhcn";
case "zh_hk":
return "zhhk";
case "zh_tw":
return "zhtw";
//case "zhmc":
// return "zhmc";
}
if (locale.contains("_")) {
locale = locale.substring(0, locale.indexOf("_"));
}
// Match locale code with "_"
if (LOCALE_LIST.contains(locale)) {
return locale;
}
return settings.getProperty(PluginSettings.MESSAGES_LANGUAGE);
}
}

View File

@ -0,0 +1,112 @@
package fr.xephi.authme.util.message;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.util.Utils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
public class I18NUtils {
private static Method spigotMethod;
private static final List<String> LOCALE_LIST = Arrays.asList(
"en", "bg", "de", "eo", "es", "et", "eu", "fi", "fr", "gl", "hu", "id", "it", "ja", "ko", "lt", "nl", "pl",
"pt", "ro", "ru", "sk", "sr", "tr", "uk"
);
static {
try {
spigotMethod = Player.class.getMethod("spigot");
} catch (NoSuchMethodException e) {
spigotMethod = null;
}
}
/**
* Returns the locale that player uses.
*
* @param sender The player
*/
public static String getLocale(CommandSender sender) {
String locale = null;
if (sender instanceof Player) {
Player player = (Player) sender;
if (Utils.majorVersion >= 12) {
locale = player.getLocale().toLowerCase();
} else {
try {
Object spigot = spigotMethod.invoke(player);
Method spigotGetLocaleMethod = spigot.getClass().getMethod("getLocale");
locale = ((String) spigotGetLocaleMethod.invoke(spigot)).toLowerCase();
} catch (Exception ignored) {
}
}
}
return locale;
}
/**
* Returns the AuthMe messages file language code, by given locale and settings.
* Dreeam - Hard mapping, based on mc1.20.6, 5/29/2024
*
* @param locale The locale that player client setting uses.
* @param settings The AuthMe settings, for default/fallback language usage.
*/
public static String localeToCode(String locale, Settings settings) {
// Certain locale code to AuthMe language code redirect
if (!settings.getProperty(PluginSettings.I18N_CODE_REDIRECT).isEmpty()) {
for (String raw : settings.getProperty(PluginSettings.I18N_CODE_REDIRECT)) {
String[] split = raw.split(":");
if (locale.equalsIgnoreCase(split[0])) {
return split[1];
}
}
}
// Match certain locale code
switch (locale) {
case "pt_br":
return "br";
case "cs_cz":
return "cz";
case "nds_de":
case "sxu":
case "swg":
return "de";
case "rpr":
return "ru";
case "sl_si":
return "si";
case "vi_vn":
return "vn";
case "lzh":
case "zh_cn":
return "zhcn";
case "zh_hk":
return "zhhk";
case "zh_tw":
return "zhtw";
//case "zhmc":
// return "zhmc";
}
if (locale.contains("_")) {
locale = locale.substring(0, locale.indexOf("_"));
}
// Match locale code with "_"
if (LOCALE_LIST.contains(locale)) {
return locale;
}
return settings.getProperty(PluginSettings.MESSAGES_LANGUAGE);
}
}