1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 11:15:51 +00:00

Complete restructure of language files

This commit is contained in:
TheBusyBiscuit 2021-04-23 20:48:24 +02:00
parent 3ad981aa1e
commit 1c5372d261
139 changed files with 123 additions and 110 deletions

View File

@ -23,4 +23,4 @@ rules:
## Trailing Spaces for language files are too common and these files are automated anyway
trailing-spaces:
ignore: |
/src/main/resources/languages/*.yml
/src/main/resources/languages/**/*.yml

View File

@ -5,7 +5,7 @@ on:
branches:
- master
paths:
- 'src/main/resources/languages/**_en.yml'
- 'src/main/resources/languages/en/**.yml'
jobs:
notify:

View File

@ -203,11 +203,6 @@
<pattern>kong.unirest</pattern>
<shadedPattern>io.github.thebusybiscuit.slimefun4.libraries.unirest</shadedPattern>
</relocation>
<relocation>
<!-- (Dependency of Unirest) -->
<pattern>org.apache</pattern>
<shadedPattern>io.github.thebusybiscuit.slimefun4.libraries.apache</shadedPattern>
</relocation>
</relocations>
<!-- Exclude unneeded metadata files from shaded dependencies -->

View File

@ -13,6 +13,7 @@ import java.util.logging.Level;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.apache.commons.lang.Validate;
import org.bukkit.NamespacedKey;
@ -24,6 +25,7 @@ import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import io.github.thebusybiscuit.slimefun4.core.services.localization.Language;
import io.github.thebusybiscuit.slimefun4.core.services.localization.LanguageFile;
import io.github.thebusybiscuit.slimefun4.core.services.localization.SlimefunLocalization;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.utils.NumberUtils;
@ -60,7 +62,7 @@ public class LocalizationService extends SlimefunLocalization {
translationsEnabled = SlimefunPlugin.getCfg().getBoolean("options.enable-translations");
defaultLanguage = new Language(serverDefaultLanguage, "11b3188fd44902f72602bd7c2141f5a70673a411adb3d81862c69e536166b");
defaultLanguage.setMessagesFile(getConfig().getConfiguration());
defaultLanguage.setFile(LanguageFile.MESSAGES, getConfig().getConfiguration());
loadEmbeddedLanguages();
@ -122,7 +124,7 @@ public class LocalizationService extends SlimefunLocalization {
public boolean hasLanguage(@Nonnull String id) {
Validate.notNull(id, "The language id cannot be null");
// Checks if our jar files contains a messages.yml file for that language
return containsResource("messages_" + id);
return containsResource(id, LanguageFile.MESSAGES);
}
/**
@ -138,9 +140,11 @@ public class LocalizationService extends SlimefunLocalization {
return languages.containsKey(id);
}
private boolean containsResource(@Nonnull String file) {
Validate.notNull(file, "File name cannot be null!");
return plugin.getClass().getResource("/languages/" + file + ".yml") != null;
private boolean containsResource(@Nonnull String languageId, @Nonnull LanguageFile file) {
Validate.notNull(languageId, "Language id cannot be null");
Validate.notNull(file, "Language file cannot be null");
return plugin.getClass().getResource(file.getFilePath(languageId)) != null;
}
@Override
@ -172,16 +176,18 @@ public class LocalizationService extends SlimefunLocalization {
getConfig().clear();
}
defaultLanguage.setResearchesFile(streamConfigFile("researches_" + language + ".yml", null));
defaultLanguage.setResourcesFile(streamConfigFile("resources_" + language + ".yml", null));
defaultLanguage.setCategoriesFile(streamConfigFile("categories_" + language + ".yml", null));
defaultLanguage.setRecipeTypesFile(streamConfigFile("recipes_" + language + ".yml", null));
// Copy defaults
for (LanguageFile file : LanguageFile.values()) {
if (file != LanguageFile.MESSAGES) {
copyToDefaultLanguage(language, file);
}
}
SlimefunPlugin.logger().log(Level.INFO, "Loaded language \"{0}\"", language);
getConfig().setValue(LANGUAGE_PATH, language);
// Loading in the defaults from our resources folder
String path = "/languages/messages_" + language + ".yml";
String path = "/languages/" + language + "/messages.yml";
try (BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getClass().getResourceAsStream(path), StandardCharsets.UTF_8))) {
FileConfiguration config = YamlConfiguration.loadConfiguration(reader);
@ -193,24 +199,25 @@ public class LocalizationService extends SlimefunLocalization {
save();
}
@ParametersAreNonnullByDefault
private void copyToDefaultLanguage(String language, LanguageFile file) {
FileConfiguration config = streamConfigFile(file.getFilePath(language), null);
defaultLanguage.setFile(file, config);
}
@Override
protected void addLanguage(@Nonnull String id, @Nonnull String texture) {
Validate.notNull(id, "The language id cannot be null!");
Validate.notNull(texture, "The language texture cannot be null");
if (hasLanguage(id)) {
FileConfiguration messages = streamConfigFile("messages_" + id + ".yml", getConfig().getConfiguration());
FileConfiguration researches = streamConfigFile("researches_" + id + ".yml", null);
FileConfiguration resources = streamConfigFile("resources_" + id + ".yml", null);
FileConfiguration categories = streamConfigFile("categories_" + id + ".yml", null);
FileConfiguration recipes = streamConfigFile("recipes_" + id + ".yml", null);
Language language = new Language(id, texture);
language.setMessagesFile(messages);
language.setResearchesFile(researches);
language.setResourcesFile(resources);
language.setCategoriesFile(categories);
language.setRecipeTypesFile(recipes);
for (LanguageFile file : LanguageFile.values()) {
FileConfiguration defaults = file == LanguageFile.MESSAGES ? getConfig().getConfiguration() : null;
FileConfiguration config = streamConfigFile(file.getFilePath(language), defaults);
language.setFile(file, config);
}
languages.put(id, language);
}
@ -263,14 +270,13 @@ public class LocalizationService extends SlimefunLocalization {
return keys;
}
@Nullable
private FileConfiguration streamConfigFile(@Nonnull String file, @Nullable FileConfiguration defaults) {
String path = "/languages/" + file;
if (plugin.getClass().getResourceAsStream(path) == null) {
if (plugin.getClass().getResourceAsStream(file) == null) {
return new YamlConfiguration();
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getClass().getResourceAsStream(path), StandardCharsets.UTF_8))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getClass().getResourceAsStream(file), StandardCharsets.UTF_8))) {
FileConfiguration config = YamlConfiguration.loadConfiguration(reader);
if (defaults != null) {
@ -279,7 +285,7 @@ public class LocalizationService extends SlimefunLocalization {
return config;
} catch (IOException e) {
SlimefunPlugin.logger().log(Level.SEVERE, e, () -> "Failed to load language file into memory: \"" + path + "\"");
SlimefunPlugin.logger().log(Level.SEVERE, e, () -> "Failed to load language file into memory: \"" + file + "\"");
return null;
}
}

View File

@ -1,6 +1,9 @@
package io.github.thebusybiscuit.slimefun4.core.services.localization;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.Locale;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -26,16 +29,12 @@ import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
*/
public final class Language {
private final Map<LanguageFile, FileConfiguration> files = new EnumMap<>(LanguageFile.class);
private final String id;
private final ItemStack item;
private double progress = -1;
private FileConfiguration messages;
private FileConfiguration researches;
private FileConfiguration resources;
private FileConfiguration categories;
private FileConfiguration recipeTypes;
/**
* This instantiates a new {@link Language} with the given language code
* and skull texture.
@ -85,58 +84,15 @@ public final class Language {
}
@Nullable
FileConfiguration getMessagesFile() {
return messages;
FileConfiguration getFile(@Nonnull LanguageFile file) {
return files.get(file);
}
@Nullable
FileConfiguration getResearchesFile() {
return researches;
}
public void setFile(@Nonnull LanguageFile file, @Nonnull FileConfiguration config) {
Validate.notNull(file, "The provided file should not be null.");
Validate.notNull(config, "The provided config should not be null.");
@Nullable
FileConfiguration getResourcesFile() {
return resources;
}
@Nullable
FileConfiguration getCategoriesFile() {
return categories;
}
@Nullable
FileConfiguration getRecipeTypesFile() {
return recipeTypes;
}
public void setMessagesFile(@Nonnull FileConfiguration config) {
Validate.notNull(config);
this.messages = config;
}
public void setResearchesFile(@Nonnull FileConfiguration config) {
Validate.notNull(config);
this.researches = config;
}
public void setResourcesFile(@Nonnull FileConfiguration config) {
Validate.notNull(config);
this.resources = config;
}
public void setCategoriesFile(@Nonnull FileConfiguration config) {
Validate.notNull(config);
this.categories = config;
}
public void setRecipeTypesFile(@Nonnull FileConfiguration config) {
Validate.notNull(config);
this.recipeTypes = config;
files.put(file, config);
}
/**
@ -180,7 +136,7 @@ public final class Language {
@Nonnull
public FileConfiguration[] getFiles() {
return new FileConfiguration[] { getMessagesFile(), getCategoriesFile(), getResearchesFile(), getResourcesFile() };
return Arrays.stream(LanguageFile.valuesCached).map(this::getFile).toArray(FileConfiguration[]::new);
}
}

View File

@ -0,0 +1,43 @@
package io.github.thebusybiscuit.slimefun4.core.services.localization;
import javax.annotation.Nonnull;
import org.apache.commons.lang.Validate;
/**
* This enum holds the different types of files each {@link Language} holds.
*
* @author TheBusyBiscuit
*
* @see Language
* @see SlimefunLocalization
*
*/
public enum LanguageFile {
MESSAGES("messages.yml"),
CATEGORIES("categories.yml"),
RECIPES("recipes.yml"),
RESOURCES("resources.yml"),
RESEARCHES("researches.yml");
static final LanguageFile[] valuesCached = values();
private final String fileName;
LanguageFile(@Nonnull String fileName) {
this.fileName = fileName;
}
@Nonnull
public String getFilePath(@Nonnull Language language) {
return getFilePath(language.getId());
}
@Nonnull
public String getFilePath(@Nonnull String languageId) {
Validate.notNull(languageId, "Language id must not be null!");
return "/languages/" + languageId + '/' + fileName;
}
}

View File

@ -121,6 +121,23 @@ public abstract class SlimefunLocalization extends Localization implements Keyed
}
}
@Nonnull
private final FileConfiguration getFallback(@Nonnull LanguageFile file) {
Language language = getLanguage(SupportedLanguage.ENGLISH.getLanguageId());
if (language == null) {
throw new IllegalStateException("Fallback language \"en\" is missing!");
}
FileConfiguration fallback = language.getFile(file);
if (fallback != null) {
return fallback;
} else {
throw new IllegalStateException("Fallback file: \"" + file.getFilePath("en") + "\" is missing!");
}
}
@Nonnull
@Override
public String getMessage(@Nonnull String key) {
@ -128,11 +145,10 @@ public abstract class SlimefunLocalization extends Localization implements Keyed
Language language = getDefaultLanguage();
String message = language == null ? null : language.getMessagesFile().getString(key);
String message = language == null ? null : language.getFile(LanguageFile.MESSAGES).getString(key);
if (message == null) {
Language fallback = getLanguage(SupportedLanguage.ENGLISH.getLanguageId());
return fallback.getMessagesFile().getString(key);
return getFallback(LanguageFile.MESSAGES).getString(key);
}
return message;
@ -149,11 +165,10 @@ public abstract class SlimefunLocalization extends Localization implements Keyed
return "NO LANGUAGE FOUND";
}
String message = language.getMessagesFile().getString(key);
String message = language.getFile(LanguageFile.MESSAGES).getString(key);
if (message == null) {
Language fallback = getLanguage(SupportedLanguage.ENGLISH.getLanguageId());
return fallback.getMessagesFile().getString(key);
return getFallback(LanguageFile.MESSAGES).getString(key);
}
return message;
@ -170,11 +185,10 @@ public abstract class SlimefunLocalization extends Localization implements Keyed
return Collections.singletonList("NO LANGUAGE FOUND");
}
List<String> messages = language.getMessagesFile().getStringList(key);
List<String> messages = language.getFile(LanguageFile.MESSAGES).getStringList(key);
if (messages.isEmpty()) {
Language fallback = getLanguage(SupportedLanguage.ENGLISH.getLanguageId());
return fallback.getMessagesFile().getStringList(key);
return getFallback(LanguageFile.MESSAGES).getStringList(key);
}
return messages;
@ -200,11 +214,11 @@ public abstract class SlimefunLocalization extends Localization implements Keyed
Language language = getLanguage(p);
if (language == null || language.getResearchesFile() == null) {
if (language == null || language.getFile(LanguageFile.RESEARCHES) == null) {
return null;
}
return language.getResearchesFile().getString(key.getNamespace() + '.' + key.getKey());
return language.getFile(LanguageFile.RESEARCHES).getString(key.getNamespace() + '.' + key.getKey());
}
@Nullable
@ -214,11 +228,11 @@ public abstract class SlimefunLocalization extends Localization implements Keyed
Language language = getLanguage(p);
if (language == null || language.getCategoriesFile() == null) {
if (language == null || language.getFile(LanguageFile.CATEGORIES) == null) {
return null;
}
return language.getCategoriesFile().getString(key.getNamespace() + '.' + key.getKey());
return language.getFile(LanguageFile.CATEGORIES).getString(key.getNamespace() + '.' + key.getKey());
}
@Nullable
@ -228,13 +242,12 @@ public abstract class SlimefunLocalization extends Localization implements Keyed
Language language = getLanguage(p);
String value = language != null && language.getResourcesFile() != null ? language.getResourcesFile().getString(key) : null;
String value = language != null && language.getFile(LanguageFile.RESOURCES) != null ? language.getFile(LanguageFile.RESOURCES).getString(key) : null;
if (value != null) {
return value;
} else {
Language fallback = getLanguage(SupportedLanguage.ENGLISH.getLanguageId());
return fallback.getResourcesFile().getString(key);
return getFallback(LanguageFile.RESOURCES).getString(key);
}
}
@ -247,15 +260,15 @@ public abstract class SlimefunLocalization extends Localization implements Keyed
ItemStack item = recipeType.toItem();
NamespacedKey key = recipeType.getKey();
if (language == null || language.getRecipeTypesFile() == null || !language.getRecipeTypesFile().contains(key.getNamespace() + '.' + key.getKey())) {
if (language == null || language.getFile(LanguageFile.RECIPES) == null || !language.getFile(LanguageFile.RECIPES).contains(key.getNamespace() + '.' + key.getKey())) {
language = getLanguage("en");
}
if (!language.getRecipeTypesFile().contains(key.getNamespace() + '.' + key.getKey())) {
if (!language.getFile(LanguageFile.RECIPES).contains(key.getNamespace() + '.' + key.getKey())) {
return item;
}
FileConfiguration config = language.getRecipeTypesFile();
FileConfiguration config = language.getFile(LanguageFile.RECIPES);
return new CustomItem(item, meta -> {
meta.setDisplayName(ChatColor.AQUA + config.getString(key.getNamespace() + "." + key.getKey() + ".name"));

Some files were not shown because too many files have changed in this diff Show More