i18n upstream merge

This commit is contained in:
NotMyFault
2019-12-09 20:07:57 +01:00
parent c5a9436174
commit 80d7b0582f
24 changed files with 121 additions and 119 deletions

View File

@ -39,7 +39,6 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;
@ -123,12 +122,7 @@ public class PropertiesConfiguration extends LocalConfiguration {
allowSymlinks = getBool("allow-symbolic-links", allowSymlinks);
serverSideCUI = getBool("server-side-cui", serverSideCUI);
extendedYLimit = getBool("extended-y-limit", extendedYLimit);
defaultLocaleName = getString("default-locale", defaultLocaleName);
if (defaultLocaleName.equals("default")) {
defaultLocale = Locale.getDefault();
} else {
defaultLocale = Locale.forLanguageTag(defaultLocaleName.replace('_', '-'));
}
setDefaultLocaleName(getString("default-locale", defaultLocaleName));
LocalSession.MAX_HISTORY_SIZE = Math.max(15, getInt("history-size", 15));

View File

@ -30,7 +30,6 @@ import org.slf4j.Logger;
import java.io.IOException;
import java.util.HashSet;
import java.util.Locale;
/**
* A less simple implementation of {@link LocalConfiguration}
@ -128,12 +127,7 @@ public class YAMLConfiguration extends LocalConfiguration {
extendedYLimit = config.getBoolean("compat.extended-y-limit", false);
defaultLocaleName = config.getString("default-locale", defaultLocaleName);
if (defaultLocaleName.equals("default")) {
defaultLocale = Locale.getDefault();
} else {
defaultLocale = Locale.forLanguageTag(defaultLocaleName.replace('_', '-'));
}
setDefaultLocaleName(config.getString("default-locale", defaultLocaleName));
}
public void unload() {

View File

@ -35,7 +35,7 @@ public class WorldEditText {
}
public static Component format(Component component, Locale locale) {
return CONFIG_HOLDER.replace(WorldEdit.getInstance().getTranslationManager().convertText(component, locale));
return WorldEdit.getInstance().getTranslationManager().convertText(CONFIG_HOLDER.replace(component), locale);
}
public static String reduceToText(Component component, Locale locale) {

View File

@ -23,6 +23,7 @@ import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import java.util.List;
import java.util.Locale;
public class TextUtils {
@ -46,4 +47,13 @@ public class TextUtils {
}
return builder.build();
}
/**
* Gets a Java Locale object by the Minecraft locale tag.
*
* @param locale The Minecraft locale tag
* @return A Java locale
*/
public static Locale getLocaleByMinecraftTag(String locale) {
return Locale.forLanguageTag(locale.replace('_', '-'));
}
}

View File

@ -28,18 +28,18 @@ public class ResourceLoader {
}
private static URL getResourceForgeHack(String location) throws IOException {
return new URL("modjar://worldedit/" + location);
try {
return new URL("modjar://worldedit/" + location);
} catch (Exception e) {
throw new IOException("Could not find " + location);
}
}
public static URL getResource(Class clazz, String name) throws IOException {
URL url = clazz.getResource(name);
if (url == null) {
try {
return getResourceForgeHack(clazz.getName().substring(0, clazz.getName().lastIndexOf('.')).replace(".", "/")
+ "/" + name);
} catch (Exception e) {
throw new IOException("Could not find " + name);
}
return getResourceForgeHack(clazz.getName().substring(0, clazz.getName().lastIndexOf('.')).replace(".", "/")
+ "/" + name);
}
return url;
}
@ -47,12 +47,8 @@ public class ResourceLoader {
public static URL getResourceRoot(String name) throws IOException {
URL url = ResourceLoader.class.getResource("/" + name);
if (url == null) {
try {
return getResourceForgeHack(name);
} catch (Exception e) {
throw new IOException("Could not find " + name);
}
return getResourceForgeHack(name);
}
return url;
}
}
}

View File

@ -19,8 +19,7 @@
package com.sk89q.worldedit.util.translation;
import com.google.common.io.Files;
import com.google.common.io.Resources;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
@ -29,21 +28,14 @@ import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.renderer.FriendlyComponentRenderer;
import com.sk89q.worldedit.util.io.ResourceLoader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.lang.reflect.Type;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import static java.util.stream.Collectors.toMap;
/**
* Handles translations for the plugin.
@ -62,7 +54,7 @@ public class TranslationManager {
private static final Gson gson = new GsonBuilder().create();
private static final Type STRING_MAP_TYPE = new TypeToken<Map<String, String>>() {}.getType();
private final Map<Locale, Map<String, String>> translationMap = new HashMap<>();
private final Map<Locale, Map<String, String>> translationMap = new ConcurrentHashMap<>();
private final FriendlyComponentRenderer<Locale> friendlyComponentRenderer = FriendlyComponentRenderer.from(
(locale, key) -> new MessageFormat(getTranslationMap(locale).getOrDefault(key, key), locale));
private Locale defaultLocale = Locale.ENGLISH;
@ -80,8 +72,10 @@ public class TranslationManager {
}
private Map<String, String> filterTranslations(Map<String, String> translations) {
translations.entrySet().removeIf(entry -> entry.getValue().isEmpty());
return translations;
return translations.entrySet().stream()
.filter(e -> !e.getValue().isEmpty())
.map(e -> Maps.immutableEntry(e.getKey(), e.getValue().replace("'", "''")))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
}
private Map<String, String> parseTranslationFile(InputStream inputStream) {
@ -95,7 +89,7 @@ public class TranslationManager {
baseTranslations = parseTranslationFile(ResourceLoader.getResourceRoot("lang/" + filename).openStream());
} catch (IOException e) {
// Seem to be missing base. If the user has provided a file use that.
baseTranslations = new HashMap<>();
baseTranslations = new ConcurrentHashMap<>();
}
File localFile = worldEdit.getWorkingDirectoryFile("lang/" + filename);
@ -117,7 +111,7 @@ public class TranslationManager {
}
checkedLocales.add(locale);
// Make a copy of the default language file
Map<String, String> baseTranslations = new HashMap<>();
Map<String, String> baseTranslations = new ConcurrentHashMap<>();
if (!locale.equals(defaultLocale)) {
baseTranslations.putAll(getTranslationMap(defaultLocale));
}