Skip to content

Commit

Permalink
feat: add localizer extension methods for player language
Browse files Browse the repository at this point in the history
  • Loading branch information
roflmuffin committed Aug 19, 2024
1 parent fce68cb commit dddf24d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
19 changes: 15 additions & 4 deletions examples/WithTranslations/WithTranslationsPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Globalization;
using System.Globalization;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
Expand All @@ -22,18 +22,29 @@ public override void Load(bool hotReload)
// 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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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];
}
}

0 comments on commit dddf24d

Please sign in to comment.