Added translatable tags and topics for the Catalog entries

This commit is contained in:
Florian CUNY 2019-06-29 13:34:49 +02:00
parent 3ce8b3060a
commit e23ed0b25d
3 changed files with 53 additions and 9 deletions

View File

@ -1,5 +1,6 @@
package world.bentobox.bentobox.managers;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.github.TheBusyBiscuit.GitHubWebAPI4Java.GitHubWebAPI;
@ -10,6 +11,7 @@ import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.web.catalog.CatalogEntry;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
@ -55,20 +57,55 @@ public class WebManager {
plugin.log("Downloading data from GitHub...");
}
GitHubRepository repo = new GitHubRepository(gh, "BentoBoxWorld/weblink");
String tagsContent = "";
String topicsContent = "";
String catalogContent = "";
// Downloading the data
try {
tagsContent = repo.getContent("catalog/tags.json").getContent().replaceAll("\\n", "");
topicsContent = repo.getContent("catalog/topics.json").getContent().replaceAll("\\n", "");
catalogContent = repo.getContent("catalog/catalog.json").getContent().replaceAll("\\n", "");
catalogContent = new String(Base64.getDecoder().decode(catalogContent));
} catch (IllegalAccessException e) {
plugin.log("Could not connect to GitHub.");
} catch (Exception e) {
plugin.logError("An error occurred when downloading from GitHub...");
plugin.logStacktrace(e);
}
// Parsing the data
// Decoding the Base64 encoded contents
tagsContent = new String(Base64.getDecoder().decode(tagsContent), StandardCharsets.UTF_8);
topicsContent = new String(Base64.getDecoder().decode(topicsContent), StandardCharsets.UTF_8);
catalogContent = new String(Base64.getDecoder().decode(catalogContent), StandardCharsets.UTF_8);
/* Parsing the data */
// Register the tags translations in the locales
if (!tagsContent.isEmpty()) {
JsonObject tags = new JsonParser().parse(tagsContent).getAsJsonObject();
tags.entrySet().forEach((entry) -> plugin.getLocalesManager().getLanguages().values().forEach((locale) -> {
JsonElement translation = entry.getValue().getAsJsonObject().get(locale.toLanguageTag());
if (translation != null) {
locale.set("catalog.tags." + entry.getKey(), translation.getAsString());
}
}));
}
// Register the topics translations in the locales
if (!topicsContent.isEmpty()) {
JsonObject topics = new JsonParser().parse(topicsContent).getAsJsonObject();
topics.entrySet().forEach((entry) -> plugin.getLocalesManager().getLanguages().values().forEach((locale) -> {
JsonElement translation = entry.getValue().getAsJsonObject().get(locale.toLanguageTag());
if (translation != null) {
locale.set("catalog.topics." + entry.getKey(), translation.getAsString());
}
}));
}
// Register the catalog data
if (!catalogContent.isEmpty()) {
if (clearCache) {
gh.clearCache();
this.addonsCatalog.clear();
this.gamemodesCatalog.clear();
}

View File

@ -77,7 +77,12 @@ public class CatalogPanel {
for (CatalogEntry addon : catalog) {
PanelItemBuilder itemBuilder = new PanelItemBuilder();
itemBuilder.icon(addon.getIcon()).name(ChatColor.WHITE + addon.getName());
String name = ChatColor.WHITE + addon.getName();
if (addon.getTag() != null) {
name += " " + ChatColor.AQUA + "" + ChatColor.BOLD + user.getTranslation("catalog.tags." + addon.getTag());
}
itemBuilder.icon(addon.getIcon()).name(name);
// If the addon is already installed, then tell the user it's already installed
String install;
@ -89,7 +94,7 @@ public class CatalogPanel {
}
itemBuilder.description(user.getTranslation(LOCALE_REF + "icon.description-template",
"[topic]", StringUtils.capitalize(addon.getTopic()),
"[topic]", StringUtils.capitalize(user.getTranslation("catalog.topics." + addon.getTopic())),
"[install]", install,
"[description]", addon.getDescription()));

View File

@ -1,5 +1,6 @@
package world.bentobox.bentobox.web.catalog;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import org.bukkit.Material;
import org.eclipse.jdt.annotation.NonNull;
@ -22,7 +23,7 @@ public class CatalogEntry {
private @NonNull String name;
private @NonNull String description;
private @Nullable String topic;
private @NonNull List<String> tags = new ArrayList<>();
private @Nullable String tag;
private @NonNull String repository;
public CatalogEntry(@NonNull JsonObject object) {
@ -33,6 +34,7 @@ public class CatalogEntry {
this.description = object.get("description").getAsString();
this.repository = object.get("repository").getAsString();
this.topic = object.get("topic").getAsString();
this.tag = (!(object.get("tag") instanceof JsonNull)) ? object.get("tag").getAsString() : null;
}
public int getSlot() {
@ -59,9 +61,9 @@ public class CatalogEntry {
return topic;
}
@NonNull
public List<String> getTags() {
return tags;
@Nullable
public String getTag() {
return tag;
}
@NonNull