Add configurable locale code to AuthMe language code redirect

This commit is contained in:
Dreeam 2024-05-29 18:46:57 +08:00
parent d09e545554
commit 6001ee6979
No known key found for this signature in database
GPG Key ID: 0998F8AFD8F667AB
2 changed files with 26 additions and 3 deletions

View File

@ -5,6 +5,9 @@ import ch.jalu.configme.SettingsHolder;
import ch.jalu.configme.properties.Property;
import fr.xephi.authme.output.LogLevel;
import java.util.Set;
import static ch.jalu.configme.properties.PropertyInitializer.newLowercaseStringSetProperty;
import static ch.jalu.configme.properties.PropertyInitializer.newProperty;
public final class PluginSettings implements SettingsHolder {
@ -23,6 +26,18 @@ public final class PluginSettings implements SettingsHolder {
public static final Property<Boolean> I18N_MESSAGES =
newProperty("3rdPartyFeature.features.i18nMessages.enabled", false);
@Comment({"Redirect locale code to certain AuthMe language code as you want",
"Minecraft locale list: https://minecraft.wiki/w/Language",
"AuthMe language code: https://github.com/HaHaWTH/AuthMeReReloaded/blob/master/docs/translations.md",
"For example, if you want to show Russian messages to player using language Tatar(tt_ru),",
"and show Chinese Simplified messages to player using language Classical Chinese(lzh), then:",
"locale-code-redirect:",
"- 'tt_ru:ru'",
"- 'lzh:zhcn'"})
public static final Property<Set<String>> I18N_CODE_REDIRECT =
newLowercaseStringSetProperty("3rdPartyFeature.features.i18nMessages.locale-code-redirect",
"tt_ru:ru", "lzh:zhcn");
@Comment({
"Do you want to enable the session feature?",
"If enabled, when a player authenticates successfully,",

View File

@ -52,14 +52,14 @@ public final class PlayerUtils {
if (sender instanceof Player) {
Player player = (Player) sender;
if (Utils.majorVersion >= 12) {
locale = player.getLocale();
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);
locale = ((String) spigotGetLocaleMethod.invoke(spigot)).toLowerCase();
} catch (Exception ignored) {
}
}
@ -76,7 +76,15 @@ public final class PlayerUtils {
* @param settings The AuthMe settings, for default/fallback language usage.
*/
public static String LocaleToCode(String locale, Settings settings) {
locale = locale.toLowerCase();
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];
}
}
}
switch (locale) {
case "pt_br":