Draft of Caption resolving

This commit is contained in:
Hannes Greule 2020-06-29 21:33:57 +02:00
parent 600f757046
commit f80cadcd7f
9 changed files with 258 additions and 4 deletions

View File

@ -28,13 +28,16 @@ package com.plotsquared.core;
import com.plotsquared.core.command.WE_Anywhere;
import com.plotsquared.core.components.ComponentPresetManager;
import com.plotsquared.core.configuration.Caption;
import com.plotsquared.core.configuration.caption.CaptionMap;
import com.plotsquared.core.configuration.CaptionUtility;
import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.ConfigurationSection;
import com.plotsquared.core.configuration.ConfigurationUtil;
import com.plotsquared.core.configuration.caption.LegacyCaptionMap;
import com.plotsquared.core.configuration.MemorySection;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.Storage;
import com.plotsquared.core.configuration.caption.CaptionLoader;
import com.plotsquared.core.configuration.file.YamlConfiguration;
import com.plotsquared.core.configuration.serialization.ConfigurationSerialization;
import com.plotsquared.core.database.DBFunc;
@ -107,6 +110,7 @@ import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.sql.SQLException;
import java.util.ArrayDeque;
@ -164,6 +168,8 @@ public class PlotSquared {
public HashMap<String, Set<PlotCluster>> clusters_tmp;
public HashMap<String, HashMap<PlotId, Plot>> plots_tmp;
private YamlConfiguration config;
// Localization
@Getter private CaptionMap captionMap;
// Implementation logger
@Setter @Getter private ILogger logger;
// Platform / Version / Update URL
@ -219,10 +225,23 @@ public class PlotSquared {
if (!setupConfigs()) {
return;
}
this.translationFile = MainUtil.getFile(this.IMP.getDirectory(),
Settings.Paths.TRANSLATIONS + File.separator + IMP.getPluginName()
+ ".use_THIS.yml");
Captions.load(this.translationFile);
// Setup localization
CaptionMap captionMap;
if (Settings.Enabled_Components.PER_USER_LOCALE) {
captionMap = CaptionLoader.loadAll(Paths.get("lang"));
} else {
String fileName = "messages_" + Settings.Enabled_Components.DEFAULT_LOCALE + ".json";
captionMap = CaptionLoader.loadSingle(Paths.get("lang", fileName));
}
if (Settings.Enabled_Components.LEGACY_MESSAGES) {
this.translationFile = MainUtil.getFile(this.IMP.getDirectory(),
Settings.Paths.TRANSLATIONS + File.separator + IMP.getPluginName()
+ ".use_THIS.yml");
Captions.load(this.translationFile);
captionMap = new LegacyCaptionMap(captionMap);
}
this.captionMap = captionMap;
// Setup plotAreaManager
if (Settings.Enabled_Components.WORLDS) {

View File

@ -573,6 +573,10 @@ public class Settings extends Config {
PERSISTENT_ROAD_REGEN = false;
@Comment({"Enable the `/plot component` preset GUI",
"Read more about components here: https://wiki.intellectualsites.com/en/plotsquared/installation/plot-components"}) public static boolean COMPONENT_PRESETS = true;
@Comment({"Use legacy messages (will be removed in future)",
"It is recommended to use the new message system"}) public static boolean LEGACY_MESSAGES = true;
@Comment("Enable per user locale") public static boolean PER_USER_LOCALE = false;
@Comment("The default locale") public static String DEFAULT_LOCALE = "en";
@Comment("Use UUID cache to complete usernames")
public static boolean EXTENDED_USERNAME_COMPLETION = true;
@Comment("Command aliases that will be tab completed")

View File

@ -0,0 +1,60 @@
package com.plotsquared.core.configuration.caption;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class CaptionLoader {
private static final Gson GSON = new GsonBuilder().create();
private static final Pattern FILE_NAME_PATTERN = Pattern.compile("messages_(.*)\\.json");
private CaptionLoader() {
}
public static CaptionMap loadAll(@NotNull final Path directory) throws IOException {
final Map<Locale, CaptionMap> localeMaps = new HashMap<>();
try (Stream<Path> files = Files.list(directory)) {
List<Path> captionFiles = files.filter(Files::isRegularFile).collect(Collectors.toList());
for (Path file : captionFiles) {
CaptionMap localeMap = loadSingle(file);
localeMaps.put(localeMap.getLocale(), localeMap);
}
return new PerUserLocaleCaptionMap(localeMaps);
}
}
public static CaptionMap loadSingle(@NotNull final Path file) throws IOException {
final String fileName = file.getFileName().toString();
final Matcher matcher = FILE_NAME_PATTERN.matcher(fileName);
final Locale locale;
if (matcher.matches()) {
locale = Locale.forLanguageTag(matcher.group(1));
} else {
throw new IllegalArgumentException(fileName + " is an invalid message file (cannot extract locale)");
}
JsonObject object = GSON.fromJson(
Files.newBufferedReader(file, StandardCharsets.UTF_16),
JsonObject.class);
Map<TranslatableCaption, String> captions = new HashMap<>();
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
TranslatableCaption key = TranslatableCaption.of(entry.getKey());
captions.put(key, entry.getValue().getAsString());
}
return new LocalizedCaptionMap(locale, captions);
}
}

View File

@ -0,0 +1,16 @@
package com.plotsquared.core.configuration.caption;
import com.plotsquared.core.player.PlotPlayer;
import java.util.Locale;
public interface CaptionMap {
String getMessage(TranslatableCaption caption);
String getMessage(TranslatableCaption caption, PlotPlayer<?> context);
boolean supportsLocale(Locale locale);
Locale getLocale();
}

View File

@ -0,0 +1,52 @@
package com.plotsquared.core.configuration.caption;
import com.plotsquared.core.configuration.Caption;
import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.player.PlotPlayer;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@Deprecated
public class LegacyCaptionMap implements CaptionMap {
private final CaptionMap parent;
private final Map<String, Caption> legacyMap;
public LegacyCaptionMap(CaptionMap parent) {
this.parent = parent;
this.legacyMap = new HashMap<>();
// add here custom mappings
}
@Override
public String getMessage(TranslatableCaption caption) {
Caption legacy = this.legacyMap.computeIfAbsent(caption.getKey(), key -> {
String captionsName = key.substring(key.indexOf('.') + 1).toUpperCase(Locale.ROOT);
try {
return Captions.valueOf(captionsName);
} catch (IllegalArgumentException ignored) {
return null;
}
});
if (legacy == null) {
return this.parent.getMessage(caption);
}
return legacy.getTranslated();
}
@Override
public String getMessage(TranslatableCaption caption, PlotPlayer<?> context) {
return getMessage(caption); // Captions does not allow per player locale
}
@Override
public boolean supportsLocale(Locale locale) {
return false;
}
@Override
public Locale getLocale() {
return Locale.ROOT;
}
}

View File

@ -0,0 +1,32 @@
package com.plotsquared.core.configuration.caption;
import com.plotsquared.core.player.PlotPlayer;
import java.util.Locale;
import java.util.Map;
public class LocalizedCaptionMap implements CaptionMap {
private final Locale locale;
private final Map<TranslatableCaption, String> captions;
public LocalizedCaptionMap(Locale locale, Map<TranslatableCaption, String> captions) {
this.locale = locale;
this.captions = captions;
}
@Override public String getMessage(TranslatableCaption caption) {
return this.captions.get(caption);
}
@Override public String getMessage(TranslatableCaption caption, PlotPlayer<?> context) {
return getMessage(caption); // use the translation of this locale
}
@Override public boolean supportsLocale(Locale locale) {
return this.locale.equals(locale);
}
@Override public Locale getLocale() {
return this.locale;
}
}

View File

@ -0,0 +1,26 @@
package com.plotsquared.core.configuration.caption;
import com.plotsquared.core.player.PlotPlayer;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
public class PerUserLocaleCaptionMap extends LocalizedCaptionMap {
private final Map<Locale, CaptionMap> localeMap;
public PerUserLocaleCaptionMap(Map<Locale, CaptionMap> localeMap) {
super(Locale.ROOT, Collections.emptyMap());
this.localeMap = localeMap;
}
@Override
public String getMessage(TranslatableCaption caption, PlotPlayer<?> context) {
return this.localeMap.get(context.getLocale()).getMessage(caption);
}
@Override
public boolean supportsLocale(Locale locale) {
return this.localeMap.containsKey(locale);
}
}

View File

@ -0,0 +1,28 @@
package com.plotsquared.core.configuration.caption;
import com.plotsquared.core.configuration.Caption;
import org.jetbrains.annotations.NotNull;
public final class TranslatableCaption implements Caption {
@NotNull private final String key;
private TranslatableCaption(@NotNull String key) {
this.key = key;
}
public static TranslatableCaption of(@NotNull final String key) {
return new TranslatableCaption(key);
}
@Override public String getTranslated() {
return null;
}
@Override public boolean usePrefix() {
return false;
}
@NotNull public String getKey() {
return this.key;
}
}

View File

@ -61,6 +61,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@ -86,6 +87,7 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer
*/
private ConcurrentHashMap<String, Object> meta;
private int hash;
private Locale locale;
public static <T> PlotPlayer<T> from(@NonNull final T object) {
if (!converters.containsKey(object.getClass())) {
@ -737,6 +739,21 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer
return this.getAttribute("debug");
}
@NotNull public Locale getLocale() {
if (this.locale == null) {
this.locale = Locale.forLanguageTag(Settings.Enabled_Components.DEFAULT_LOCALE);
}
return this.locale;
}
public void setLocale(@NotNull final Locale locale) {
if (!PlotSquared.get().getCaptionMap().supportsLocale(locale)) {
this.locale = Locale.forLanguageTag(Settings.Enabled_Components.DEFAULT_LOCALE);
} else {
this.locale = locale;
}
}
@Override public int hashCode() {
if (this.hash == 0 || this.hash == 485) {
this.hash = 485 + this.getUUID().hashCode();