diff --git a/crowdin.yml b/crowdin.yml index cde5daa9d..044d44643 100644 --- a/crowdin.yml +++ b/crowdin.yml @@ -1,3 +1,3 @@ files: - - source: /src/main/resources/bundled/*.json + - source: /src/main/resources/lang-original/*.json translation: /src/main/resources/lang/%locale%/%original_file_name% diff --git a/pom.xml b/pom.xml index 5df9f0847..97f9add87 100644 --- a/pom.xml +++ b/pom.xml @@ -314,7 +314,7 @@ src/main/resources/bin src/main/resources/lang - src/main/resources/bundled + src/main/resources/lang-original @@ -328,9 +328,9 @@ src/main/resources/lang - bundled + lang-original false - src/main/resources/bundled + src/main/resources/lang-original diff --git a/src/main/java/org/maxgamer/quickshop/util/MsgUtil.java b/src/main/java/org/maxgamer/quickshop/util/MsgUtil.java index 14c0e1f3d..e239e9363 100644 --- a/src/main/java/org/maxgamer/quickshop/util/MsgUtil.java +++ b/src/main/java/org/maxgamer/quickshop/util/MsgUtil.java @@ -227,7 +227,7 @@ public class MsgUtil { // String buildInMessageFilePath = "lang/" + languageCode + "/messages.json"; // if (plugin.getResource(buildInMessageFilePath) == null) { // //Use default -// buildInMessageFilePath = "bundled/messages.json"; +// buildInMessageFilePath = "lang-original/messages.json"; // } // if (!extractedMessageFile.exists()) { // try { @@ -280,7 +280,7 @@ public class MsgUtil { // Util.debugLog("Cannot load default built-in language file: " + ioException.getMessage()); // } // builtInLang = HumanReadableJsonConfiguration.loadConfiguration(buildInLangFile); -// try (InputStreamReader inputStreamReader = new InputStreamReader(Objects.requireNonNull(plugin.getResource("bundled/messages.json")))) { +// try (InputStreamReader inputStreamReader = new InputStreamReader(Objects.requireNonNull(plugin.getResource("lang-original/messages.json")))) { // builtInOriginalLang = HumanReadableJsonConfiguration.loadConfiguration(inputStreamReader); // } catch (IOException | NullPointerException exception) { // plugin.getLogger().log(Level.WARNING, "Cannot to load built-in original messages, some phrases may missing when upgrading", exception); diff --git a/src/main/java/org/maxgamer/quickshop/util/language/text/TextManager.java b/src/main/java/org/maxgamer/quickshop/util/language/text/TextManager.java index 61585f105..c367c4429 100644 --- a/src/main/java/org/maxgamer/quickshop/util/language/text/TextManager.java +++ b/src/main/java/org/maxgamer/quickshop/util/language/text/TextManager.java @@ -25,10 +25,6 @@ import java.nio.charset.StandardCharsets; import java.util.*; import java.util.logging.Level; -/** - * QuickShop universal text manager - * With multi-files and multi-languages support - */ public class TextManager { private final QuickShop plugin; private final Distribution distribution; @@ -36,8 +32,6 @@ public class TextManager { private final Map> locale2ContentMapping = new HashMap<>(); private final static String languageFileCrowdin = "/master/src/main/resources/lang/%locale%/messages.json"; public List postProcessors = new ArrayList<>(); - // - private final Map bundledMapping = new HashMap<>(); private JsonConfiguration bundledLang = new JsonConfiguration(); public TextManager(QuickShop plugin) { @@ -46,78 +40,69 @@ public class TextManager { load(); } - /** - * Generate and returns the override files storage folder for specific file - * - * @param distributionPath The file name or path on distribution platform - * @return The storage folder - */ @NotNull - private File getOverrideFilesFolder(@NotNull String distributionPath) { - File folder = new File(new File(plugin.getDataFolder(), "overrides"), getFileNameFromFullPath(distributionPath) + ".overrides"); + private File getOverrideFilesFolder(@NotNull String crowdinPath) { + File file = new File(crowdinPath); + File folder = new File(new File(plugin.getDataFolder(), "overrides"), file.getName() + ".overrides"); folder.mkdirs(); return folder; } - private String getFileNameFromFullPath(@NotNull String path) { - return new File(path).getName(); - } - public void load() { plugin.getLogger().info("Checking for translation updates..."); locale2ContentMapping.clear(); postProcessors.clear(); // Load mapping - - + //for (String availableFile : distribution.getAvailableFiles()) { + try { + bundledLang.loadFromString(new String(IOUtils.toByteArray(new InputStreamReader(plugin.getResource("lang-original/messages.json")), StandardCharsets.UTF_8))); + } catch (IOException | InvalidConfigurationException ex) { + bundledLang = new JsonConfiguration(); + plugin.getLogger().log(Level.SEVERE, "Cannot load bundled language file from Jar, some strings may missing!", ex); + } // init file mapping locale2ContentMapping.computeIfAbsent(languageFileCrowdin, e -> new HashMap<>()); // Multi File and Multi-Language loader - distribution.getAvailableFiles().parallelStream().forEach(crowdinFile -> { - JsonConfiguration bundledLang = new JsonConfiguration(); - try { - bundledLang.loadFromString(new String(IOUtils.toByteArray(new InputStreamReader(plugin.getResource("bundled/" + getFileNameFromFullPath(crowdinFile))), StandardCharsets.UTF_8))); - } catch (IOException | InvalidConfigurationException ex) { - bundledLang = new JsonConfiguration(); - plugin.getLogger().log(Level.SEVERE, "Cannot load bundled file " + crowdinFile + " from Jar, some strings may missing!", ex); - } - bundledMapping.put(crowdinFile, bundledLang); - distribution.getAvailableLanguages().parallelStream().forEach(crowdinCode -> { - try { - // load OTA text from Crowdin - String minecraftCode = crowdinCode.toLowerCase(Locale.ROOT).replace("-", "_"); - Util.debugLog("Loading translation for locale: " + crowdinCode + " (" + minecraftCode + ")"); - JsonConfiguration configuration = new JsonConfiguration(); - try { - configuration.loadFromString(distribution.getFile(crowdinFile, crowdinCode)); - } catch (InvalidConfigurationException exception) { - configuration.loadFromString(distribution.getFile(crowdinFile, crowdinCode, true)); - } - // load override text (allow user modification the translation) - JsonConfiguration override = new JsonConfiguration(); - File localOverrideFile = new File(getOverrideFilesFolder(crowdinFile), minecraftCode + ".json"); - if (!localOverrideFile.exists()) { - localOverrideFile.getParentFile().mkdirs(); - localOverrideFile.createNewFile(); - } - override.loadFromString(Util.readToString(localOverrideFile)); - for (String key : override.getKeys(true)) { - if (key.equals("language-version") || key.equals("config-version") || key.equals("version")) - continue; - configuration.set(key, override.get(key)); - } - locale2ContentMapping.get(languageFileCrowdin).computeIfAbsent(minecraftCode, e -> configuration); - } catch (CrowdinOTA.OTAException e) { - plugin.getLogger().warning("Couldn't update the translation for locale " + crowdinCode + " because it not configured, please report to QuickShop"); - } catch (IOException e) { - plugin.getLogger().log(Level.WARNING, "Couldn't update the translation for locale " + crowdinCode + " please check your network connection.", e); - } catch (Exception e) { - plugin.getLogger().log(Level.WARNING, "Couldn't update the translation for locale " + crowdinCode + ".", e); - } - }); - }); + distribution.getAvailableLanguages().parallelStream().forEach(crowdinCode -> distribution.getAvailableFiles().parallelStream().forEach(crowdinFile -> { + try { + // load OTA text from Crowdin + + String minecraftCode = crowdinCode.toLowerCase(Locale.ROOT).replace("-", "_"); + + Util.debugLog("Loading translation for locale: " + crowdinCode + " (" + minecraftCode + ")"); + JsonConfiguration configuration = new JsonConfiguration(); + try { + configuration.loadFromString(distribution.getFile(crowdinFile, crowdinCode)); + } catch (InvalidConfigurationException exception) { + configuration.loadFromString(distribution.getFile(crowdinFile, crowdinCode, true)); + } + // load override text (allow user modification the translation) + JsonConfiguration override = new JsonConfiguration(); + File localOverrideFile = new File(getOverrideFilesFolder(crowdinFile), minecraftCode + ".json"); + if (!localOverrideFile.exists()) { + localOverrideFile.getParentFile().mkdirs(); + localOverrideFile.createNewFile(); + } + override.loadFromString(Util.readToString(localOverrideFile)); + for (String key : override.getKeys(true)) { + if (key.equals("language-version") || key.equals("config-version") || key.equals("version")) + continue; + configuration.set(key, override.get(key)); + } + locale2ContentMapping.get(languageFileCrowdin).computeIfAbsent(minecraftCode, e -> configuration); + Util.debugLog("Locale " + crowdinFile); + if (configuration.getInt("language-version") < bundledLang.getInt("language-version")) + Util.debugLog("Locale " + crowdinCode + " file version is outdated, some string will fallback to English."); + } catch (CrowdinOTA.OTAException e) { + plugin.getLogger().warning("Couldn't update the translation for locale " + crowdinCode + " because it not configured, please report to QuickShop"); + } catch (IOException e) { + plugin.getLogger().log(Level.WARNING, "Couldn't update the translation for locale " + crowdinCode + " please check your network connection.", e); + } catch (Exception e) { + plugin.getLogger().log(Level.WARNING, "Couldn't update the translation for locale " + crowdinCode + ".", e); + } + })); // for (String availableLanguage : distribution.getAvailableLanguages()) { @@ -129,27 +114,27 @@ public class TextManager { } public Text of(@NotNull String path, String... args) { - return new Text(this, (CommandSender) null, languageFileCrowdin, path, args); + return new Text(this, (CommandSender) null, locale2ContentMapping.get(languageFileCrowdin), path, args); } public Text of(@Nullable CommandSender sender, @NotNull String path, String... args) { - return new Text(this, sender, languageFileCrowdin, path, args); + return new Text(this, sender, locale2ContentMapping.get(languageFileCrowdin), path, args); } public Text of(@Nullable UUID sender, @NotNull String path, String... args) { - return new Text(this, sender, languageFileCrowdin, path, args); + return new Text(this, sender, locale2ContentMapping.get(languageFileCrowdin), path, args); } public TextList ofList(@NotNull String path, String... args) { - return new TextList(this, (CommandSender) null, languageFileCrowdin, path, args); + return new TextList(this, (CommandSender) null, locale2ContentMapping.get(languageFileCrowdin), path, args); } public TextList ofList(@Nullable UUID sender, @NotNull String path, String... args) { - return new TextList(this, sender, languageFileCrowdin, path, args); + return new TextList(this, sender, locale2ContentMapping.get(languageFileCrowdin), path, args); } public TextList ofList(@Nullable CommandSender sender, @NotNull String path, String... args) { - return new TextList(this, sender, languageFileCrowdin, path, args); + return new TextList(this, sender, locale2ContentMapping.get(languageFileCrowdin), path, args); } public static class TextList { @@ -160,23 +145,23 @@ public class TextManager { private final CommandSender sender; private final String[] args; - private TextList(TextManager manager, CommandSender sender, String file, String path, String... args) { + private TextList(TextManager manager, CommandSender sender, Map mapping, String path, String... args) { this.plugin = manager.plugin; this.manager = manager; this.sender = sender; - this.mapping = manager.locale2ContentMapping.get(file); + this.mapping = mapping; this.path = path; this.args = args; } - private TextList(TextManager manager, UUID sender, String file, String path, String... args) { + private TextList(TextManager manager, UUID sender, Map mapping, String path, String... args) { this.plugin = manager.plugin; this.manager = manager; if (sender != null) this.sender = Bukkit.getPlayer(sender); else this.sender = null; - this.mapping = manager.locale2ContentMapping.get(file); + this.mapping = mapping; this.path = path; this.args = args; } @@ -244,23 +229,23 @@ public class TextManager { private final CommandSender sender; private final String[] args; - private Text(TextManager manager, CommandSender sender, String file, String path, String... args) { + private Text(TextManager manager, CommandSender sender, Map mapping, String path, String... args) { this.plugin = manager.plugin; this.manager = manager; this.sender = sender; - this.mapping = manager.locale2ContentMapping.get(file); + this.mapping = mapping; this.path = path; this.args = args; } - private Text(TextManager manager, UUID sender, String file, String path, String... args) { + private Text(TextManager manager, UUID sender, Map mapping, String path, String... args) { this.plugin = manager.plugin; this.manager = manager; if (sender != null) this.sender = Bukkit.getPlayer(sender); else this.sender = null; - this.mapping = manager.locale2ContentMapping.get(file); + this.mapping = mapping; this.path = path; this.args = args; } diff --git a/src/main/resources/bundled/messages.json b/src/main/resources/lang-original/messages.json similarity index 100% rename from src/main/resources/bundled/messages.json rename to src/main/resources/lang-original/messages.json