Intial i18n messages support

Send different languages of messages based on player's client setting
This commit is contained in:
Dreeam
2024-05-29 00:11:25 +08:00
parent de69919c01
commit 2219dd55dd
5 changed files with 88 additions and 1 deletions
@@ -14,6 +14,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.File;
import java.util.concurrent.ConcurrentHashMap;
import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_LANGUAGE;
@@ -33,6 +34,7 @@ public abstract class AbstractMessageFileHandler implements Reloadable {
private String filename;
private FileConfiguration configuration;
private ConcurrentHashMap<String, FileConfiguration> i18nConfiguration;
private final String defaultFile;
protected AbstractMessageFileHandler() {
@@ -46,6 +48,7 @@ public abstract class AbstractMessageFileHandler implements Reloadable {
filename = createFilePath(language);
File messagesFile = initializeFile(filename);
configuration = YamlConfiguration.loadConfiguration(messagesFile);
i18nConfiguration = null;
}
protected String getLanguage() {
@@ -83,6 +86,24 @@ public abstract class AbstractMessageFileHandler implements Reloadable {
: message;
}
/**
* Returns the i18n message for the given key and given locale.
*
* @param key the key to retrieve the message for
* @param locale the locale that player client setting uses
* @return the message
*/
public String getMessageByLocale(String key, String locale) {
if (locale == null || !settings.getProperty(PluginSettings.I18N_MESSAGES)) {
return getMessage(key);
}
String message = getI18nConfiguration(locale).getString(key);
return message == null
? "Error retrieving message '" + key + "'"
: message;
}
/**
* Returns the message for the given key only if it exists,
* i.e. without falling back to the default file.
@@ -94,6 +115,25 @@ public abstract class AbstractMessageFileHandler implements Reloadable {
return configuration.getString(key);
}
public FileConfiguration getI18nConfiguration(String locale) {
if (i18nConfiguration == null) {
i18nConfiguration = new ConcurrentHashMap<>();
}
if (i18nConfiguration.containsKey(locale)) {
return i18nConfiguration.get(locale);
} else {
// Sync with reload();
String i18nFilename = createFilePath(locale);
File i18nMessagesFile = initializeFile(i18nFilename);
FileConfiguration config = YamlConfiguration.loadConfiguration(i18nMessagesFile);
i18nConfiguration.put(locale, config);
return config;
}
}
/**
* Creates the path to the messages file for the given language code.
*
@@ -4,6 +4,7 @@ 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 org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
@@ -120,7 +121,8 @@ public class Messages {
* @return The message from the file
*/
private String retrieveMessage(MessageKey key, CommandSender sender) {
String message = messagesFileHandler.getMessage(key.getKey());
String locale = PlayerUtils.getLocale(sender);
String message = messagesFileHandler.getMessageByLocale(key.getKey(), locale);
String displayName = sender.getName();
if (sender instanceof Player) {
displayName = ((Player) sender).getDisplayName();