mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-11-22 13:16:45 +01:00
Add namespaces for translations
This commit is contained in:
parent
32a0765484
commit
af44fe74ff
@ -33,6 +33,9 @@ import com.plotsquared.core.configuration.Settings;
|
|||||||
import com.plotsquared.core.configuration.Storage;
|
import com.plotsquared.core.configuration.Storage;
|
||||||
import com.plotsquared.core.configuration.caption.CaptionLoader;
|
import com.plotsquared.core.configuration.caption.CaptionLoader;
|
||||||
import com.plotsquared.core.configuration.caption.CaptionMap;
|
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.file.YamlConfiguration;
|
||||||
import com.plotsquared.core.configuration.serialization.ConfigurationSerialization;
|
import com.plotsquared.core.configuration.serialization.ConfigurationSerialization;
|
||||||
import com.plotsquared.core.database.DBFunc;
|
import com.plotsquared.core.database.DBFunc;
|
||||||
@ -142,7 +145,7 @@ public class PlotSquared {
|
|||||||
public HashMap<String, HashMap<PlotId, Plot>> plots_tmp;
|
public HashMap<String, HashMap<PlotId, Plot>> plots_tmp;
|
||||||
private YamlConfiguration config;
|
private YamlConfiguration config;
|
||||||
// Localization
|
// Localization
|
||||||
private CaptionMap captionMap;
|
private Map<String, CaptionMap> captionMaps;
|
||||||
// Platform / Version / Update URL
|
// Platform / Version / Update URL
|
||||||
private PlotVersion version;
|
private PlotVersion version;
|
||||||
// Files and configuration
|
// Files and configuration
|
||||||
@ -194,6 +197,8 @@ public class PlotSquared {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.captionMaps = new HashMap<>();
|
||||||
|
|
||||||
// Setup localization
|
// Setup localization
|
||||||
CaptionMap captionMap;
|
CaptionMap captionMap;
|
||||||
if (Settings.Enabled_Components.PER_USER_LOCALE) {
|
if (Settings.Enabled_Components.PER_USER_LOCALE) {
|
||||||
@ -202,8 +207,8 @@ public class PlotSquared {
|
|||||||
String fileName = "messages_" + Settings.Enabled_Components.DEFAULT_LOCALE + ".json";
|
String fileName = "messages_" + Settings.Enabled_Components.DEFAULT_LOCALE + ".json";
|
||||||
captionMap = CaptionLoader.loadSingle(Paths.get("lang", fileName));
|
captionMap = CaptionLoader.loadSingle(Paths.get("lang", fileName));
|
||||||
}
|
}
|
||||||
this.captionMap = captionMap;
|
|
||||||
|
|
||||||
|
this.captionMaps.put(TranslatableCaption.DEFAULT_NAMESPACE, captionMap);
|
||||||
this.worldedit = WorldEdit.getInstance();
|
this.worldedit = WorldEdit.getInstance();
|
||||||
|
|
||||||
// Create Event utility class
|
// Create Event utility class
|
||||||
@ -1471,8 +1476,30 @@ public class PlotSquared {
|
|||||||
return this.worldConfiguration;
|
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() {
|
public File getJarFile() {
|
||||||
|
@ -51,7 +51,7 @@ public interface CaptionMap {
|
|||||||
* @return Component
|
* @return Component
|
||||||
* @throws NoSuchCaptionException if no caption with the given key exists
|
* @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
|
* Check if the map supports a given locale
|
||||||
@ -70,7 +70,7 @@ public interface CaptionMap {
|
|||||||
|
|
||||||
class NoSuchCaptionException extends IllegalArgumentException {
|
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()));
|
super(String.format("No caption with the key '%s' exists in the map", caption.getKey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -43,7 +43,8 @@ public class LocalizedCaptionMap implements CaptionMap {
|
|||||||
return this.captions.get(caption);
|
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
|
return getMessage(caption); // use the translation of this locale
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,9 +28,9 @@ package com.plotsquared.core.configuration.caption;
|
|||||||
import javax.annotation.Nonnull;
|
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
|
* Get the key that identifies this caption
|
||||||
@ -39,4 +39,11 @@ public interface KeyedCaption extends Caption {
|
|||||||
*/
|
*/
|
||||||
@Nonnull String getKey();
|
@Nonnull String getKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the namespace that this caption belongs to
|
||||||
|
*
|
||||||
|
* @return Namespace
|
||||||
|
*/
|
||||||
|
@Nonnull String getNamespace();
|
||||||
|
|
||||||
}
|
}
|
@ -25,37 +25,92 @@
|
|||||||
*/
|
*/
|
||||||
package com.plotsquared.core.configuration.caption;
|
package com.plotsquared.core.configuration.caption;
|
||||||
|
|
||||||
|
import com.google.common.base.Objects;
|
||||||
import com.plotsquared.core.PlotSquared;
|
import com.plotsquared.core.PlotSquared;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Caption that is user modifiable
|
* 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 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;
|
this.key = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a new {@link TranslatableCaption} instance
|
* 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
|
* @return Caption instance
|
||||||
*/
|
*/
|
||||||
@Nonnull public static TranslatableCaption of(@Nonnull final String key) {
|
@Nonnull public static TranslatableCaption of(@Nonnull final String rawKey) {
|
||||||
return new TranslatableCaption(key.toLowerCase(Locale.ENGLISH));
|
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) {
|
@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;
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
package com.plotsquared.core.util;
|
package com.plotsquared.core.util;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user