mirror of
https://github.com/EarthMC/QuickShop-Reremake.git
synced 2026-11-25 18:09:48 -05:00
Revert "Full support for multi-file and multi-lanuages with overrides and bundled supports"
This reverts commit 954ea6ee1d.
This commit is contained in:
1 parent
954ea6ee1d
commit
05babb83a0
5 files changed
+68
-83
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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<String, Map<String, JsonConfiguration>> locale2ContentMapping = new HashMap<>();
|
||||
private final static String languageFileCrowdin = "/master/src/main/resources/lang/%locale%/messages.json";
|
||||
public List<PostProcessor> postProcessors = new ArrayList<>();
|
||||
// <File, Section>
|
||||
private final Map<String, JsonConfiguration> 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<String, JsonConfiguration> 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<String, JsonConfiguration> 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<String, JsonConfiguration> 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<String, JsonConfiguration> 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;
|
||||
}
|
||||
|
||||
File renamed without changes.
Reference in new issue
Block a user