From af44fe74ffc90cc6ba1abf5f0f10c4f8c3135fba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Fri, 24 Jul 2020 17:19:25 +0200 Subject: [PATCH] Add namespaces for translations --- .../com/plotsquared/core/PlotSquared.java | 35 ++++++++-- .../configuration/caption/CaptionMap.java | 4 +- .../caption/DummyCaptionMap.java | 54 +++++++++++++++ .../caption/LocalizedCaptionMap.java | 3 +- ...yedCaption.java => NamespacedCaption.java} | 11 ++- .../caption/TranslatableCaption.java | 69 +++++++++++++++++-- .../com/plotsquared/core/util/ChunkUtil.java | 25 +++++++ 7 files changed, 185 insertions(+), 16 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/configuration/caption/DummyCaptionMap.java rename Core/src/main/java/com/plotsquared/core/configuration/caption/{KeyedCaption.java => NamespacedCaption.java} (85%) diff --git a/Core/src/main/java/com/plotsquared/core/PlotSquared.java b/Core/src/main/java/com/plotsquared/core/PlotSquared.java index 4f3924e50..bcd9cfcdd 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotSquared.java +++ b/Core/src/main/java/com/plotsquared/core/PlotSquared.java @@ -33,6 +33,9 @@ import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Storage; import com.plotsquared.core.configuration.caption.CaptionLoader; import com.plotsquared.core.configuration.caption.CaptionMap; +import com.plotsquared.core.configuration.caption.DummyCaptionMap; +import com.plotsquared.core.configuration.caption.LocalizedCaptionMap; +import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.configuration.file.YamlConfiguration; import com.plotsquared.core.configuration.serialization.ConfigurationSerialization; import com.plotsquared.core.database.DBFunc; @@ -142,7 +145,7 @@ public class PlotSquared { public HashMap> plots_tmp; private YamlConfiguration config; // Localization - private CaptionMap captionMap; + private Map captionMaps; // Platform / Version / Update URL private PlotVersion version; // Files and configuration @@ -194,6 +197,8 @@ public class PlotSquared { return; } + this.captionMaps = new HashMap<>(); + // Setup localization CaptionMap captionMap; if (Settings.Enabled_Components.PER_USER_LOCALE) { @@ -202,8 +207,8 @@ public class PlotSquared { String fileName = "messages_" + Settings.Enabled_Components.DEFAULT_LOCALE + ".json"; captionMap = CaptionLoader.loadSingle(Paths.get("lang", fileName)); } - this.captionMap = captionMap; + this.captionMaps.put(TranslatableCaption.DEFAULT_NAMESPACE, captionMap); this.worldedit = WorldEdit.getInstance(); // Create Event utility class @@ -1471,8 +1476,30 @@ public class PlotSquared { return this.worldConfiguration; } - public CaptionMap getCaptionMap() { - return this.captionMap; + /** + * Get the caption map belonging to a namespace. If none exists, a dummy + * caption map will be returned. + * + * @param namespace Namespace + * @return Map instance + * @see #registerCaptionMap(String, CaptionMap) To register a caption map + */ + @Nonnull public CaptionMap getCaptionMap(@Nonnull final String namespace) { + return this.captionMaps.computeIfAbsent(namespace.toLowerCase(Locale.ENGLISH), + missingNamespace -> new DummyCaptionMap()); + } + + /** + * Register a caption map + * + * @param namespace Namespace + * @param captionMap Map instance + */ + public void registerCaptionMap(@Nonnull final String namespace, @Nonnull final CaptionMap captionMap) { + if (namespace.equalsIgnoreCase(TranslatableCaption.DEFAULT_NAMESPACE)) { + throw new IllegalArgumentException("Cannot replace default caption map"); + } + this.captionMaps.put(namespace.toLowerCase(Locale.ENGLISH), captionMap); } public File getJarFile() { diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionMap.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionMap.java index 1b3efbbb8..70cf571b6 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionMap.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/CaptionMap.java @@ -51,7 +51,7 @@ public interface CaptionMap { * @return Component * @throws NoSuchCaptionException if no caption with the given key exists */ - String getMessage(@Nonnull TranslatableCaption caption, @Nonnull LocaleHolder localeHolder) throws NoSuchCaptionException; + @Nonnull String getMessage(@Nonnull TranslatableCaption caption, @Nonnull LocaleHolder localeHolder) throws NoSuchCaptionException; /** * Check if the map supports a given locale @@ -70,7 +70,7 @@ public interface CaptionMap { class NoSuchCaptionException extends IllegalArgumentException { - public NoSuchCaptionException(@Nonnull final KeyedCaption caption) { + public NoSuchCaptionException(@Nonnull final NamespacedCaption caption) { super(String.format("No caption with the key '%s' exists in the map", caption.getKey())); } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/DummyCaptionMap.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/DummyCaptionMap.java new file mode 100644 index 000000000..2dde6d501 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/DummyCaptionMap.java @@ -0,0 +1,54 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.core.configuration.caption; + +import javax.annotation.Nonnull; +import java.util.Locale; + +/** + * {@link CaptionMap} implementation that throws exception on all getters + */ +public class DummyCaptionMap implements CaptionMap { + + @Override @Nonnull public String getMessage(@Nonnull final TranslatableCaption caption) + throws NoSuchCaptionException { + throw new NoSuchCaptionException(caption); + } + + @Override @Nonnull public String getMessage(@Nonnull final TranslatableCaption caption, + @Nonnull final LocaleHolder localeHolder) throws NoSuchCaptionException { + throw new NoSuchCaptionException(caption); + } + + @Override public boolean supportsLocale(@Nonnull Locale locale) { + return false; + } + + @Override @Nonnull public Locale getLocale() { + throw new UnsupportedOperationException("Cannot get locale of DummyCaptionMap"); + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/LocalizedCaptionMap.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/LocalizedCaptionMap.java index b6c455d23..817008aef 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/LocalizedCaptionMap.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/LocalizedCaptionMap.java @@ -43,7 +43,8 @@ public class LocalizedCaptionMap implements CaptionMap { return this.captions.get(caption); } - @Override @Nonnull public String getMessage(@Nonnull final TranslatableCaption caption, @Nonnull final LocaleHolder localeHolder) { + @Override @Nonnull public String getMessage(@Nonnull final TranslatableCaption caption, + @Nonnull final LocaleHolder localeHolder) { return getMessage(caption); // use the translation of this locale } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/KeyedCaption.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/NamespacedCaption.java similarity index 85% rename from Core/src/main/java/com/plotsquared/core/configuration/caption/KeyedCaption.java rename to Core/src/main/java/com/plotsquared/core/configuration/caption/NamespacedCaption.java index 5ccda3712..dccb7bc73 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/KeyedCaption.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/NamespacedCaption.java @@ -28,9 +28,9 @@ package com.plotsquared.core.configuration.caption; import javax.annotation.Nonnull; /** - * A {@link Caption} that can be identified by a key + * A {@link Caption} that can be identified by a namespace-key pair */ -public interface KeyedCaption extends Caption { +public interface NamespacedCaption extends Caption { /** * Get the key that identifies this caption @@ -39,4 +39,11 @@ public interface KeyedCaption extends Caption { */ @Nonnull String getKey(); + /** + * Get the namespace that this caption belongs to + * + * @return Namespace + */ + @Nonnull String getNamespace(); + } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/TranslatableCaption.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/TranslatableCaption.java index 80ccf5bbe..a3ad37276 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/TranslatableCaption.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/TranslatableCaption.java @@ -25,37 +25,92 @@ */ package com.plotsquared.core.configuration.caption; +import com.google.common.base.Objects; import com.plotsquared.core.PlotSquared; import javax.annotation.Nonnull; import java.util.Locale; +import java.util.regex.Pattern; /** * Caption that is user modifiable */ -public final class TranslatableCaption implements KeyedCaption { +public final class TranslatableCaption implements NamespacedCaption { + /** + * Default caption namespace + */ + public static final String DEFAULT_NAMESPACE = "plotsquared"; + + private final String namespace; private final String key; - private TranslatableCaption(@Nonnull final String key) { + private TranslatableCaption(@Nonnull final String namespace, @Nonnull final String key) { + this.namespace = namespace; this.key = key; } /** * Get a new {@link TranslatableCaption} instance * - * @param key Caption key + * @param rawKey Caption key in the format namespace:key. If no namespace is + * included, {@link #DEFAULT_NAMESPACE} will be used. * @return Caption instance */ - @Nonnull public static TranslatableCaption of(@Nonnull final String key) { - return new TranslatableCaption(key.toLowerCase(Locale.ENGLISH)); + @Nonnull public static TranslatableCaption of(@Nonnull final String rawKey) { + final String namespace; + final String key; + if (rawKey.contains(":")) { + final String[] split = rawKey.split(Pattern.quote(":")); + namespace = split[0]; + key = split[1]; + } else { + namespace = DEFAULT_NAMESPACE; + key = rawKey; + } + return new TranslatableCaption(namespace.toLowerCase(Locale.ENGLISH), + key.toLowerCase(Locale.ENGLISH)); + } + + /** + * Get a new {@link TranslatableCaption} instance + * + * @param namespace Caption namespace + * @param key Caption key + * @return Caption instance + */ + @Nonnull public static TranslatableCaption of(@Nonnull final String namespace, + @Nonnull final String key) { + return new TranslatableCaption(namespace.toLowerCase(Locale.ENGLISH), + key.toLowerCase(Locale.ENGLISH)); } @Override @Nonnull public String getComponent(@Nonnull final LocaleHolder localeHolder) { - return PlotSquared.get().getCaptionMap().getMessage(this, localeHolder); + return PlotSquared.get().getCaptionMap(this.namespace).getMessage(this, localeHolder); } - @Nonnull public String getKey() { + @Override @Nonnull public String getKey() { return this.key; } + + @Override @Nonnull public String getNamespace() { + return this.namespace; + } + + @Override public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || this.getClass() != o.getClass()) { + return false; + } + final TranslatableCaption that = (TranslatableCaption) o; + return Objects.equal(this.getNamespace(), that.getNamespace()) && Objects + .equal(this.getKey(), that.getKey()); + } + + @Override public int hashCode() { + return Objects.hashCode(this.getNamespace(), this.getKey()); + } + } diff --git a/Core/src/main/java/com/plotsquared/core/util/ChunkUtil.java b/Core/src/main/java/com/plotsquared/core/util/ChunkUtil.java index 0df87047b..0a3b5f369 100644 --- a/Core/src/main/java/com/plotsquared/core/util/ChunkUtil.java +++ b/Core/src/main/java/com/plotsquared/core/util/ChunkUtil.java @@ -1,3 +1,28 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package com.plotsquared.core.util; /**