Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional I18n APIs. #12

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
122 changes: 117 additions & 5 deletions src/main/java/fr/zcraft/zlib/components/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +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.LazyTranslation;
import fr.zcraft.zlib.components.rawtext.RawText;
import fr.zcraft.zlib.core.ZLib;
import fr.zcraft.zlib.tools.text.RawMessage;
Expand All @@ -44,10 +47,10 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;


abstract public class Command
{
private static final Pattern FLAG_PATTERN = Pattern.compile("(--?)[a-zA-Z0-9-]+");
Expand All @@ -63,6 +66,9 @@ abstract public class Command

protected CommandSender sender;
protected String[] args;

private Locale senderLocale;

protected Set<String> flags;

/**
Expand Down Expand Up @@ -158,12 +164,15 @@ protected List<String> complete() throws CommandException
public void execute(CommandSender sender, String[] args)
{
this.sender = sender;
this.senderLocale = null;

parseArgs(args);

try
{
if (!canExecute(sender, args))
throw new CommandException(this, Reason.SENDER_NOT_AUTHORIZED);

run();
}
catch (CommandException ex)
Expand All @@ -172,6 +181,7 @@ public void execute(CommandSender sender, String[] args)
}

this.sender = null;
this.senderLocale = null;
this.args = null;
this.flags = null;
}
Expand All @@ -187,16 +197,18 @@ public List<String> tabComplete(CommandSender sender, String[] args)
List<String> result = null;

this.sender = sender;
this.senderLocale = null;
parseArgs(args);

try
{
if (canExecute(sender, args))
result = complete();
}
catch (CommandException ignored) {}
catch (CommandException ignored){}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L'autre qui retire des espaces


this.sender = null;
this.senderLocale = null;
this.args = null;
this.flags = null;

Expand Down Expand Up @@ -379,6 +391,7 @@ private static boolean isValidFlag(Set<String> acceptedFlags, String flag)
}



///////////// Common methods for commands /////////////

/**
Expand All @@ -394,6 +407,12 @@ protected void throwInvalidArgument(String reason) throws CommandException
throw new CommandException(this, Reason.INVALID_PARAMETERS, reason);
}

protected void throwInvalidArgument(LazyTranslation reason, Object... parameters) throws CommandException
{
throwInvalidArgument(I.tl(getSenderLocale(), reason, parameters));
}


/**
* Stops the command execution because the command usage is disallowed, and
* displays an error message.
Expand All @@ -408,7 +427,7 @@ protected void throwNotAuthorized() throws CommandException
/**
* Retrieves the {@link Player} who executed this command. If the command is
* not executed by a player, aborts the execution and displays an error
* messagE.
* message.
*
* @return The player executing this command.
* @throws CommandException If the sender is not a player.
Expand All @@ -420,6 +439,32 @@ protected Player playerSender() throws CommandException
return (Player) sender;
}

/**
* Retrieves the sender's locale. If it cannot be retrieved for some reason,
* or if the sender is not a player (e.g. the console), the primary, or the
* fallback if no primary is defined, language is returned instead.
*
* @return The {@link Locale} to use for this sender.
*/
protected Locale getSenderLocale()
{
if(sender == null) return null;

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 /////////////

Expand All @@ -444,6 +489,20 @@ protected void info(String message)
info(sender, message);
}

/**
* Displays a gray informational message to the sender.
*
* @param message The message to display.
* @param parameters The parameters of the I18n message.
*
* @see I#t(Locale, LazyTranslation, Object...) the underlying method used
* to retrieve the text and merge it with the parameters.
*/
protected void info(LazyTranslation message, Object... parameters)
{
info(I.tl(getSenderLocale(), message, parameters));
}

/**
* Displays a green success message.
*
Expand All @@ -465,6 +524,21 @@ protected void success(String message)
success(sender, message);
}

/**
* Displays a green success message to the sender.
*
* @param message The message to display.
* @param parameters The parameters of the I18n message.
*
* @see I#t(Locale, LazyTranslation, Object...) the underlying method used
* to retrieve the text and merge it with the parameters.
*/
protected void success(LazyTranslation message, Object... parameters)
{
success(I.tl(getSenderLocale(), message, parameters));
}


/**
* Displays a red warning message.
*
Expand All @@ -486,22 +560,57 @@ protected void warning(String message)
warning(sender, message);
}

/**
* Displays a red warning message to the sender.
*
* @param message The message to display.
* @param parameters The parameters of the I18n message.
*
* @see I#t(Locale, LazyTranslation, Object...) the underlying method used
* to retrieve the text and merge it with the parameters.
*/
protected void warning(LazyTranslation message, Object... parameters)
{
warning(I.tl(getSenderLocale(), message, parameters));
}


/**
* Aborts the execution and displays an error message.
*
* @param message The message.
*
* @throws CommandException
* @throws CommandException This method always throws a {@link Reason#COMMAND_ERROR}'s
* {@link CommandException}, interrupting the execution of the command.
*/
protected void error(String message) throws CommandException
{
throw new CommandException(this, Reason.COMMAND_ERROR, message);
}

/**
* Aborts the execution and displays an error message.
*
* @param message The message.
* @param parameters The parameters of the I18n message.
*
* @throws CommandException This method always throws a {@link Reason#COMMAND_ERROR}'s
* {@link CommandException}, interrupting the execution of the command.
*
* @see I#t(Locale, LazyTranslation, Object...) the underlying method used
* to retrieve the text and merge it with the parameters.
*/
protected void error(LazyTranslation message, Object... parameters) throws CommandException
{
error(I.tl(getSenderLocale(), message, parameters));
}


/**
* Aborts the execution and displays a generic error message.
*
* @throws CommandException
* @throws CommandException This method always throws a {@link Reason#COMMAND_ERROR}'s
* {@link CommandException}, interrupting the execution of the command.
*/
protected void error() throws CommandException
{
Expand All @@ -527,6 +636,9 @@ protected void tellRaw(String rawMessage) throws CommandException
*/
protected void send(RawText text)
{
if(text.getLocale() == null)
text.locale(getSenderLocale());

RawMessage.send(sender, text);
}

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/fr/zcraft/zlib/components/gui/ActionGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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());
}

