From 8e04a2bcf36dff4c97cd4111cc1017045653853e Mon Sep 17 00:00:00 2001 From: Adrien Prokopowicz Date: Mon, 18 Jul 2016 08:41:35 +0200 Subject: [PATCH 1/8] Started working on additional I18n APIs. * NEW: I18nText is a new structure holding translation data, separating runtime arguments from the rest and making it easier for gettext to detect. * NEW: I.i() is a new set of method allowing to easily generate I18nText objects. * NEW: The I.t() set of methods can now use I18nText objects. * NEW: The I18n.translate() set of methods can now use I18nText objects. --- .../fr/zcraft/zlib/components/i18n/I.java | 24 ++++++ .../fr/zcraft/zlib/components/i18n/I18n.java | 5 ++ .../zcraft/zlib/components/i18n/I18nText.java | 84 +++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 src/main/java/fr/zcraft/zlib/components/i18n/I18nText.java diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/I.java b/src/main/java/fr/zcraft/zlib/components/i18n/I.java index e3b3b3ef..72751c23 100644 --- a/src/main/java/fr/zcraft/zlib/components/i18n/I.java +++ b/src/main/java/fr/zcraft/zlib/components/i18n/I.java @@ -87,6 +87,16 @@ public static String t(String text, Object... parameters) { return I18n.translate(null, null, text, null, null, parameters); } + + public static String t(Locale locale, I18nText text, Object... parameters) + { + return I18n.translate(locale, text, parameters); + } + + public static String t(I18nText text, Object... parameters) + { + return t(null, text, parameters); + } /** * Translates the string with a plural. @@ -305,4 +315,18 @@ public static void sendTcn(Player player, String context, String singular, Strin player.sendMessage(I18n.translate(I18n.getPlayerLocale(player), context, singular, plural, count, parameters)); } + public static I18nText i(String messageId) + { + return i(messageId, null, null, null); + } + + public static I18nText i(String messageId, String pluralMessageId, Integer count) + { + return i(messageId, pluralMessageId, count, null); + } + + public static I18nText i(String messageId, String pluralMessageId, Integer count, String context) + { + return new I18nText(messageId, pluralMessageId, count, context); + } } diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java b/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java index 71bc36fd..6355d044 100644 --- a/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java +++ b/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java @@ -647,6 +647,11 @@ public static String translate(String context, String messageId, String messageI return translate(null, context, messageId, messageIdPlural, count, parameters); } + public static String translate(Locale locale, I18nText text, Object... parameters) + { + return translate(null, text.getContext(), text.getMessageId(), text.getMessageId(), text.getCount(), parameters); + } + /** * Replaces some formatting codes into system codes. * diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/I18nText.java b/src/main/java/fr/zcraft/zlib/components/i18n/I18nText.java new file mode 100644 index 00000000..cdf38fb0 --- /dev/null +++ b/src/main/java/fr/zcraft/zlib/components/i18n/I18nText.java @@ -0,0 +1,84 @@ +/* + * Copyright or © or Copr. ZLib contributors (2015 - 2016) + * + * This software is governed by the CeCILL-B license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL-B + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL-B license and that you accept its terms. + */ + +package fr.zcraft.zlib.components.i18n; + +public class I18nText +{ + private final String messageId; + private final String pluralMessageId; + private final Integer count; + private final String context; + + private Object[] parameters; + + public I18nText(String messageId, String pluralMessageId, Integer count, String context) + { + this.messageId = messageId; + this.pluralMessageId = pluralMessageId; + this.count = count; + this.context = context; + } + + public I18nText(String messageId) + { + this(messageId, null, null, null); + } + + public String getMessageId() + { + return messageId; + } + + public String getPluralMessageId() + { + return pluralMessageId; + } + + public Integer getCount() + { + return count; + } + + public String getContext() + { + return context; + } + + public Object[] getParameters() + { + return parameters; + } + + public void setParameters(Object[] parameters) + { + this.parameters = parameters; + } +} From fa53d42467ee679647074fd497dcd4b95684623d Mon Sep 17 00:00:00 2001 From: Adrien Prokopowicz Date: Mon, 18 Jul 2016 16:23:03 +0200 Subject: [PATCH 2/8] Added I18nText integration to ItemStackBuilder and GUI APIs. * NEW: ActionGui now sets the locale for the ItemStackBuilders it returns. * NEW: ItemStackBuilder now handles locale and I18nTexts. * NEW: GuiBase: sendMessage() is a new set of method to send (translated) messages to the user of the GUI. * NEW: InventoryGui: setTitle() now handles I18nTexts. * BUG: I18n: fixed I18nText translation handling. * BUG: I18n: added some missing documentation. --- .../zcraft/zlib/components/gui/ActionGui.java | 10 +- .../zcraft/zlib/components/gui/GuiBase.java | 23 ++++- .../zlib/components/gui/InventoryGui.java | 13 +++ .../fr/zcraft/zlib/components/i18n/I18n.java | 19 +++- .../zlib/tools/items/ItemStackBuilder.java | 97 ++++++++++++++++++- 5 files changed, 150 insertions(+), 12 deletions(-) diff --git a/src/main/java/fr/zcraft/zlib/components/gui/ActionGui.java b/src/main/java/fr/zcraft/zlib/components/gui/ActionGui.java index 41870d8f..5001edab 100644 --- a/src/main/java/fr/zcraft/zlib/components/gui/ActionGui.java +++ b/src/main/java/fr/zcraft/zlib/components/gui/ActionGui.java @@ -130,10 +130,12 @@ protected void action(String name, int slot, ItemStack item, String title, List< * @param name The identifier of the action. * @param slot The slot the action will be placed on. * @param material The material used to represent the action. + * @return an {@link fr.zcraft.zlib.tools.items.ItemStackBuilder} ItemStackBuilder to build the representing item */ - protected void action(String name, int slot, Material material) + protected ItemStackBuilder action(String name, int slot, Material material) { - action(name, slot, GuiUtils.makeItem(material)); + return action(name, slot) + .material(material); } /** @@ -180,7 +182,7 @@ protected ItemStackBuilder action(String name, int slot, ItemStack item) action(action); - if(item == null) return action.updateItem(); + if(item == null) return action.updateItem().locale(getPlayerLocale()); return null; } @@ -243,7 +245,7 @@ protected ItemStackBuilder updateAction(String name, Material material) protected ItemStackBuilder updateAction(String name) { - return getAction(name).updateItem(); + return getAction(name).updateItem().locale(getPlayerLocale()); } /** diff --git a/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java b/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java index 57f45875..ef727f79 100644 --- a/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java +++ b/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java @@ -30,7 +30,9 @@ package fr.zcraft.zlib.components.gui; +import fr.zcraft.zlib.components.i18n.I; import fr.zcraft.zlib.components.i18n.I18n; +import fr.zcraft.zlib.components.i18n.I18nText; import fr.zcraft.zlib.core.ZLib; import java.util.Locale; import org.bukkit.entity.Player; @@ -164,6 +166,25 @@ void setParent(GuiBase parent) /** @return The locale used by the player this Gui instance is associated to. */ protected final Locale getPlayerLocale() { return playerLocale; } + /** + * Sends a message to the player this Gui instance is associated to. + * @param message The message to send to the player. + */ + protected void sendMessage(String message) + { + getPlayer().sendMessage(message); + } + + /** + * Sends a message to the player this Gui instance is associated to. + * @param message The message to send to the player. + * @param parameters Parameters for the translatable format text, if any. + */ + protected void sendMessage(I18nText message, Object... parameters) + { + sendMessage(I.t(message, parameters)); + } + protected boolean checkImmune() { if(!immune) return false; @@ -176,6 +197,4 @@ private void setImmune(boolean immune) this.immune = immune; } - /* ===== Static API ===== */ - } diff --git a/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java b/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java index fbe48c89..4a1a9b43 100644 --- a/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java +++ b/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java @@ -30,6 +30,8 @@ package fr.zcraft.zlib.components.gui; +import fr.zcraft.zlib.components.i18n.I; +import fr.zcraft.zlib.components.i18n.I18nText; import fr.zcraft.zlib.tools.items.InventoryUtils; import fr.zcraft.zlib.tools.runners.RunTask; import org.bukkit.Bukkit; @@ -234,6 +236,17 @@ protected void setTitle(String title) this.title = title; } + /** + * Sets the new title of the inventory. + * It will be applied on the next GUI update. + * @param title The new title of the inventory + * @param parameters Parameters for the translatable format text, if any. + */ + protected void setTitle(I18nText title, Object ...parameters) + { + setTitle(I.t(getPlayerLocale(), title, parameters)); + } + /** @return The underlying inventory, or null if the Gui has not been opened yet. */ public Inventory getInventory() { return inventory; } diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java b/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java index 6355d044..478c4261 100644 --- a/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java +++ b/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java @@ -647,9 +647,26 @@ public static String translate(String context, String messageId, String messageI return translate(null, context, messageId, messageIdPlural, count, parameters); } + /** + * Translates the given string. + * + *

Tries to use the primary locale; fallbacks to the fallback locale if the string cannot be + * translated; fallbacks to the input text if the string still cannot be translated.

+ * + *

The count is likely to be used in the string, so if, for a translation with plurals, only + * a count is given, this count is also interpreted as a parameter (the first and only one, {@code + * {0}}). If this behavior annoys you, you can disable it using {@link + * #addCountToParameters(boolean)}.

+ * @param locale The locale to use to translate the string. + * @param text The text to translate + * @param parameters The parameters, replacing values like {@code {0}} in the translated + * string. + * + * @return The translated text, with the parameters replaced by their values. + */ public static String translate(Locale locale, I18nText text, Object... parameters) { - return translate(null, text.getContext(), text.getMessageId(), text.getMessageId(), text.getCount(), parameters); + return translate(null, text.getContext(), text.getMessageId(), text.getPluralMessageId(), text.getCount(), parameters); } /** diff --git a/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java b/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java index 6455a9f4..12edad2c 100644 --- a/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java +++ b/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java @@ -31,6 +31,8 @@ package fr.zcraft.zlib.tools.items; import fr.zcraft.zlib.components.gui.GuiUtils; +import fr.zcraft.zlib.components.i18n.I; +import fr.zcraft.zlib.components.i18n.I18nText; import fr.zcraft.zlib.components.rawtext.RawText; import fr.zcraft.zlib.components.rawtext.RawTextPart; import fr.zcraft.zlib.tools.PluginLogger; @@ -46,8 +48,10 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Map.Entry; @@ -74,6 +78,8 @@ public class ItemStackBuilder private DyeColor dye = null; + private Locale locale = null; + /** * Creates a new ItemStackBuilder. */ @@ -284,6 +290,20 @@ public ItemStackBuilder title(ChatColor color, String... texts) return this; } + /** + * Sets the title of the ItemStack. If a text has already been defined, it + * will be appended to the already existing one. + * + * @param text The text. It will be translated using the locale currently set for the ItemStack. + * @param parameters Parameters for the translatable format text, if any. + * + * @return The current ItemStackBuilder instance, for methods chaining. + */ + public ItemStackBuilder title(I18nText text, Object ...parameters) + { + return title(I.t(locale, text, parameters)); + } + /** * Sets the title of the ItemStack. If a text has already been defined, it * will be appended to the already existing one. @@ -295,7 +315,7 @@ public ItemStackBuilder title(ChatColor color, String... texts) */ public ItemStackBuilder title(String... texts) { - return title(null, texts); + return title((ChatColor) null, texts); } /** @@ -318,7 +338,7 @@ public ItemStackBuilder lore(String... lines) * * @return The current ItemStackBuilder instance, for methods chaining. */ - public ItemStackBuilder lore(List lines) + public ItemStackBuilder lore(Collection lines) { loreLines.addAll(lines); return this; @@ -334,7 +354,34 @@ public ItemStackBuilder lore(List lines) */ public ItemStackBuilder loreLine(String... text) { - return loreLine(null, text); + return loreLine((ChatColor) null, text); + } + + /** + * Adds one line of lore to the ItemStack. + * + * @param text The lore line's text. It will be translated using the locale currently set for the ItemStack. + * @param parameters Parameters for the translatable format text, if any. + * + * @return The current ItemStackBuilder instance, for methods chaining. + */ + public ItemStackBuilder loreLine(I18nText text, Object ...parameters) + { + return loreLine(I.t(locale, text, parameters)); + } + + /** + * Adds one line of lore to the ItemStack. + * This method is an alias for {@link #loreLine(fr.zcraft.zlib.components.i18n.I18nText, java.lang.Object...) }. + * + * @param text The lore line's text. It will be translated using the locale currently set for the ItemStack. + * @param parameters Parameters for the translatable format text, if any. + * + * @return The current ItemStackBuilder instance, for methods chaining. + */ + public ItemStackBuilder lore(I18nText text, Object ...parameters) + { + return loreLine(text, parameters); } /** @@ -387,7 +434,7 @@ public ItemStackBuilder longLore(String text) * characters, so the tooltip is not too large. * * @param text The text. - * @param lineLength The max length of a line. + * @param lineLength The maximum length of a line. * * @return The current ItemStackBuilder instance, for methods chaining. * @see GuiUtils#generateLore(String, int) @@ -418,7 +465,7 @@ public ItemStackBuilder longLore(ChatColor color, String text) * * @param color The color for this line of lore. * @param text The text. - * @param lineLength The max length of a line. + * @param lineLength The maximum length of a line. * * @return The current ItemStackBuilder instance, for methods chaining. * @see GuiUtils#generateLore(String, int) @@ -427,6 +474,35 @@ public ItemStackBuilder longLore(ChatColor color, String text, int lineLength) { return longLore(color + text, lineLength); } + + /** + * Adds a line of lore and wraps it to lines of {@code lineLength} + * characters, so the tooltip is not too large. + * + * @param lineLength The maximum length of a line. + * @param text The lore line's text. It will be translated using the locale currently set for the ItemStack. + * @param parameters Parameters for the translatable format text, if any. + * + * @return The current ItemStackBuilder instance, for methods chaining. + */ + public ItemStackBuilder longLore(int lineLength, I18nText text, Object ...parameters) + { + return longLore(I.t(locale, text, parameters), lineLength); + } + + /** + * Adds a line of lore and wraps it to lines of {@code lineLength} + * characters, so the tooltip is not too large. + * + * @param text The lore line's text. It will be translated using the locale currently set for the ItemStack. + * @param parameters Parameters for the translatable format text, if any. + * + * @return The current ItemStackBuilder instance, for methods chaining. + */ + public ItemStackBuilder longLore(I18nText text, Object ...parameters) + { + return longLore(I.t(locale, text, parameters)); + } /** * Adds a glow effect (a fake enchantment) to the ItemStack. This can only @@ -540,6 +616,17 @@ public ItemStackBuilder dye(DyeColor dye) this.dye = dye; return this; } + + /** + * Sets the locale used to translate different strings of the ItemStack. + * @param locale The locale to use. + * @return The current ItemStackBuilder instance, for methods chaining. + */ + public ItemStackBuilder locale(Locale locale) + { + this.locale = locale; + return this; + } /** * Concatenates a String[] to a single one. From 23ffa83cc0ab6302d0235df124deba7d1204780c Mon Sep 17 00:00:00 2001 From: Adrien Prokopowicz Date: Mon, 18 Jul 2016 17:25:56 +0200 Subject: [PATCH 3/8] Added I18nText integration to the RawText API, and affiliates. * NEW: RawText and RawTextPart now handles locale and I18nTexts. * NEW: Command now sets the locale for the RawText it takes. * NEW: ItemStackBuilder now sets the locale for the RawText it takes. * NEW: ListHeaderFooter now sets the locale for the RawText it takes. * NEW: MessageSender now sets the locale for the RawText it takes. * NEW: RawMessage now sets the locale for the RawText it takes. * NEW: Titles now sets the locale for the RawText it takes. --- .../zlib/components/commands/Command.java | 23 ++++++ .../zlib/components/rawtext/RawTextPart.java | 74 +++++++++++++++++-- .../components/rawtext/RawTextSubPart.java | 12 +++ .../zlib/tools/items/ItemStackBuilder.java | 10 ++- .../zlib/tools/text/ListHeaderFooter.java | 13 ++++ .../zcraft/zlib/tools/text/MessageSender.java | 3 + .../fr/zcraft/zlib/tools/text/RawMessage.java | 4 + .../fr/zcraft/zlib/tools/text/Titles.java | 13 ++++ 8 files changed, 145 insertions(+), 7 deletions(-) diff --git a/src/main/java/fr/zcraft/zlib/components/commands/Command.java b/src/main/java/fr/zcraft/zlib/components/commands/Command.java index c9d08f3b..d5f4d4ec 100644 --- a/src/main/java/fr/zcraft/zlib/components/commands/Command.java +++ b/src/main/java/fr/zcraft/zlib/components/commands/Command.java @@ -31,6 +31,7 @@ package fr.zcraft.zlib.components.commands; import fr.zcraft.zlib.components.commands.CommandException.Reason; +import fr.zcraft.zlib.components.i18n.I18n; import fr.zcraft.zlib.components.rawtext.RawText; import fr.zcraft.zlib.core.ZLib; import fr.zcraft.zlib.tools.text.RawMessage; @@ -42,6 +43,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Locale; abstract public class Command { @@ -54,6 +56,8 @@ abstract public class Command protected CommandSender sender; protected String[] args; + private Locale senderLocale; + abstract protected void run() throws CommandException; void init(CommandGroup commandGroup) @@ -187,6 +191,22 @@ protected Player playerSender() throws CommandException throw new CommandException(this, Reason.COMMANDSENDER_EXPECTED_PLAYER); return (Player)sender; } + + protected Locale getSenderLocale() + { + if(senderLocale != null) return senderLocale; + + if(sender instanceof Player) + senderLocale = I18n.getPlayerLocale((Player) sender); + + if(senderLocale == null) + senderLocale = I18n.getPrimaryLocale(); + + if(senderLocale == null) + senderLocale = I18n.getFallbackLocale(); + + return senderLocale; + } ///////////// Methods for command execution ///////////// @@ -232,6 +252,9 @@ protected void tellRaw(String rawMessage) throws CommandException protected void send(RawText text) { + if(text.getLocale() == null) + text.locale(getSenderLocale()); + RawMessage.send(sender, text); } diff --git a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java index 4604dc3a..85225d98 100644 --- a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java +++ b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java @@ -34,6 +34,8 @@ import com.google.common.base.CaseFormat; import fr.zcraft.zlib.components.commands.Command; import fr.zcraft.zlib.components.commands.Commands; +import fr.zcraft.zlib.components.i18n.I; +import fr.zcraft.zlib.components.i18n.I18nText; import fr.zcraft.zlib.tools.PluginLogger; import fr.zcraft.zlib.tools.items.ItemUtils; import fr.zcraft.zlib.tools.reflection.NMSException; @@ -50,6 +52,7 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Iterator; +import java.util.Locale; public abstract class RawTextPart> implements Iterable, JSONAware { @@ -68,7 +71,9 @@ static private enum ActionHover SHOW_ENTITY } - private String text; + private String text = null; + private I18nText i18nText = null; + private Object[] textParameters = null; private boolean translate = false; private final RawTextPart parent; @@ -89,9 +94,11 @@ static private enum ActionHover private ActionHover actionHover = null; private Object actionHoverValue = null; + private Locale locale = null; + RawTextPart() { - this(null); + this((String) null); } RawTextPart(String text) @@ -99,15 +106,27 @@ static private enum ActionHover this(text, null); } + RawTextPart(I18nText text) + { + this(null, text); + } + RawTextPart(String text, RawTextPart parent) { this.text = text; this.parent = parent; } + RawTextPart(RawTextPart parent, I18nText text, Object... parameters) + { + this.i18nText = text; + this.parent = parent; + this.textParameters = parameters; + } + public RawTextPart then() { - return then(null); + return then((String) null); } public RawTextPart then(String text) @@ -119,17 +138,38 @@ public RawTextPart then(String text) return newPart; } + public RawTextPart then(I18nText text, Object... parameters) + { + RawTextPart root = getRoot(); + RawTextPart newPart = new RawTextSubPart(root, text, parameters); + + root.extra.add(newPart); + return newPart; + } + public T text(String text) { this.text = text; + this.i18nText = null; this.translate = false; return (T)this; } + public T text(I18nText text, Object... parameters) + { + this.text = null; + this.i18nText = text; + this.translate = false; + this.textParameters = parameters; + + return (T)this; + } + public T translate(String text) { this.text = text; + this.i18nText = null; this.translate = true; return (T)this; @@ -152,6 +192,19 @@ public T translate(ItemStack item) return translate(trName); } + public T locale(Locale locale) + { + this.locale = locale; + return (T)this; + } + + public Locale getLocale() + { + if(this.locale == null && this.parent != null) + return this.parent.getLocale(); + return this.locale; + } + public T color(ChatColor color) { if(this.color != null) throw new IllegalStateException("Color already set."); @@ -312,7 +365,7 @@ public JSONObject toJSON() } else { - obj.put("text", text); + obj.put("text", getText()); } if(!extra.isEmpty()) @@ -358,7 +411,7 @@ public String toPlainText() private void writePlainText(StringBuilder buf) { - buf.append(text); + buf.append(getText()); for(RawTextPart subPart : this) { @@ -382,14 +435,23 @@ private void writeFormattedText(StringBuilder buf) if(strikethrough) buf.append(ChatColor.STRIKETHROUGH); if(obfuscated) buf.append(ChatColor.MAGIC); - buf.append(text); + buf.append(getText()); buf.append(ChatColor.RESET); for(RawTextPart subPart : this) { subPart.writeFormattedText(buf); } + } + + private String getText() + { + if(i18nText != null) + { + return I.t(getLocale(), i18nText, textParameters); + } + return text; } @Override diff --git a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextSubPart.java b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextSubPart.java index ee6788e2..0d449ebf 100644 --- a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextSubPart.java +++ b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextSubPart.java @@ -30,6 +30,8 @@ package fr.zcraft.zlib.components.rawtext; +import fr.zcraft.zlib.components.i18n.I18nText; + class RawTextSubPart extends RawTextPart { public RawTextSubPart(String text) @@ -37,8 +39,18 @@ public RawTextSubPart(String text) super(text); } + public RawTextSubPart(I18nText text) + { + super(text); + } + public RawTextSubPart(String text, RawTextPart parent) { super(text, parent); } + + public RawTextSubPart(RawTextPart parent, I18nText text, Object... parameters) + { + super(parent, text, parameters); + } } diff --git a/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java b/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java index 12edad2c..01545d31 100644 --- a/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java +++ b/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java @@ -168,7 +168,12 @@ public ItemStack item() ItemMeta meta = newItemStack.getItemMeta(); if (title != null) + { + if(title.getLocale() == null) + title.locale(locale); + meta.setDisplayName(ChatColor.RESET + title.build().toFormattedText()); + } if (!loreLines.isEmpty()) meta.setLore(loreLines); @@ -260,7 +265,7 @@ public ItemStackBuilder title(RawTextPart text) { if (this.title != null) throw new IllegalStateException("Title has already been defined."); - + this.title = text; return this; } @@ -412,6 +417,9 @@ public ItemStackBuilder loreLine(ChatColor color, String... text) */ public ItemStackBuilder loreLine(RawTextPart rawText) { + if(rawText.getLocale() == null) + rawText.locale(locale); + return loreLine(rawText.toFormattedText()); } diff --git a/src/main/java/fr/zcraft/zlib/tools/text/ListHeaderFooter.java b/src/main/java/fr/zcraft/zlib/tools/text/ListHeaderFooter.java index 0c64ee3b..57c06071 100644 --- a/src/main/java/fr/zcraft/zlib/tools/text/ListHeaderFooter.java +++ b/src/main/java/fr/zcraft/zlib/tools/text/ListHeaderFooter.java @@ -29,6 +29,7 @@ */ package fr.zcraft.zlib.tools.text; +import fr.zcraft.zlib.components.i18n.I18n; import fr.zcraft.zlib.components.rawtext.RawText; import fr.zcraft.zlib.exceptions.IncompatibleMinecraftVersionException; import fr.zcraft.zlib.tools.reflection.NMSNetwork; @@ -37,6 +38,7 @@ import org.bukkit.entity.Player; import java.lang.reflect.InvocationTargetException; +import java.util.Locale; /** @@ -108,6 +110,17 @@ public static void sendListHeaderFooter(Player player, String header, String foo */ public static void sendListHeaderFooter(Player player, RawText header, RawText footer) { + Locale playerLocale = null; + + if(header != null || footer != null) + playerLocale = I18n.getPlayerLocale(player); + + if(header != null && header.getLocale() != null) + header.locale(playerLocale); + + if(footer != null && footer.getLocale() != null) + footer.locale(playerLocale); + sendRawListHeaderFooter( player, header != null ? header.toJSONString() : "{\"text\": \"\"}", diff --git a/src/main/java/fr/zcraft/zlib/tools/text/MessageSender.java b/src/main/java/fr/zcraft/zlib/tools/text/MessageSender.java index c621eb02..d7990aa6 100644 --- a/src/main/java/fr/zcraft/zlib/tools/text/MessageSender.java +++ b/src/main/java/fr/zcraft/zlib/tools/text/MessageSender.java @@ -29,6 +29,7 @@ */ package fr.zcraft.zlib.tools.text; +import fr.zcraft.zlib.components.i18n.I18n; import fr.zcraft.zlib.components.rawtext.RawText; import fr.zcraft.zlib.tools.reflection.NMSNetwork; import fr.zcraft.zlib.tools.reflection.Reflection; @@ -131,6 +132,8 @@ public static boolean sendMessage(Player receiver, String message, MessageType t */ public static boolean sendMessage(Player receiver, RawText message, MessageType type) { + if(message.getLocale() != null) + message.locale(I18n.getPlayerLocale(receiver)); return sendChatPacket(receiver, type.isJSON() ? message.toJSONString() : message.toFormattedText(), type); } diff --git a/src/main/java/fr/zcraft/zlib/tools/text/RawMessage.java b/src/main/java/fr/zcraft/zlib/tools/text/RawMessage.java index c15343ff..c7dafa38 100644 --- a/src/main/java/fr/zcraft/zlib/tools/text/RawMessage.java +++ b/src/main/java/fr/zcraft/zlib/tools/text/RawMessage.java @@ -29,6 +29,7 @@ */ package fr.zcraft.zlib.tools.text; +import fr.zcraft.zlib.components.i18n.I18n; import fr.zcraft.zlib.components.rawtext.RawText; import fr.zcraft.zlib.tools.PluginLogger; import org.bukkit.Bukkit; @@ -64,6 +65,9 @@ public static void send(CommandSender commandSender, RawText text) { if(commandSender instanceof Player) { + if(text.getLocale() == null) + text.locale(I18n.getPlayerLocale((Player) commandSender)); + send((Player) commandSender, text.toJSONString()); } else diff --git a/src/main/java/fr/zcraft/zlib/tools/text/Titles.java b/src/main/java/fr/zcraft/zlib/tools/text/Titles.java index 92ff8e71..c646d3d6 100644 --- a/src/main/java/fr/zcraft/zlib/tools/text/Titles.java +++ b/src/main/java/fr/zcraft/zlib/tools/text/Titles.java @@ -29,6 +29,7 @@ */ package fr.zcraft.zlib.tools.text; +import fr.zcraft.zlib.components.i18n.I18n; import fr.zcraft.zlib.components.rawtext.RawText; import fr.zcraft.zlib.exceptions.IncompatibleMinecraftVersionException; import fr.zcraft.zlib.tools.reflection.NMSNetwork; @@ -37,6 +38,7 @@ import org.bukkit.entity.Player; import java.lang.reflect.InvocationTargetException; +import java.util.Locale; /** @@ -144,6 +146,17 @@ public static void displayTitle(Player player, int fadeIn, int stay, int fadeOut */ public static void displayTitle(Player player, int fadeIn, int stay, int fadeOut, RawText title, RawText subtitle) { + Locale playerLocale = null; + + if(title != null || subtitle != null) + playerLocale = I18n.getPlayerLocale(player); + + if(title != null && title.getLocale() != null) + title.locale(playerLocale); + + if(subtitle != null && subtitle.getLocale() != null) + subtitle.locale(playerLocale); + displayRawTitle( player, fadeIn, stay, fadeOut, title != null ? title.toJSONString() : "{\"text\": \"\"}", From d3da46ca73eba0cda6e39f7320e579955e092328 Mon Sep 17 00:00:00 2001 From: Adrien Prokopowicz Date: Mon, 18 Jul 2016 17:29:54 +0200 Subject: [PATCH 4/8] * BUG: RawTextPart: hover() now correctly handles Locales. --- .../java/fr/zcraft/zlib/components/rawtext/RawTextPart.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java index 85225d98..2abba93f 100644 --- a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java +++ b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java @@ -270,6 +270,9 @@ private T hover(ActionHover action, Object object) public T hover(RawTextPart hoverText) { + if(hoverText.getLocale() == null) + hoverText.locale(getLocale()); + return hover(ActionHover.SHOW_TEXT, hoverText.build()); } From 451a0386e0d1097269d8f7200160482dee1a337c Mon Sep 17 00:00:00 2001 From: Adrien Prokopowicz Date: Mon, 18 Jul 2016 18:24:57 +0200 Subject: [PATCH 5/8] Added I18nText integration to the Commands API. * NEW: Command methods now handles I18nTexts. * NEW: RawTextPart hover() can now take an ItemStackBuilder, and sets its locale. * NEW: ItemStackBuilder.getLocale() is a new method to retreive the locale currently assigned to the ItemStack. --- .../zlib/components/commands/Command.java | 37 +++++++++++++++++-- .../zlib/components/rawtext/RawTextPart.java | 8 ++++ .../zlib/tools/items/ItemStackBuilder.java | 9 +++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/zcraft/zlib/components/commands/Command.java b/src/main/java/fr/zcraft/zlib/components/commands/Command.java index d5f4d4ec..182b153a 100644 --- a/src/main/java/fr/zcraft/zlib/components/commands/Command.java +++ b/src/main/java/fr/zcraft/zlib/components/commands/Command.java @@ -31,7 +31,9 @@ package fr.zcraft.zlib.components.commands; import fr.zcraft.zlib.components.commands.CommandException.Reason; +import fr.zcraft.zlib.components.i18n.I; import fr.zcraft.zlib.components.i18n.I18n; +import fr.zcraft.zlib.components.i18n.I18nText; import fr.zcraft.zlib.components.rawtext.RawText; import fr.zcraft.zlib.core.ZLib; import fr.zcraft.zlib.tools.text.RawMessage; @@ -93,7 +95,7 @@ protected List complete() throws CommandException public void execute(CommandSender sender, String[] args) { - this.sender = sender; this.args = args; + this.sender = sender; this.args = args; this.senderLocale = null; try { if(!canExecute(sender)) @@ -104,13 +106,13 @@ public void execute(CommandSender sender, String[] args) { warning(ex.getReasonString()); } - this.sender = null; this.args = null; + this.sender = null; this.args = null; this.senderLocale = null; } public List tabComplete(CommandSender sender, String[] args) { List result = null; - this.sender = sender; this.args = args; + this.sender = sender; this.args = args; this.senderLocale = null; try { if(canExecute(sender)) @@ -118,7 +120,7 @@ public List tabComplete(CommandSender sender, String[] args) } catch(CommandException ex){} - this.sender = null; this.args = null; + this.sender = null; this.args = null; this.senderLocale = null; if(result == null) result = new ArrayList(); return result; } @@ -180,6 +182,11 @@ protected void throwInvalidArgument(String reason) throws CommandException throw new CommandException(this, Reason.INVALID_PARAMETERS, reason); } + protected void throwInvalidArgument(I18nText reason, Object... parameters) throws CommandException + { + throwInvalidArgument(I.t(getSenderLocale(), reason, parameters)); + } + protected void throwNotAuthorized() throws CommandException { throw new CommandException(this, Reason.SENDER_NOT_AUTHORIZED); @@ -194,6 +201,8 @@ protected Player playerSender() throws CommandException protected Locale getSenderLocale() { + if(sender == null) return null; + if(senderLocale != null) return senderLocale; if(sender instanceof Player) @@ -219,6 +228,11 @@ protected void info(String message) { info(sender, message); } + + protected void info(I18nText message, Object... parameters) + { + info(I.t(getSenderLocale(), message, parameters)); + } static protected void success(CommandSender sender, String message) { @@ -230,6 +244,11 @@ protected void success(String message) success(sender, message); } + protected void success(I18nText message, Object... parameters) + { + success(I.t(getSenderLocale(), message, parameters)); + } + static protected void warning(CommandSender sender, String message) { sender.sendMessage("§c" + message); @@ -240,11 +259,21 @@ protected void warning(String message) warning(sender, message); } + protected void warning(I18nText message, Object... parameters) + { + warning(I.t(getSenderLocale(), message, parameters)); + } + protected void error(String message) throws CommandException { throw new CommandException(this, Reason.COMMAND_ERROR, message); } + protected void error(I18nText message, Object... parameters) throws CommandException + { + error(I.t(getSenderLocale(), message, parameters)); + } + protected void tellRaw(String rawMessage) throws CommandException { RawMessage.send(playerSender(), rawMessage); diff --git a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java index 2abba93f..b1322ce8 100644 --- a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java +++ b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java @@ -37,6 +37,7 @@ import fr.zcraft.zlib.components.i18n.I; import fr.zcraft.zlib.components.i18n.I18nText; import fr.zcraft.zlib.tools.PluginLogger; +import fr.zcraft.zlib.tools.items.ItemStackBuilder; import fr.zcraft.zlib.tools.items.ItemUtils; import fr.zcraft.zlib.tools.reflection.NMSException; import org.bukkit.Achievement; @@ -296,6 +297,13 @@ public T hover(ItemStack item) return hover(ActionHover.SHOW_ITEM, RawText.toJSONString(item)); } + public T hover(ItemStackBuilder item) + { + if(item.getLocale() != null) + item.locale(getLocale()); + return hover(item.item()); + } + public T hover(Entity entity) { return hover(ActionHover.SHOW_ENTITY, RawText.toJSON(entity).toJSONString()); diff --git a/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java b/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java index 01545d31..e5db0e7f 100644 --- a/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java +++ b/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java @@ -635,6 +635,15 @@ public ItemStackBuilder locale(Locale locale) this.locale = locale; return this; } + + /** + * Retreives the locale assigned to this ItemStackBuilder + * @return the locale assigned to this ItemStackBuilder, or null if not defined. + */ + public Locale getLocale() + { + return this.locale; + } /** * Concatenates a String[] to a single one. From 5d1e035c93daec14e59c3d774eee6f850d12a4b1 Mon Sep 17 00:00:00 2001 From: Adrien Prokopowicz Date: Wed, 7 Mar 2018 01:53:21 +0100 Subject: [PATCH 6/8] Renamed class I18nText to LazyTranslation --- .../zlib/components/commands/Command.java | 20 ++++++++-------- .../zcraft/zlib/components/gui/GuiBase.java | 4 ++-- .../zlib/components/gui/InventoryGui.java | 4 ++-- .../fr/zcraft/zlib/components/i18n/I.java | 12 +++++----- .../fr/zcraft/zlib/components/i18n/I18n.java | 2 +- .../{I18nText.java => LazyTranslation.java} | 6 ++--- .../zlib/components/rawtext/RawTextPart.java | 24 +++++++++---------- .../components/rawtext/RawTextSubPart.java | 6 ++--- .../zlib/tools/items/ItemStackBuilder.java | 14 +++++------ 9 files changed, 46 insertions(+), 46 deletions(-) rename src/main/java/fr/zcraft/zlib/components/i18n/{I18nText.java => LazyTranslation.java} (93%) diff --git a/src/main/java/fr/zcraft/zlib/components/commands/Command.java b/src/main/java/fr/zcraft/zlib/components/commands/Command.java index cbd58e12..7c087cdb 100644 --- a/src/main/java/fr/zcraft/zlib/components/commands/Command.java +++ b/src/main/java/fr/zcraft/zlib/components/commands/Command.java @@ -33,7 +33,7 @@ import fr.zcraft.zlib.components.commands.CommandException.Reason; import fr.zcraft.zlib.components.i18n.I; import fr.zcraft.zlib.components.i18n.I18n; -import fr.zcraft.zlib.components.i18n.I18nText; +import fr.zcraft.zlib.components.i18n.LazyTranslation; import fr.zcraft.zlib.components.rawtext.RawText; import fr.zcraft.zlib.core.ZLib; import fr.zcraft.zlib.tools.text.RawMessage; @@ -407,7 +407,7 @@ protected void throwInvalidArgument(String reason) throws CommandException throw new CommandException(this, Reason.INVALID_PARAMETERS, reason); } - protected void throwInvalidArgument(I18nText reason, Object... parameters) throws CommandException + protected void throwInvalidArgument(LazyTranslation reason, Object... parameters) throws CommandException { throwInvalidArgument(I.t(getSenderLocale(), reason, parameters)); } @@ -495,10 +495,10 @@ protected void info(String message) * @param message The message to display. * @param parameters The parameters of the I18n message. * - * @see I#t(Locale, I18nText, Object...) the underlying method used + * @see I#t(Locale, LazyTranslation, Object...) the underlying method used * to retrieve the text and merge it with the parameters. */ - protected void info(I18nText message, Object... parameters) + protected void info(LazyTranslation message, Object... parameters) { info(I.t(getSenderLocale(), message, parameters)); } @@ -530,10 +530,10 @@ protected void success(String message) * @param message The message to display. * @param parameters The parameters of the I18n message. * - * @see I#t(Locale, I18nText, Object...) the underlying method used + * @see I#t(Locale, LazyTranslation, Object...) the underlying method used * to retrieve the text and merge it with the parameters. */ - protected void success(I18nText message, Object... parameters) + protected void success(LazyTranslation message, Object... parameters) { success(I.t(getSenderLocale(), message, parameters)); } @@ -566,10 +566,10 @@ protected void warning(String message) * @param message The message to display. * @param parameters The parameters of the I18n message. * - * @see I#t(Locale, I18nText, Object...) the underlying method used + * @see I#t(Locale, LazyTranslation, Object...) the underlying method used * to retrieve the text and merge it with the parameters. */ - protected void warning(I18nText message, Object... parameters) + protected void warning(LazyTranslation message, Object... parameters) { warning(I.t(getSenderLocale(), message, parameters)); } @@ -597,10 +597,10 @@ protected void error(String message) throws CommandException * @throws CommandException This method always throws a {@link Reason#COMMAND_ERROR}'s * {@link CommandException}, interrupting the execution of the command. * - * @see I#t(Locale, I18nText, Object...) the underlying method used + * @see I#t(Locale, LazyTranslation, Object...) the underlying method used * to retrieve the text and merge it with the parameters. */ - protected void error(I18nText message, Object... parameters) throws CommandException + protected void error(LazyTranslation message, Object... parameters) throws CommandException { error(I.t(getSenderLocale(), message, parameters)); } diff --git a/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java b/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java index ef727f79..a0e242cc 100644 --- a/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java +++ b/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java @@ -32,7 +32,7 @@ import fr.zcraft.zlib.components.i18n.I; import fr.zcraft.zlib.components.i18n.I18n; -import fr.zcraft.zlib.components.i18n.I18nText; +import fr.zcraft.zlib.components.i18n.LazyTranslation; import fr.zcraft.zlib.core.ZLib; import java.util.Locale; import org.bukkit.entity.Player; @@ -180,7 +180,7 @@ protected void sendMessage(String message) * @param message The message to send to the player. * @param parameters Parameters for the translatable format text, if any. */ - protected void sendMessage(I18nText message, Object... parameters) + protected void sendMessage(LazyTranslation message, Object... parameters) { sendMessage(I.t(message, parameters)); } diff --git a/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java b/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java index 1475da93..9ffe33be 100644 --- a/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java +++ b/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java @@ -31,7 +31,7 @@ package fr.zcraft.zlib.components.gui; import fr.zcraft.zlib.components.i18n.I; -import fr.zcraft.zlib.components.i18n.I18nText; +import fr.zcraft.zlib.components.i18n.LazyTranslation; import fr.zcraft.zlib.tools.items.InventoryUtils; import fr.zcraft.zlib.tools.runners.RunTask; import org.bukkit.Bukkit; @@ -255,7 +255,7 @@ protected void setTitle(String title) * @param title The new title of the inventory * @param parameters Parameters for the translatable format text, if any. */ - protected void setTitle(I18nText title, Object ...parameters) + protected void setTitle(LazyTranslation title, Object ...parameters) { setTitle(I.t(getPlayerLocale(), title, parameters)); } diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/I.java b/src/main/java/fr/zcraft/zlib/components/i18n/I.java index 72751c23..65ae14ce 100644 --- a/src/main/java/fr/zcraft/zlib/components/i18n/I.java +++ b/src/main/java/fr/zcraft/zlib/components/i18n/I.java @@ -88,12 +88,12 @@ public static String t(String text, Object... parameters) return I18n.translate(null, null, text, null, null, parameters); } - public static String t(Locale locale, I18nText text, Object... parameters) + public static String t(Locale locale, LazyTranslation text, Object... parameters) { return I18n.translate(locale, text, parameters); } - public static String t(I18nText text, Object... parameters) + public static String t(LazyTranslation text, Object... parameters) { return t(null, text, parameters); } @@ -315,18 +315,18 @@ public static void sendTcn(Player player, String context, String singular, Strin player.sendMessage(I18n.translate(I18n.getPlayerLocale(player), context, singular, plural, count, parameters)); } - public static I18nText i(String messageId) + public static LazyTranslation i(String messageId) { return i(messageId, null, null, null); } - public static I18nText i(String messageId, String pluralMessageId, Integer count) + public static LazyTranslation i(String messageId, String pluralMessageId, Integer count) { return i(messageId, pluralMessageId, count, null); } - public static I18nText i(String messageId, String pluralMessageId, Integer count, String context) + public static LazyTranslation i(String messageId, String pluralMessageId, Integer count, String context) { - return new I18nText(messageId, pluralMessageId, count, context); + return new LazyTranslation(messageId, pluralMessageId, count, context); } } diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java b/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java index 1c2ada27..3eec5959 100644 --- a/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java +++ b/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java @@ -664,7 +664,7 @@ public static String translate(String context, String messageId, String messageI * * @return The translated text, with the parameters replaced by their values. */ - public static String translate(Locale locale, I18nText text, Object... parameters) + public static String translate(Locale locale, LazyTranslation text, Object... parameters) { return translate(null, text.getContext(), text.getMessageId(), text.getPluralMessageId(), text.getCount(), parameters); } diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/I18nText.java b/src/main/java/fr/zcraft/zlib/components/i18n/LazyTranslation.java similarity index 93% rename from src/main/java/fr/zcraft/zlib/components/i18n/I18nText.java rename to src/main/java/fr/zcraft/zlib/components/i18n/LazyTranslation.java index cdf38fb0..222a09f5 100644 --- a/src/main/java/fr/zcraft/zlib/components/i18n/I18nText.java +++ b/src/main/java/fr/zcraft/zlib/components/i18n/LazyTranslation.java @@ -30,7 +30,7 @@ package fr.zcraft.zlib.components.i18n; -public class I18nText +public class LazyTranslation { private final String messageId; private final String pluralMessageId; @@ -39,7 +39,7 @@ public class I18nText private Object[] parameters; - public I18nText(String messageId, String pluralMessageId, Integer count, String context) + public LazyTranslation(String messageId, String pluralMessageId, Integer count, String context) { this.messageId = messageId; this.pluralMessageId = pluralMessageId; @@ -47,7 +47,7 @@ public I18nText(String messageId, String pluralMessageId, Integer count, String this.context = context; } - public I18nText(String messageId) + public LazyTranslation(String messageId) { this(messageId, null, null, null); } diff --git a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java index 686fbc07..75503b02 100644 --- a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java +++ b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java @@ -34,7 +34,7 @@ import fr.zcraft.zlib.components.commands.Command; import fr.zcraft.zlib.components.commands.Commands; import fr.zcraft.zlib.components.i18n.I; -import fr.zcraft.zlib.components.i18n.I18nText; +import fr.zcraft.zlib.components.i18n.LazyTranslation; import fr.zcraft.zlib.tools.PluginLogger; import fr.zcraft.zlib.tools.items.ItemStackBuilder; import fr.zcraft.zlib.tools.items.ItemUtils; @@ -73,7 +73,7 @@ private enum ActionHover } private String text = null; - private I18nText i18nText = null; + private LazyTranslation lazyTranslation = null; private Object[] textParameters = null; private boolean translate = false; @@ -111,7 +111,7 @@ private enum ActionHover this(text, null); } - RawTextPart(I18nText text) + RawTextPart(LazyTranslation text) { this(null, text); } @@ -122,9 +122,9 @@ private enum ActionHover this.parent = parent; } - RawTextPart(RawTextPart parent, I18nText text, Object... parameters) + RawTextPart(RawTextPart parent, LazyTranslation text, Object... parameters) { - this.i18nText = text; + this.lazyTranslation = text; this.parent = parent; this.textParameters = parameters; } @@ -155,7 +155,7 @@ public RawTextPart then(String text) return newPart; } - public RawTextPart then(I18nText text, Object... parameters) + public RawTextPart then(LazyTranslation text, Object... parameters) { RawTextPart root = getRoot(); RawTextPart newPart = new RawTextSubPart(root, text, parameters); @@ -173,16 +173,16 @@ public RawTextPart then(I18nText text, Object... parameters) public T text(String text) { this.text = text; - this.i18nText = null; + this.lazyTranslation = null; this.translate = false; return (T)this; } - public T text(I18nText text, Object... parameters) + public T text(LazyTranslation text, Object... parameters) { this.text = null; - this.i18nText = text; + this.lazyTranslation = text; this.translate = false; this.textParameters = parameters; @@ -199,7 +199,7 @@ public T text(I18nText text, Object... parameters) public T translate(String text) { this.text = text; - this.i18nText = null; + this.lazyTranslation = null; this.translate = true; return (T)this; @@ -704,9 +704,9 @@ private void writeFormattedText(StringBuilder buf) private String getText() { - if(i18nText != null) + if(lazyTranslation != null) { - return I.t(getLocale(), i18nText, textParameters); + return I.t(getLocale(), lazyTranslation, textParameters); } return text; diff --git a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextSubPart.java b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextSubPart.java index 0d449ebf..9abbf335 100644 --- a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextSubPart.java +++ b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextSubPart.java @@ -30,7 +30,7 @@ package fr.zcraft.zlib.components.rawtext; -import fr.zcraft.zlib.components.i18n.I18nText; +import fr.zcraft.zlib.components.i18n.LazyTranslation; class RawTextSubPart extends RawTextPart { @@ -39,7 +39,7 @@ public RawTextSubPart(String text) super(text); } - public RawTextSubPart(I18nText text) + public RawTextSubPart(LazyTranslation text) { super(text); } @@ -49,7 +49,7 @@ public RawTextSubPart(String text, RawTextPart parent) super(text, parent); } - public RawTextSubPart(RawTextPart parent, I18nText text, Object... parameters) + public RawTextSubPart(RawTextPart parent, LazyTranslation text, Object... parameters) { super(parent, text, parameters); } diff --git a/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java b/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java index dd38ec36..e0083982 100644 --- a/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java +++ b/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java @@ -32,7 +32,7 @@ import fr.zcraft.zlib.components.gui.GuiUtils; import fr.zcraft.zlib.components.i18n.I; -import fr.zcraft.zlib.components.i18n.I18nText; +import fr.zcraft.zlib.components.i18n.LazyTranslation; import fr.zcraft.zlib.components.nbt.NBT; import fr.zcraft.zlib.components.nbt.NBTCompound; import fr.zcraft.zlib.components.rawtext.RawText; @@ -379,7 +379,7 @@ public ItemStackBuilder title(ChatColor color, String... texts) * * @return The current ItemStackBuilder instance, for methods chaining. */ - public ItemStackBuilder title(I18nText text, Object ...parameters) + public ItemStackBuilder title(LazyTranslation text, Object ...parameters) { return title(I.t(locale, text, parameters)); } @@ -445,21 +445,21 @@ public ItemStackBuilder loreLine(String... text) * * @return The current ItemStackBuilder instance, for methods chaining. */ - public ItemStackBuilder loreLine(I18nText text, Object ...parameters) + public ItemStackBuilder loreLine(LazyTranslation text, Object ...parameters) { return loreLine(I.t(locale, text, parameters)); } /** * Adds one line of lore to the ItemStack. - * This method is an alias for {@link #loreLine(fr.zcraft.zlib.components.i18n.I18nText, java.lang.Object...) }. + * This method is an alias for {@link #loreLine(LazyTranslation, java.lang.Object...) }. * * @param text The lore line's text. It will be translated using the locale currently set for the ItemStack. * @param parameters Parameters for the translatable format text, if any. * * @return The current ItemStackBuilder instance, for methods chaining. */ - public ItemStackBuilder lore(I18nText text, Object ...parameters) + public ItemStackBuilder lore(LazyTranslation text, Object ...parameters) { return loreLine(text, parameters); } @@ -568,7 +568,7 @@ public ItemStackBuilder longLore(ChatColor color, String text, int lineLength) * * @return The current ItemStackBuilder instance, for methods chaining. */ - public ItemStackBuilder longLore(int lineLength, I18nText text, Object ...parameters) + public ItemStackBuilder longLore(int lineLength, LazyTranslation text, Object ...parameters) { return longLore(I.t(locale, text, parameters), lineLength); } @@ -582,7 +582,7 @@ public ItemStackBuilder longLore(int lineLength, I18nText text, Object ...parame * * @return The current ItemStackBuilder instance, for methods chaining. */ - public ItemStackBuilder longLore(I18nText text, Object ...parameters) + public ItemStackBuilder longLore(LazyTranslation text, Object ...parameters) { return longLore(I.t(locale, text, parameters)); } From 27e29b3125bda322ead2f2120e8713aae18ed3f4 Mon Sep 17 00:00:00 2001 From: Adrien Prokopowicz Date: Wed, 7 Mar 2018 03:21:42 +0100 Subject: [PATCH 7/8] Renamed method I.t to I.l to match LazyTranslation --- src/main/java/fr/zcraft/zlib/components/i18n/I.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/I.java b/src/main/java/fr/zcraft/zlib/components/i18n/I.java index 65ae14ce..0b771936 100644 --- a/src/main/java/fr/zcraft/zlib/components/i18n/I.java +++ b/src/main/java/fr/zcraft/zlib/components/i18n/I.java @@ -314,18 +314,18 @@ public static void sendTcn(Player player, String context, String singular, Strin { player.sendMessage(I18n.translate(I18n.getPlayerLocale(player), context, singular, plural, count, parameters)); } - - public static LazyTranslation i(String messageId) + + public static LazyTranslation l(String messageId) { - return i(messageId, null, null, null); + return l(messageId, null, null, null); } - public static LazyTranslation i(String messageId, String pluralMessageId, Integer count) + public static LazyTranslation l(String messageId, String pluralMessageId, Integer count) { - return i(messageId, pluralMessageId, count, null); + return l(messageId, pluralMessageId, count, null); } - public static LazyTranslation i(String messageId, String pluralMessageId, Integer count, String context) + public static LazyTranslation l(String messageId, String pluralMessageId, Integer count, String context) { return new LazyTranslation(messageId, pluralMessageId, count, context); } From 0b8ae148bb060c943a24c11856ee2ad190109cf4 Mon Sep 17 00:00:00 2001 From: Adrien Prokopowicz Date: Sun, 11 Mar 2018 20:08:58 +0100 Subject: [PATCH 8/8] Various minor changes --- .../zlib/components/commands/Command.java | 10 ++++----- .../zcraft/zlib/components/gui/GuiBase.java | 2 +- .../zlib/components/gui/InventoryGui.java | 2 +- .../fr/zcraft/zlib/components/i18n/I.java | 21 ++++++++++++------- .../fr/zcraft/zlib/components/i18n/I18n.java | 9 +------- .../zlib/components/rawtext/RawTextPart.java | 2 +- .../zlib/tools/items/ItemStackBuilder.java | 8 +++---- .../fr/zcraft/zlib/tools/items/ItemUtils.java | 3 +++ 8 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/main/java/fr/zcraft/zlib/components/commands/Command.java b/src/main/java/fr/zcraft/zlib/components/commands/Command.java index 7c087cdb..6849892c 100644 --- a/src/main/java/fr/zcraft/zlib/components/commands/Command.java +++ b/src/main/java/fr/zcraft/zlib/components/commands/Command.java @@ -409,7 +409,7 @@ protected void throwInvalidArgument(String reason) throws CommandException protected void throwInvalidArgument(LazyTranslation reason, Object... parameters) throws CommandException { - throwInvalidArgument(I.t(getSenderLocale(), reason, parameters)); + throwInvalidArgument(I.tl(getSenderLocale(), reason, parameters)); } @@ -500,7 +500,7 @@ protected void info(String message) */ protected void info(LazyTranslation message, Object... parameters) { - info(I.t(getSenderLocale(), message, parameters)); + info(I.tl(getSenderLocale(), message, parameters)); } /** @@ -535,7 +535,7 @@ protected void success(String message) */ protected void success(LazyTranslation message, Object... parameters) { - success(I.t(getSenderLocale(), message, parameters)); + success(I.tl(getSenderLocale(), message, parameters)); } @@ -571,7 +571,7 @@ protected void warning(String message) */ protected void warning(LazyTranslation message, Object... parameters) { - warning(I.t(getSenderLocale(), message, parameters)); + warning(I.tl(getSenderLocale(), message, parameters)); } @@ -602,7 +602,7 @@ protected void error(String message) throws CommandException */ protected void error(LazyTranslation message, Object... parameters) throws CommandException { - error(I.t(getSenderLocale(), message, parameters)); + error(I.tl(getSenderLocale(), message, parameters)); } diff --git a/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java b/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java index a0e242cc..5d4c4d0a 100644 --- a/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java +++ b/src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java @@ -182,7 +182,7 @@ protected void sendMessage(String message) */ protected void sendMessage(LazyTranslation message, Object... parameters) { - sendMessage(I.t(message, parameters)); + sendMessage(I.tl(getPlayerLocale(), message, parameters)); } protected boolean checkImmune() diff --git a/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java b/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java index 9ffe33be..fc286c38 100644 --- a/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java +++ b/src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java @@ -257,7 +257,7 @@ protected void setTitle(String title) */ protected void setTitle(LazyTranslation title, Object ...parameters) { - setTitle(I.t(getPlayerLocale(), title, parameters)); + setTitle(I.tl(getPlayerLocale(), title, parameters)); } /** @return The underlying inventory, or null if the Gui has not been opened yet. */ diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/I.java b/src/main/java/fr/zcraft/zlib/components/i18n/I.java index 0b771936..a1de0dc1 100644 --- a/src/main/java/fr/zcraft/zlib/components/i18n/I.java +++ b/src/main/java/fr/zcraft/zlib/components/i18n/I.java @@ -88,14 +88,14 @@ public static String t(String text, Object... parameters) return I18n.translate(null, null, text, null, null, parameters); } - public static String t(Locale locale, LazyTranslation text, Object... parameters) + public static String tl(Locale locale, LazyTranslation text, Object... parameters) { return I18n.translate(locale, text, parameters); } - public static String t(LazyTranslation text, Object... parameters) + public static String tl(LazyTranslation text, Object... parameters) { - return t(null, text, parameters); + return tl(null, text, parameters); } /** @@ -317,15 +317,20 @@ public static void sendTcn(Player player, String context, String singular, Strin public static LazyTranslation l(String messageId) { - return l(messageId, null, null, null); + return lcn(null, messageId, null, null); } - public static LazyTranslation l(String messageId, String pluralMessageId, Integer count) + public static LazyTranslation ln(String messageId, String pluralMessageId, Integer count) { - return l(messageId, pluralMessageId, count, null); + return lcn(null, messageId, pluralMessageId, count); } - - public static LazyTranslation l(String messageId, String pluralMessageId, Integer count, String context) + + public static LazyTranslation lc(String messageId, Integer count) + { + return lcn(null, messageId, null, count); + } + + public static LazyTranslation lcn(String context, String messageId, String pluralMessageId, Integer count) { return new LazyTranslation(messageId, pluralMessageId, count, context); } diff --git a/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java b/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java index 3eec5959..0ec960fc 100644 --- a/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java +++ b/src/main/java/fr/zcraft/zlib/components/i18n/I18n.java @@ -650,13 +650,6 @@ public static String translate(String context, String messageId, String messageI /** * Translates the given string. * - *

Tries to use the primary locale; fallbacks to the fallback locale if the string cannot be - * translated; fallbacks to the input text if the string still cannot be translated.

- * - *

The count is likely to be used in the string, so if, for a translation with plurals, only - * a count is given, this count is also interpreted as a parameter (the first and only one, {@code - * {0}}). If this behavior annoys you, you can disable it using {@link - * #addCountToParameters(boolean)}.

* @param locale The locale to use to translate the string. * @param text The text to translate * @param parameters The parameters, replacing values like {@code {0}} in the translated @@ -666,7 +659,7 @@ public static String translate(String context, String messageId, String messageI */ public static String translate(Locale locale, LazyTranslation text, Object... parameters) { - return translate(null, text.getContext(), text.getMessageId(), text.getPluralMessageId(), text.getCount(), parameters); + return translate(locale, text.getContext(), text.getMessageId(), text.getPluralMessageId(), text.getCount(), parameters); } /** diff --git a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java index 75503b02..7b6f99d4 100644 --- a/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java +++ b/src/main/java/fr/zcraft/zlib/components/rawtext/RawTextPart.java @@ -706,7 +706,7 @@ private String getText() { if(lazyTranslation != null) { - return I.t(getLocale(), lazyTranslation, textParameters); + return I.tl(getLocale(), lazyTranslation, textParameters); } return text; diff --git a/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java b/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java index e0083982..1c3b1972 100644 --- a/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java +++ b/src/main/java/fr/zcraft/zlib/tools/items/ItemStackBuilder.java @@ -381,7 +381,7 @@ public ItemStackBuilder title(ChatColor color, String... texts) */ public ItemStackBuilder title(LazyTranslation text, Object ...parameters) { - return title(I.t(locale, text, parameters)); + return title(I.tl(locale, text, parameters)); } /** @@ -447,7 +447,7 @@ public ItemStackBuilder loreLine(String... text) */ public ItemStackBuilder loreLine(LazyTranslation text, Object ...parameters) { - return loreLine(I.t(locale, text, parameters)); + return loreLine(I.tl(locale, text, parameters)); } /** @@ -570,7 +570,7 @@ public ItemStackBuilder longLore(ChatColor color, String text, int lineLength) */ public ItemStackBuilder longLore(int lineLength, LazyTranslation text, Object ...parameters) { - return longLore(I.t(locale, text, parameters), lineLength); + return longLore(I.tl(locale, text, parameters), lineLength); } /** @@ -584,7 +584,7 @@ public ItemStackBuilder longLore(int lineLength, LazyTranslation text, Object .. */ public ItemStackBuilder longLore(LazyTranslation text, Object ...parameters) { - return longLore(I.t(locale, text, parameters)); + return longLore(I.tl(locale, text, parameters)); } /** diff --git a/src/main/java/fr/zcraft/zlib/tools/items/ItemUtils.java b/src/main/java/fr/zcraft/zlib/tools/items/ItemUtils.java index b9ad8dae..30f3857e 100644 --- a/src/main/java/fr/zcraft/zlib/tools/items/ItemUtils.java +++ b/src/main/java/fr/zcraft/zlib/tools/items/ItemUtils.java @@ -29,6 +29,8 @@ */ package fr.zcraft.zlib.tools.items; +import fr.zcraft.zlib.components.i18n.I; +import fr.zcraft.zlib.components.i18n.LazyTranslation; import fr.zcraft.zlib.tools.PluginLogger; import fr.zcraft.zlib.tools.reflection.NMSException; import fr.zcraft.zlib.tools.reflection.Reflection; @@ -49,6 +51,7 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.Random; //import org.bukkit.Sound;