Skip to content

Commit

Permalink
Setup the file loading system for translations
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Jun 22, 2019
1 parent 182df86 commit b651156
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 8 deletions.
22 changes: 22 additions & 0 deletions worldedit-core/build.gradle
@@ -1,5 +1,6 @@
plugins {
id("net.ltgt.apt") version "0.21"
id "com.mendhak.gradlecrowdin" version "0.1.0"
}

apply plugin: 'java-library'
Expand Down Expand Up @@ -50,3 +51,24 @@ sourceSets {
}
}
}

if (project.hasProperty("crowdin_apikey")) {
build.dependsOn(crowdinUpload)
build.dependsOn(crowdinDownload)
} else {
ext.crowdin_apikey = ""
}

crowdinUpload {
apiKey = "${crowdin_apikey}"
projectId = 'worldedit-core'
files = [
[ name: 'strings.json', source: "$projectDir/worldedit-core/src/main/resources/lang/strings.json" ]
]
}

crowdinDownload {
apiKey = "${crowdin_apikey}"
destination = "$projectDir/worldedit-core/src/main/resources/lang"
projectId = 'worldedit-core'
}
Expand Up @@ -108,7 +108,7 @@ public final class WorldEdit {
private final SessionManager sessions = new SessionManager(this);
private final ListeningExecutorService executorService = MoreExecutors.listeningDecorator(EvenMoreExecutors.newBoundedCachedThreadPool(0, 1, 20));;
private final Supervisor supervisor = new SimpleSupervisor();
private final TranslationManager translationManager = new TranslationManager();
private final TranslationManager translationManager = new TranslationManager(this);

private final BlockFactory blockFactory = new BlockFactory(this);
private final ItemFactory itemFactory = new ItemFactory(this);
Expand Down
Expand Up @@ -40,4 +40,17 @@ public static URL getResource(Class clazz, String name) throws IOException {
}
return url;
}

public static URL getResourceRoot(String name) throws IOException {
URL url = ResourceLoader.class.getResource("/" + name);
if (url == null) {
try {
return new URL("modjar://worldedit/" + name);
} catch (Exception e) {
// Not forge.
}
throw new IOException("Could not find " + name);
}
return url;
}
}
Expand Up @@ -19,38 +19,109 @@

package com.sk89q.worldedit.util.translation;

import com.google.common.io.Files;
import com.google.common.io.Resources;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.util.formatting.text.renderer.FriendlyComponentRenderer;
import com.sk89q.worldedit.util.io.ResourceLoader;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URL;
import java.nio.charset.Charset;
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.stream.Collectors;

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 FriendlyComponentRenderer<Locale> friendlyComponentRenderer = FriendlyComponentRenderer.from(
(locale, key) -> new MessageFormat(getTranslationMap(locale).getOrDefault(key, key), locale));
private Locale defaultLocale = Locale.US;
private Locale defaultLocale = Locale.ENGLISH;

private final WorldEdit worldEdit;

private final Set<Locale> checkedLocales = new HashSet<>();

public TranslationManager() {
// Temporary store until we have file loads
Map<String, String> us = new HashMap<>();
us.put("worldedit.region.expanded", "Region expanded {0} block(s)");
translationMap.put(Locale.US, us);
public TranslationManager(WorldEdit worldEdit) {
this.worldEdit = worldEdit;
}

public void setDefaultLocale(Locale defaultLocale) {
this.defaultLocale = defaultLocale;
}

private Map<String, String> parseTranslationFile(File file) throws IOException {
return gson.fromJson(Files.toString(file, Charset.defaultCharset()), STRING_MAP_TYPE);
}

private Map<String, String> parseTranslationFile(URL file) throws IOException {
return gson.fromJson(Resources.toString(file, Charset.defaultCharset()), STRING_MAP_TYPE);
}

private Optional<Map<String, String>> loadTranslationFile(String filename) {
File localFile = worldEdit.getWorkingDirectoryFile("lang/" + filename);
if (localFile.exists()) {
try {
return Optional.of(parseTranslationFile(localFile));
} catch (IOException e) {
return Optional.empty();
}
} else {
try {
return Optional.of(parseTranslationFile(ResourceLoader.getResourceRoot("lang/" + filename)));
} catch (IOException e) {
return Optional.empty();
}
}
}

private boolean tryLoadTranslations(Locale locale) {
if (checkedLocales.contains(locale)) {
return false;
}
checkedLocales.add(locale);
Optional<Map<String, String>> langData = loadTranslationFile(locale.getLanguage() + "-" + locale.getCountry() + "/strings.json");
if (!langData.isPresent()) {
langData = loadTranslationFile(locale.getLanguage() + "/strings.json");
}
if (langData.isPresent()) {
translationMap.put(locale, langData.get());
return true;
}
if (locale.equals(defaultLocale)) {
translationMap.put(Locale.ENGLISH, loadTranslationFile("strings.json").orElseThrow(
() -> new RuntimeException("Failed to load WorldEdit strings!")
));
return true;
}
return false;
}

private Map<String, String> getTranslationMap(Locale locale) {
Map<String, String> translations = translationMap.get(locale);
if (translations == null) {
translations = translationMap.get(defaultLocale);
if (tryLoadTranslations(locale)) {
return getTranslationMap(locale);
}
if (!locale.equals(defaultLocale)) {
translations = getTranslationMap(defaultLocale);
}
}

return translations;
Expand Down
1 change: 1 addition & 0 deletions worldedit-core/src/main/resources/lang/.gitignore
@@ -0,0 +1 @@
*/
3 changes: 3 additions & 0 deletions worldedit-core/src/main/resources/lang/strings.json
@@ -0,0 +1,3 @@
{
"worldedit.region.expanded": "Region expanded {0} block(s)"
}

0 comments on commit b651156

Please sign in to comment.