|
| 1 | +package org.maxgamer.quickshop.util.language.text; |
| 2 | + |
| 3 | +import com.dumptruckman.bukkit.configuration.json.JsonConfiguration; |
| 4 | +import org.bukkit.command.CommandSender; |
| 5 | +import org.bukkit.configuration.InvalidConfigurationException; |
| 6 | +import org.bukkit.entity.Player; |
| 7 | +import org.jetbrains.annotations.NotNull; |
| 8 | +import org.jetbrains.annotations.Nullable; |
| 9 | +import org.maxgamer.quickshop.QuickShop; |
| 10 | +import org.maxgamer.quickshop.util.Util; |
| 11 | +import org.maxgamer.quickshop.util.language.text.distributions.Distribution; |
| 12 | +import org.maxgamer.quickshop.util.language.text.distributions.crowdin.CrowdinOTA; |
| 13 | +import org.maxgamer.quickshop.util.language.text.postprocessing.PostProcessor; |
| 14 | +import org.maxgamer.quickshop.util.language.text.postprocessing.impl.ColorProcessor; |
| 15 | +import org.maxgamer.quickshop.util.language.text.postprocessing.impl.FillerProcessor; |
| 16 | +import org.maxgamer.quickshop.util.language.text.postprocessing.impl.PlaceHolderApiProcessor; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.logging.Level; |
| 28 | + |
| 29 | +public class TextManager { |
| 30 | + private final QuickShop plugin; |
| 31 | + private final Distribution distribution; |
| 32 | + private final File overrideFilesFolder; |
| 33 | + // <File <Locale, Section>> |
| 34 | + private final Map<String, Map<String, JsonConfiguration>> locale2ContentMapping = new HashMap<>(); |
| 35 | + private final static String languageFileCrowdin = "/master/src/main/resources/lang/%locale%/messages.json"; |
| 36 | + public List<PostProcessor> postProcessors = new ArrayList<>(); |
| 37 | + private JsonConfiguration bundledLang = new JsonConfiguration(); |
| 38 | + |
| 39 | + public TextManager(QuickShop plugin) { |
| 40 | + this.plugin = plugin; |
| 41 | + this.distribution = new CrowdinOTA(plugin); |
| 42 | + this.overrideFilesFolder = new File(plugin.getDataFolder(), "lang-override"); |
| 43 | + load(); |
| 44 | + } |
| 45 | + |
| 46 | + public void load() { |
| 47 | + locale2ContentMapping.clear(); |
| 48 | + postProcessors.clear(); |
| 49 | + // Load mapping |
| 50 | + //for (String availableFile : distribution.getAvailableFiles()) { |
| 51 | + try (InputStream stream = plugin.getResource("lang-original/messages.json")) { |
| 52 | + if (stream != null) |
| 53 | + bundledLang.loadFromString(new String(Util.inputStream2ByteArray(stream), StandardCharsets.UTF_8)); |
| 54 | + } catch (IOException | InvalidConfigurationException ignored) { |
| 55 | + bundledLang = new JsonConfiguration(); |
| 56 | + } |
| 57 | + for (String availableLanguage : distribution.getAvailableLanguages()) { |
| 58 | + try { |
| 59 | + // load OTA text |
| 60 | + String localeFileContent = distribution.getFile(languageFileCrowdin, availableLanguage); |
| 61 | + Map<String, JsonConfiguration> fileLocaleMapping = locale2ContentMapping.computeIfAbsent(languageFileCrowdin, k -> new HashMap<>()); |
| 62 | + JsonConfiguration configuration = new JsonConfiguration(); |
| 63 | + fileLocaleMapping.put(availableLanguage, configuration); |
| 64 | + configuration.loadFromString(localeFileContent); |
| 65 | + // load override text |
| 66 | + JsonConfiguration override = new JsonConfiguration(); |
| 67 | + File localOverrideFile = new File(overrideFilesFolder, availableLanguage + ".json"); |
| 68 | + if (localOverrideFile.exists()) { |
| 69 | + override.loadFromString(Files.readString(localOverrideFile.toPath())); |
| 70 | + for (String key : override.getKeys(true)) { |
| 71 | + configuration.set(key, override.get(key)); |
| 72 | + } |
| 73 | + } |
| 74 | + } catch (Exception e) { |
| 75 | + plugin.getLogger().log(Level.WARNING, "Couldn't update the translation for locale " + availableLanguage, e); |
| 76 | + } |
| 77 | + } |
| 78 | + postProcessors.add(new FillerProcessor()); |
| 79 | + postProcessors.add(new ColorProcessor()); |
| 80 | + postProcessors.add(new PlaceHolderApiProcessor()); |
| 81 | + } |
| 82 | + |
| 83 | + public Text of(CommandSender sender, String path, String... args) { |
| 84 | + return new Text(this, sender, locale2ContentMapping.get(languageFileCrowdin), path, args); |
| 85 | + } |
| 86 | + |
| 87 | + static class Text { |
| 88 | + private final TextManager manager; |
| 89 | + private final String path; |
| 90 | + private final QuickShop plugin; |
| 91 | + private final Map<String, JsonConfiguration> mapping; |
| 92 | + private final CommandSender sender; |
| 93 | + private final String[] args; |
| 94 | + |
| 95 | + private Text(TextManager manager, CommandSender sender, Map<String, JsonConfiguration> mapping, String path, String... args) { |
| 96 | + this.plugin = manager.plugin; |
| 97 | + this.manager = manager; |
| 98 | + this.sender = sender; |
| 99 | + this.mapping = mapping; |
| 100 | + this.path = path; |
| 101 | + this.args = args; |
| 102 | + } |
| 103 | + |
| 104 | + @Nullable |
| 105 | + private String fallbackLocal() { |
| 106 | + return manager.bundledLang.getString(path); |
| 107 | + } |
| 108 | + |
| 109 | + @NotNull |
| 110 | + private String postProcess(@NotNull String text) { |
| 111 | + for (PostProcessor postProcessor : this.manager.postProcessors) |
| 112 | + text = postProcessor.process(text, sender, args); |
| 113 | + return text; |
| 114 | + } |
| 115 | + |
| 116 | + @NotNull |
| 117 | + public String forLocale(@NotNull String locale) { |
| 118 | + JsonConfiguration index = mapping.get(locale); |
| 119 | + if (index == null && locale.equals("en")) { |
| 120 | + String str = fallbackLocal(); |
| 121 | + if (str == null) |
| 122 | + return "Fallback Missing Language Key: " + path + ", report to QuickShop!"; |
| 123 | + } |
| 124 | + if (index == null) |
| 125 | + return forLocale("en"); |
| 126 | + String str = index.getString(locale); |
| 127 | + if (str == null) |
| 128 | + return "Missing Language Key: " + path; |
| 129 | + return postProcess(str); |
| 130 | + } |
| 131 | + |
| 132 | + @NotNull |
| 133 | + public String forLocale(@NotNull Player player){ |
| 134 | + return forLocale(player.getLocale()); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments