Skip to content

Commit

Permalink
Added translations
Browse files Browse the repository at this point in the history
  • Loading branch information
B3none committed Jan 28, 2024
1 parent 4f9c49a commit d10dfe9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 10 deletions.
27 changes: 17 additions & 10 deletions InstadefusePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
using CounterStrikeSharp.API.Modules.Utils;
using System.Numerics;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using InstadefusePlugin.Modules;

namespace InstadefusePlugin;

[MinimumApiVersion(147)]
public class InstadefusePlugin : BasePlugin
{
private const string Version = "1.3.3";
private const string Version = "1.4.0";

public override string ModuleName => "Instadefuse Plugin";
public override string ModuleVersion => Version;
public override string ModuleAuthor => "B3none";
public override string ModuleDescription => "Allows a CT to instantly defuse the bomb when nothing can prevent defusal.";

private static readonly string LogPrefix = $"[Instadefuse {Version}] ";
private static readonly string MessagePrefix = $"[{ChatColors.Green}Retakes{ChatColors.White}] ";
public static readonly string LogPrefix = $"[Instadefuse {Version}] ";
public static string MessagePrefix = $"[{ChatColors.Green}Retakes{ChatColors.White}] ";

private float _bombPlantedTime = float.NaN;
private bool _bombTicking;
Expand All @@ -27,9 +28,18 @@ public class InstadefusePlugin : BasePlugin

private List<int> _infernoThreat = new();

private Translator _translator;

public InstadefusePlugin()
{
_translator = new Translator(Localizer);
}

public override void Load(bool hotReload)
{
Console.WriteLine($"{LogPrefix}Plugin loaded!");

MessagePrefix = _translator["retakes.prefix"];
}

[GameEventHandler]
Expand Down Expand Up @@ -225,14 +235,15 @@ private void AttemptInstadefuse()
if (_heThreat > 0 || _molotovThreat > 0 || _infernoThreat.Any())
{
Console.WriteLine($"{LogPrefix}Instant Defuse not possible because a grenade threat is active!");
Server.PrintToChatAll($"{MessagePrefix}Instant Defuse not possible because a grenade threat is active!");
Server.PrintToChatAll(_translator["instadefuse.prefix"] + _translator["instadefuse.not_possible"]);
return;
}

var defuser = GetDefuser();

if (defuser == null)
{
Console.WriteLine($"{LogPrefix}No defusing player found.");
return;
}

Expand All @@ -250,9 +261,7 @@ private void AttemptInstadefuse()

if (!bombCanBeDefusedInTime)
{
var outputText = $"{defuser.PlayerName} was {ChatColors.DarkRed}{Math.Abs(timeLeftAfterDefuse):n3} seconds{ChatColors.White} away from defusing.";
Console.WriteLine($"{LogPrefix}{outputText}");
Server.PrintToChatAll($"{MessagePrefix}{outputText}");
Server.PrintToChatAll(_translator["instadefuse.prefix"] + _translator["instadefuse.unsuccessful", defuser.PlayerName, $"{Math.Abs(timeLeftAfterDefuse):n3}"]);

Server.NextFrame(() =>
{
Expand All @@ -266,9 +275,7 @@ private void AttemptInstadefuse()
{
plantedBomb.DefuseCountDown = 0;

var outputText = $"{defuser.PlayerName} defused with {ChatColors.Green}{bombTimeUntilDetonation:n3} seconds{ChatColors.White} left on the bomb.";
Console.WriteLine($"{LogPrefix}{outputText}");
Server.PrintToChatAll($"{MessagePrefix}{outputText}");
Server.PrintToChatAll(_translator["instadefuse.prefix"] + _translator["instadefuse.successful", defuser.PlayerName, $"{Math.Abs(bombTimeUntilDetonation):n3}"]);
});
}

Expand Down
59 changes: 59 additions & 0 deletions Modules/Translator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using CounterStrikeSharp.API.Modules.Utils;
using Microsoft.Extensions.Localization;

namespace InstadefusePlugin.Modules;

public class Translator
{
private IStringLocalizer _stringLocalizerImplementation;

public Translator(IStringLocalizer localizer)
{
_stringLocalizerImplementation = localizer;
}

public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
{
return _stringLocalizerImplementation.GetAllStrings(includeParentCultures);
}

public string this[string name] => Translate(name);

public string this[string name, params object[] arguments] => Translate(name, arguments);

private string Translate(string key, params object[] arguments)
{
var localizedString = _stringLocalizerImplementation[key, arguments];

if (localizedString == null || localizedString.ResourceNotFound)
{
return key;
}

var translation = localizedString.Value;

// Handle translation colours
return translation
.Replace("[GREEN]", ChatColors.Green.ToString())
.Replace("[RED]", ChatColors.Red.ToString())
.Replace("[YELLOW]", ChatColors.Yellow.ToString())
.Replace("[BLUE]", ChatColors.Blue.ToString())
.Replace("[PURPLE]", ChatColors.Purple.ToString())
.Replace("[ORANGE]", ChatColors.Orange.ToString())
.Replace("[WHITE]", ChatColors.White.ToString())
.Replace("[NORMAL]", ChatColors.White.ToString())
.Replace("[GREY]", ChatColors.Grey.ToString())
.Replace("[LIGHT_RED]", ChatColors.LightRed.ToString())
.Replace("[LIGHT_BLUE]", ChatColors.LightBlue.ToString())
.Replace("[LIGHT_PURPLE]", ChatColors.LightPurple.ToString())
.Replace("[LIGHT_YELLOW]", ChatColors.LightYellow.ToString())
.Replace("[DARK_RED]", ChatColors.DarkRed.ToString())
.Replace("[DARK_BLUE]", ChatColors.DarkBlue.ToString())
.Replace("[BLUE_GREY]", ChatColors.BlueGrey.ToString())
.Replace("[OLIVE]", ChatColors.Olive.ToString())
.Replace("[LIME]", ChatColors.Lime.ToString())
.Replace("[GOLD]", ChatColors.Gold.ToString())
.Replace("[SILVER]", ChatColors.Silver.ToString())
.Replace("[MAGENTA]", ChatColors.Magenta.ToString());
}
}
7 changes: 7 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"instadefuse.prefix": "[[GREEN]Retakes[NORMAL]] ",

"instadefuse.unsuccessful": "{0} was [DARK_RED]{1} seconds[WHITE] away from defusing.",
"instadefuse.successful": "{0} defused with [GREEN]{1} seconds[WHITE] left on the bomb.",
"instadefuse.not_possible": "Instant Defuse not possible because a grenade threat is active!"
}

0 comments on commit d10dfe9

Please sign in to comment.