/**
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/fr/zcraft/zlib/components/gui/GuiBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.LazyTranslation;
import fr.zcraft.zlib.core.ZLib;
import java.util.Locale;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -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(LazyTranslation message, Object... parameters)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sans demander un retrait, quelle utilité à sendMessage dans le cadre d'un GUI, qui recouvre quasi systématiquement le chat (bien que l'implémentation soit générique et permette d'avoir des GUI en texte, certes… ça reste très marginal) ?

{
sendMessage(I.tl(getPlayerLocale(), message, parameters));
}

protected boolean checkImmune()
{
if(!immune) return false;
Expand All @@ -176,6 +197,4 @@ private void setImmune(boolean immune)
this.immune = immune;
}

/* ===== Static API ===== */

}
13 changes: 13 additions & 0 deletions src/main/java/fr/zcraft/zlib/components/gui/InventoryGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

package fr.zcraft.zlib.components.gui;

import fr.zcraft.zlib.components.i18n.I;
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;
Expand Down Expand Up @@ -247,6 +249,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(LazyTranslation title, Object ...parameters)
{
setTitle(I.tl(getPlayerLocale(), title, parameters));
}

/** @return The underlying inventory, or null if the Gui has not been opened yet. */
public Inventory getInventory() { return inventory; }

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/fr/zcraft/zlib/components/i18n/I.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public static String t(String text, Object... parameters)
{
return I18n.translate(null, null, text, null, null, parameters);
}

public static String tl(Locale locale, LazyTranslation text, Object... parameters)
{
return I18n.translate(locale, text, parameters);
}

public static String tl(LazyTranslation text, Object... parameters)
{
return tl(null, text, parameters);
}

/**
* Translates the string with a plural.
Expand Down Expand Up @@ -304,5 +314,24 @@ 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 l(String messageId)
{
return lcn(null, messageId, null, null);
}

public static LazyTranslation ln(String messageId, String pluralMessageId, Integer count)
{
return lcn(null, messageId, pluralMessageId, count);
}

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);
}
}
Loading