Implemented prefixes in localizations and a default prefix for BentoBox

Implements https://github.com/BentoBoxWorld/BentoBox/issues/1086

started working on prefixes

added BentoBox prefix
This commit is contained in:
Florian CUNY 2020-03-20 11:10:44 +01:00
parent e7b1f51a83
commit 71fb955114
4 changed files with 104 additions and 30 deletions

View File

@ -1,9 +1,12 @@
package world.bentobox.bentobox.api.localization;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.NonNull;
@ -22,6 +25,12 @@ public class BentoBoxLocale {
private ItemStack banner;
private List<String> authors;
/**
* List of available prefixes in this locale.
* @since 1.12.0
*/
private Set<String> prefixes;
public BentoBoxLocale(Locale locale, YamlConfiguration config) {
this.locale = locale;
this.config = config;
@ -32,6 +41,10 @@ public class BentoBoxLocale {
// Load authors from the configuration
authors = new LinkedList<>();
updateAuthors(config);
// Load prefixes from the configuration
prefixes = new HashSet<>();
updatePrefixes(config);
}
/**
@ -104,6 +117,7 @@ public class BentoBoxLocale {
}
}
updateAuthors(toBeMerged);
updatePrefixes(toBeMerged);
}
/**
@ -132,10 +146,27 @@ public class BentoBoxLocale {
}
}
private void updatePrefixes(YamlConfiguration yamlConfiguration) {
ConfigurationSection prefixesConfigSection = yamlConfiguration.getConfigurationSection("prefixes");
if (prefixesConfigSection != null) {
prefixes.addAll(prefixesConfigSection.getKeys(false));
}
}
/**
* @return the config
*/
public YamlConfiguration getConfig() {
return config;
}
/**
* Returns the list of prefixes available in this locale.
* @return list of prefixes available in this locale.
* @since 1.12.0
*/
@NonNull
public Set<String> getPrefixes() {
return prefixes;
}
}

View File

@ -377,19 +377,36 @@ public class User {
}
}
// Then replace variables
if (variables.length > 1) {
for (int i = 0; i < variables.length; i += 2) {
translation = translation.replace(variables[i], variables[i+1]);
// If this is a prefix, just gather and return the translation
if (reference.startsWith("prefixes.")) {
return translation;
} else {
// Replace the prefixes
for (String prefix : plugin.getLocalesManager().getAvailablePrefixes(this)) {
String prefixTranslation = getTranslation("prefixes." + prefix);
// Replace the [gamemode] text variable
prefixTranslation = prefixTranslation.replace("[gamemode]", addon != null ? addon.getDescription().getName() : "[gamemode]");
// Replace the [friendly_name] text variable
prefixTranslation = prefixTranslation.replace("[friendly_name]", getWorld() != null ? plugin.getIWM().getFriendlyName(getWorld()) : "[friendly_name]");
// Replace the prefix in the actual message
translation = translation.replace("[prefix_" + prefix + "]", prefixTranslation);
}
}
// Then replace Placeholders, this will only work if this is a player
if (player != null) {
translation = plugin.getPlaceholdersManager().replacePlaceholders(player, translation);
}
// Then replace variables
if (variables.length > 1) {
for (int i = 0; i < variables.length; i += 2) {
translation = translation.replace(variables[i], variables[i + 1]);
}
}
return Util.stripSpaceAfterColorCodes(ChatColor.translateAlternateColorCodes('&', translation));
// Then replace Placeholders, this will only work if this is a player
if (player != null) {
translation = plugin.getPlaceholdersManager().replacePlaceholders(player, translation);
}
return Util.stripSpaceAfterColorCodes(ChatColor.translateAlternateColorCodes('&', translation));
}
}
/**
@ -604,5 +621,4 @@ public class User {
public void setAddon(Addon addon) {
this.addon = addon;
}
}

View File

@ -7,11 +7,13 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.jar.JarFile;
import org.bukkit.Bukkit;
@ -19,6 +21,7 @@ import org.bukkit.ChatColor;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
@ -102,10 +105,10 @@ public class LocalesManager {
}
/**
* Gets the translated String corresponding to the reference from the server's or the en-US locale file
* Gets the translated String corresponding to the reference from the server's or the en-US locale file.
* or if it cannot be found anywhere, use the default text supplied.
* @param reference a reference that can be found in a locale file
* @param defaultText text to return if the reference cannot be found anywhere
* @param reference a reference that can be found in a locale file.
* @param defaultText text to return if the reference cannot be found anywhere.
* @return the translated String from the server's locale or from the en-US locale, or default.
*/
public String getOrDefault(String reference, String defaultText) {
@ -113,6 +116,30 @@ public class LocalesManager {
return result == null ? defaultText : result;
}
/**
* Gets the list of prefixes from the user's locale, the server's locale and the en-US locale file.
* @param user the user to get the locale, not null.
* @return the list of prefixes from the user's locale, the server's locale and the en-US locale file.
* @since 1.12.0
*/
public Set<String> getAvailablePrefixes(@NonNull User user) {
Set<String> prefixes = new HashSet<>();
// Get the player locale
BentoBoxLocale locale = languages.get(user.getLocale());
if (locale != null) {
prefixes.addAll(locale.getPrefixes());
}
// Get the prefixes from the server's locale
prefixes.addAll(languages.get(Locale.forLanguageTag(plugin.getSettings().getDefaultLanguage())).getPrefixes());
// Get the prefixes from the en-US locale
prefixes.addAll(languages.get(Locale.forLanguageTag("en-US")).getPrefixes());
return prefixes;
}
/**
* Copies locale files from the addon jar to the file system and updates current locales with the latest references
* @param addon - addon
@ -152,7 +179,6 @@ public class LocalesManager {
plugin.logError("Error updating locale file: " + lf + " : " + e.getMessage());
plugin.logStacktrace(e);
}
}
/**
@ -332,7 +358,6 @@ public class LocalesManager {
* @since 1.5.0
*/
private void analyze(User user) {
user.sendRawMessage(ChatColor.GREEN + "The following locales are supported:");
languages.forEach((k,v) -> user.sendRawMessage(ChatColor.GOLD + k.toLanguageTag() + " " + k.getDisplayLanguage() + " " + k.getDisplayCountry()));
// Start with US English

View File

@ -11,6 +11,8 @@ meta:
- Poslovitch
banner: "WHITE_BANNER:1:STRIPE_SMALL:RED:SQUARE_TOP_RIGHT:CYAN:SQUARE_TOP_RIGHT:BLUE"
prefixes:
bentobox: '&6 BentoBox &7 &l > &r '
general:
success: "&a Success!"
invalid: "Invalid"
@ -386,13 +388,13 @@ commands:
description: "displays copyright and license information"
reload:
description: "reloads BentoBox and all addons, settings and locales"
locales-reloaded: "&2 Languages reloaded."
addons-reloaded: "&2 Addons reloaded."
settings-reloaded: "&2 Settings reloaded."
addon: "&6 Reloading &b [name]&2 ."
addon-reloaded: "&b [name] &2 reloaded."
warning: "&c Warning: Reloading may cause instability, so if you see errors afterwards, restart the server."
unknown-addon: "&c Unknown addon!"
locales-reloaded: "[prefix_bentobox]&2 Languages reloaded."
addons-reloaded: "[prefix_bentobox]&2 Addons reloaded."
settings-reloaded: "[prefix_bentobox]&2 Settings reloaded."
addon: "[prefix_bentobox]&6 Reloading &b [name]&2 ."
addon-reloaded: "[prefix_bentobox]&b [name] &2 reloaded."
warning: "[prefix_bentobox]&c Warning: Reloading may cause instability, so if you see errors afterwards, restart the server."
unknown-addon: "[prefix_bentobox]&c Unknown addon!"
version:
plugin-version: "&2 BentoBox version: &3 [version]"
description: "displays BentoBox and addons versions"
@ -409,15 +411,15 @@ commands:
locale:
description: "performs localization files analysis"
see-console: |-
&a Check the console to see the feedback.
&a This command is so spammy that the feedback cannot be read from chat...
[prefix_bentobox]&a Check the console to see the feedback.
[prefix_bentobox]&a This command is so spammy that the feedback cannot be read from chat...
migrate:
description: "migrates data from one database to another"
players: "&6 Migrating players"
names: "&6 Migrating names"
addons: "&6 Migrating addons"
class: "&6 Migrating [description]"
migrated: "&A Migrated"
players: "[prefix_bentobox]&6 Migrating players"
names: "[prefix_bentobox]&6 Migrating names"
addons: "[prefix_bentobox]&6 Migrating addons"
class: "[prefix_bentobox]&6 Migrating [description]"
migrated: "[prefix_bentobox]&a Migrated"
confirmation:
confirm: "&c Type command again within &b [seconds]s&c to confirm."