Use global using static for ICatalog methods

This commit is contained in:
Janet Blackquill 2022-10-20 11:51:40 -04:00
parent 22d8dd90fa
commit 5549cfeded
3 changed files with 168 additions and 10 deletions

View file

@ -649,7 +649,7 @@ namespace TShockAPI
string cmdName; string cmdName;
if (index == 0) // Space after the command specifier should not be supported if (index == 0) // Space after the command specifier should not be supported
{ {
player.SendErrorMessage(I18n.C.GetString("Invalid command entered. Type {0}help for a list of valid commands.", Specifier)); player.SendErrorMessage(GetString("Invalid command entered. Type {0}help for a list of valid commands.", Specifier));
return true; return true;
} }
else if (index < 0) else if (index < 0)
@ -677,28 +677,28 @@ namespace TShockAPI
call(new CommandArgs(cmdText, player, args)); call(new CommandArgs(cmdText, player, args));
return true; return true;
} }
player.SendErrorMessage(I18n.C.GetString("Invalid command entered. Type {0}help for a list of valid commands.", Specifier)); player.SendErrorMessage(GetString($"Invalid command entered. Type {0}help for a list of valid commands.", Specifier));
return true; return true;
} }
foreach (Command cmd in cmds) foreach (Command cmd in cmds)
{ {
if (!cmd.CanRun(player)) if (!cmd.CanRun(player))
{ {
TShock.Utils.SendLogs(I18n.C.GetString("{0} tried to execute {1}{2}.", player.Name, Specifier, cmdText), Color.PaleVioletRed, player); TShock.Utils.SendLogs(GetString("{0} tried to execute {1}{2}.", player.Name, Specifier, cmdText), Color.PaleVioletRed, player);
player.SendErrorMessage(I18n.C.GetString("You do not have access to this command.")); player.SendErrorMessage(GetString("You do not have access to this command."));
if (player.HasPermission(Permissions.su)) if (player.HasPermission(Permissions.su))
{ {
player.SendInfoMessage(I18n.C.GetString("You can use '{0}sudo {0}{1}' to override this check.", Specifier, cmdText)); player.SendInfoMessage(GetString("You can use '{0}sudo {0}{1}' to override this check.", Specifier, cmdText));
} }
} }
else if (!cmd.AllowServer && !player.RealPlayer) else if (!cmd.AllowServer && !player.RealPlayer)
{ {
player.SendErrorMessage(I18n.C.GetString("You must use this command in-game.")); player.SendErrorMessage(GetString("You must use this command in-game."));
} }
else else
{ {
if (cmd.DoLog) if (cmd.DoLog)
TShock.Utils.SendLogs(I18n.C.GetString("{0} executed: {1}{2}.", player.Name, silent ? SilentSpecifier : Specifier, cmdText), Color.PaleVioletRed, player); TShock.Utils.SendLogs(GetString("{0} executed: {1}{2}.", player.Name, silent ? SilentSpecifier : Specifier, cmdText), Color.PaleVioletRed, player);
cmd.Run(cmdText, silent, player, args); cmd.Run(cmdText, silent, player, args);
} }
} }
@ -769,7 +769,7 @@ namespace TShockAPI
{ {
TShock.Log.Warn(String.Format("{0} ({1}) had {2} or more invalid login attempts and was kicked automatically.", TShock.Log.Warn(String.Format("{0} ({1}) had {2} or more invalid login attempts and was kicked automatically.",
args.Player.IP, args.Player.Name, TShock.Config.Settings.MaximumLoginAttempts)); args.Player.IP, args.Player.Name, TShock.Config.Settings.MaximumLoginAttempts));
args.Player.Kick(I18n.C.GetString("Too many invalid login attempts.")); args.Player.Kick(GetString("Too many invalid login attempts."));
return; return;
} }
@ -798,7 +798,7 @@ namespace TShockAPI
{ {
if (String.IsNullOrEmpty(args.Parameters[0])) if (String.IsNullOrEmpty(args.Parameters[0]))
{ {
args.Player.SendErrorMessage(I18n.C.GetString("Bad login attempt.")); args.Player.SendErrorMessage(GetString("Bad login attempt."));
return; return;
} }

View file

@ -16,6 +16,8 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
global using static TShockAPI.I18n;
using System; using System;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
@ -42,5 +44,161 @@ namespace TShockAPI
} }
/// <value>Instance of a <c>GetText.Catalog</c> loaded with TShockAPI translations for user's specified language</value> /// <value>Instance of a <c>GetText.Catalog</c> loaded with TShockAPI translations for user's specified language</value>
public static Catalog C = new Catalog("TShockAPI", TranslationsDirectory, TranslationCultureInfo); public static Catalog C = new Catalog("TShockAPI", TranslationsDirectory, TranslationCultureInfo);
#region ICatalog forwarding methods
/// <summary>
/// Returns <paramref name="text"/> translated into the selected language.
/// Similar to <c>gettext</c> function.
/// </summary>
/// <param name="text">Text to translate.</param>
/// <returns>Translated text.</returns>
static string GetString(FormattableStringAdapter text)
{
return C.GetString(text);
}
/// <summary>
/// Returns <paramref name="text"/> translated into the selected language.
/// Similar to <c>gettext</c> function.
/// </summary>
/// <param name="text">Text to translate.</param>
/// <returns>Translated text.</returns>
public static string GetString(FormattableString text)
{
return C.GetString(text);
}
/// <summary>
/// Returns <paramref name="text"/> translated into the selected language.
/// Similar to <c>gettext</c> function.
/// </summary>
/// <param name="text">Text to translate.</param>
/// <param name="args">Optional arguments for <see cref="string.Format(string, object[])"/> method.</param>
/// <returns>Translated text.</returns>
public static string GetString(FormattableStringAdapter text, params object[] args)
{
return C.GetString(text, args);
}
/// <summary>
/// Returns the plural form for <paramref name="n"/> of the translation of <paramref name="text"/>.
/// Similar to <c>gettext</c> function.
/// </summary>
/// <param name="text">Singular form of message to translate.</param>
/// <param name="pluralText">Plural form of message to translate.</param>
/// <param name="n">Value that determines the plural form.</param>
/// <returns>Translated text.</returns>
public static string GetPluralString(FormattableStringAdapter text, FormattableStringAdapter pluralText, long n)
{
return C.GetString(text, pluralText, n);
}
/// <summary>
/// Returns the plural form for <paramref name="n"/> of the translation of <paramref name="text"/>.
/// Similar to <c>gettext</c> function.
/// </summary>
/// <param name="text">Singular form of message to translate.</param>
/// <param name="pluralText">Plural form of message to translate.</param>
/// <param name="n">Value that determines the plural form.</param>
/// <returns>Translated text.</returns>
public static string GetPluralString(FormattableString text, FormattableString pluralText, long n)
{
return C.GetString(text, pluralText, n);
}
/// <summary>
/// Returns the plural form for <paramref name="n"/> of the translation of <paramref name="text"/>.
/// Similar to <c>gettext</c> function.
/// </summary>
/// <param name="text">Singular form of message to translate.</param>
/// <param name="pluralText">Plural form of message to translate.</param>
/// <param name="n">Value that determines the plural form.</param>
/// <param name="args">Optional arguments for <see cref="string.Format(string, object[])"/> method.</param>
/// <returns>Translated text.</returns>
public static string GetPluralString(FormattableStringAdapter text, FormattableStringAdapter pluralText, long n, params object[] args)
{
return C.GetString(text, pluralText, n, args);
}
/// <summary>
/// Returns <paramref name="text"/> translated into the selected language using given <paramref name="context"/>.
/// Similar to <c>pgettext</c> function.
/// </summary>
/// <param name="context">Context.</param>
/// <param name="text">Text to translate.</param>
/// <returns>Translated text.</returns>
public static string GetParticularString(string context, FormattableStringAdapter text)
{
return C.GetParticularString(context, text);
}
/// <summary>
/// Returns <paramref name="text"/> translated into the selected language using given <paramref name="context"/>.
/// Similar to <c>pgettext</c> function.
/// </summary>
/// <param name="context">Context.</param>
/// <param name="text">Text to translate.</param>
/// <returns>Translated text.</returns>
public static string GetParticularString(string context, FormattableString text)
{
return C.GetParticularString(context, text);
}
/// <summary>
/// Returns <paramref name="text"/> translated into the selected language using given <paramref name="context"/>.
/// Similar to <c>pgettext</c> function.
/// </summary>
/// <param name="context">Context.</param>
/// <param name="text">Text to translate.</param>
/// <param name="args">Optional arguments for <see cref="string.Format(string, object[])"/> method.</param>
/// <returns>Translated text.</returns>
public static string GetParticularString(string context, FormattableStringAdapter text, params object[] args)
{
return C.GetParticularString(context, text, args);
}
/// <summary>
/// Returns the plural form for <paramref name="n"/> of the translation of <paramref name="text"/> using given <paramref name="context"/>.
/// Similar to <c>npgettext</c> function.
/// </summary>
/// <param name="context">Context.</param>
/// <param name="text">Singular form of message to translate.</param>
/// <param name="pluralText">Plural form of message to translate.</param>
/// <param name="n">Value that determines the plural form.</param>
/// <returns>Translated text.</returns>
public static string GetParticularPluralString(string context, FormattableStringAdapter text, FormattableStringAdapter pluralText, long n)
{
return C.GetParticularString(context, text, pluralText, n);
}
/// <summary>
/// Returns the plural form for <paramref name="n"/> of the translation of <paramref name="text"/> using given <paramref name="context"/>.
/// Similar to <c>npgettext</c> function.
/// </summary>
/// <param name="context">Context.</param>
/// <param name="text">Singular form of message to translate.</param>
/// <param name="pluralText">Plural form of message to translate.</param>
/// <param name="n">Value that determines the plural form.</param>
/// <returns>Translated text.</returns>
public static string GetParticularPluralString(string context, FormattableString text, FormattableString pluralText, long n)
{
return C.GetParticularString(context, text, pluralText, n);
}
/// <summary>
/// Returns the plural form for <paramref name="n"/> of the translation of <paramref name="text"/> using given <paramref name="context"/>.
/// Similar to <c>npgettext</c> function.
/// </summary>
/// <param name="context">Context.</param>
/// <param name="text">Singular form of message to translate.</param>
/// <param name="pluralText">Plural form of message to translate.</param>
/// <param name="n">Value that determines the plural form.</param>
/// <param name="args">Optional arguments for <see cref="string.Format(string, object[])"/> method.</param>
/// <returns>Translated text.</returns>
public static string GetParticularPluralString(string context, FormattableStringAdapter text, FormattableStringAdapter pluralText, long n, params object[] args)
{
return C.GetParticularString(context, text, pluralText, n, args);
}
#endregion
} }
} }

View file

@ -2,7 +2,7 @@ TShock supports the concept of multiple languages using `GetText.NET`.
CI is automatically designed to build and push updated `.po` files (the source for lang files) based on usage in TShock. CI is automatically designed to build and push updated `.po` files (the source for lang files) based on usage in TShock.
To use a localized string instead of a standard string, use `I18n.C.GetString()`. For example, `I18n.C.GetString("Invalid command!")` instead of `"Invalid command!"`. To use a localized string instead of a standard string, use `GetString()`. For example, `GetString("Invalid command!")` instead of `"Invalid command!"`.
A test language file has been added that converts a limited number of phrases to [Toki Pona](https://tokipona.org/), the language of good. To use this, you can set an environment variable called `TSHOCK_LANGUAGE` set to `tok`. For example, `TSHOCK_LANGUAGE=tok dotnet run --project TShockLauncher`. Then, run the `die` command. A test language file has been added that converts a limited number of phrases to [Toki Pona](https://tokipona.org/), the language of good. To use this, you can set an environment variable called `TSHOCK_LANGUAGE` set to `tok`. For example, `TSHOCK_LANGUAGE=tok dotnet run --project TShockLauncher`. Then, run the `die` command.