Compare commits

...

2 Commits

Author SHA1 Message Date
dependabot[bot]
28ce183cdc chore(deps): bump libraries/hl2sdk-cs2 from c57d5ab to 0b862d7 (#550)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-08-19 04:48:07 +00:00
Michael Wilson
dddf24d0f3 feat: add localizer extension methods for player language 2024-08-19 12:10:12 +10:00
3 changed files with 41 additions and 5 deletions

View File

@@ -1,4 +1,4 @@
using System.Globalization;
using System.Globalization;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
@@ -32,6 +32,17 @@ public class WithTranslationsPlugin : BasePlugin
// 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")]

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];
}
}