Compare commits

...

3 Commits

Author SHA1 Message Date
Michael Wilson
dddf24d0f3 feat: add localizer extension methods for player language 2024-08-19 12:10:12 +10:00
Yarukon
fce68cb160 Update SetStateChanged parameters (#541)
Co-authored-by: Michael Wilson <roflmuffin@users.noreply.github.com>
2024-08-19 01:58:25 +00:00
Ian Lucas
a82faeb5c8 Fix CBasePlayerController_SetPawn signature (#547) 2024-08-19 11:52:30 +10:00
4 changed files with 44 additions and 8 deletions

View File

@@ -35,8 +35,8 @@
"CBasePlayerController_SetPawn": {
"signatures": {
"library": "server",
"windows": "44 88 4C 24 ? 55 56 57 41 54 41 56 48 8D 6C 24 ?",
"linux": "55 48 89 E5 41 57 41 56 41 89 D6 41 55 49 89 FD 41 54 45 89 C4"
"windows": "48 89 74 24 ? 55 41 54 41 55 41 56 41 57 48 8D 6C 24 ? 48 81 EC ? ? ? ? 4C 8B F9",
"linux": "55 48 89 E5 41 57 41 56 41 55 49 89 FD 41 54 45 89 C4"
}
},
"CCSPlayerPawnBase_PostThink": {

View File

@@ -1,4 +1,4 @@
using System.Globalization;
using System.Globalization;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
@@ -22,18 +22,29 @@ public class WithTranslationsPlugin : BasePlugin
// A global `Localizer` is provided on the plugin instance.
// You can also use dependency injection to inject `IStringLocalizer` in your own services.
Logger.LogInformation("This message is in the server language: {Message}", Localizer["test.translation"]);
// IStringLocalizer can accept standard string format arguments.
// "This number has 2 decimal places {0:n2}" -> "This number has 2 decimal places 123.55"
Logger.LogInformation(Localizer["test.format", 123.551]);
// This message has colour codes
Server.PrintToChatAll(Localizer["test.colors"]);
// This message has colour codes and formatted values
Server.PrintToChatAll(Localizer["test.colors.withformat", 123.551]);
// This prints the message to all players in their respective language
PrintToAllPlayersLocalized("test.format", 123.456);
}
void PrintToAllPlayersLocalized(string key, params object[] args)
{
foreach (var player in Utilities.GetPlayers().Where(x => x.IsValid))
{
player.PrintToChat(Localizer.ForPlayer(player, key, args));
}
}
[ConsoleCommand("css_replylanguage", "Test Translations")]
public void OnCommandReplyLanguage(CCSPlayerController? player, CommandInfo command)
{

View File

@@ -0,0 +1,25 @@
using Microsoft.Extensions.Localization;
namespace CounterStrikeSharp.API.Core.Translations;
public static class LocalizerExtensions
{
/// <summary>
/// Returns a localized string using the locale of the specified player.
/// <remarks>Defaults to the server language if the player is invalid.</remarks>
/// </summary>
public static string ForPlayer(this IStringLocalizer localizer, CCSPlayerController? player, string key)
{
using WithTemporaryCulture temporaryCulture = new WithTemporaryCulture(player.GetLanguage());
return localizer[key];
}
/// <summary>
/// <inheritdoc cref="ForPlayer(Microsoft.Extensions.Localization.IStringLocalizer,CounterStrikeSharp.API.Core.CCSPlayerController?,string)"/>
/// </summary>
public static string ForPlayer(this IStringLocalizer localizer, CCSPlayerController? player, string key, params object[] args)
{
using WithTemporaryCulture temporaryCulture = new WithTemporaryCulture(player.GetLanguage());
return localizer[key, args];
}
}

View File

@@ -82,9 +82,9 @@ public static class VirtualFunctions
new(GameData.GetSignature("CBasePlayerPawn_RemovePlayerItem"));
public static Action<IntPtr, IntPtr> RemovePlayerItemVirtual = RemovePlayerItemFunc.Invoke;
public static MemoryFunctionVoid<IntPtr, IntPtr, int, short, short> StateChangedFunc =
public static MemoryFunctionVoid<IntPtr, IntPtr, int, short, int> StateChangedFunc =
new(GameData.GetSignature("StateChanged"));
public static Action<IntPtr, IntPtr, int, short, short> StateChanged = StateChangedFunc.Invoke;
public static Action<IntPtr, IntPtr, int, short, int> StateChanged = StateChangedFunc.Invoke;
public static MemoryFunctionVoid<IntPtr, int, long> NetworkStateChangedFunc = new(GameData.GetSignature("NetworkStateChanged"));
public static Action<IntPtr, int, long> NetworkStateChanged = NetworkStateChangedFunc.Invoke;